html - 自定义 CSS 属性是否使用一个或两个前导破折号?

标签 html css styles css-selectors css-parsing

#elem {
  -myCustom: 99;
}

#elem {
  --myCustom: 99;
}

我在网上的例子中看到了以上两种情况。两者有什么区别?

尝试访问 JavaScript 中的自定义属性返回 null..

#elem {
-myCustom: 99;
}

<div id="elem">some text</div>

elem = document.getElementById("elem");
style= window.getComputedStyle(elem);
value = style.getPropertyValue('-myCustom');
alert(value);

最佳答案

  • 单前导破折号用于供应商前缀
  • 双前导破折号用于定义custom properties .

2 Defining Custom Properties: the '--*' family of properties

A custom property is any property whose name starts with two dashes (U+002D HYPHEN-MINUS), like --foo. The <custom-property-name> production corresponds to this: it’s defined as any valid identifier that starts with two dashes.

来自 W3C 的示例:

:root {
  --main-color: #06c;
  --accent-color: #006;
}
/* The rest of the CSS file */
#foo h1 {
  color: var(--main-color);
}

值得注意的是,CSS 变量在 Firefox 31 及更新版本中实现。

关于html - 自定义 CSS 属性是否使用一个或两个前导破折号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26516942/

相关文章:

CSS图像线性渐变封面图像

javascript - 如何确定 if::before 应用于元素?

html - 在此上下文中,元素样式不允许作为元素主体的子元素。 (抑制来自该子树的更多错误。)

javascript - 如何使用小数的最大长度

javascript - 如何使用 serializeArray() 验证其默认值的表单字段。?

javascript - IE <= 8 相当于 selection.getRangeAt()?

javascript - 无法让图像适合正文元素的背景

html - 如何使用粘性顶部导航、侧边栏和页脚开发 Bootstrap 布局

asp.net - CSS 与不同浏览器的兼容性

php - 如何通过点击php中的链接来显示消息?