#elem {
-myCustom: 99;
}
或
#elem {
--myCustom: 99;
}
我在网上的例子中看到了上述两种方法.两者有什么区别?
I have seen both of the above used in examples online. What the difference between the two?
尝试在 JavaScript 中访问自定义属性返回 null..
Trying to access custom properties in JavaScript returns null..
#elem {
-myCustom: 99;
}
<div id="elem">some text</div>
elem = document.getElementById("elem");
style= window.getComputedStyle(elem);
value = style.getPropertyValue('-myCustom');
alert(value);
2 定义自定义属性:'--*' 系列属性一个>
自定义属性是名称以两个破折号开头的任何属性(U+002D HYPHEN-MINUS),如 --foo.<custom-property-name>生产对应于此:它被定义为任何有效的标识符以两个破折号开头.
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 及更高版本中实现的.
It's worth noting that CSS variables are implemented in Firefox 31 and newer.
这篇关于自定义 CSS 属性是否使用一个或两个前导破折号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
每隔三个元素设置样式?Style every third element?(每隔三个元素设置样式?)
为什么我不应该在 CSS 中使用 ID 选择器?Why shouldn#39;t I use ID selectors in CSS?(为什么我不应该在 CSS 中使用 ID 选择器?)
CSS 中的 img[class*=“align"] 是什么意思?What does img[class*=quot;alignquot;] mean in CSS?(CSS 中的 img[class*=“align] 是什么意思?)
CSS:最后一个元素CSS: Last element on line(CSS:最后一个元素)
如何仅使用 CSS(无 js)选择所有其他 div 类元素How do I select every other div class element using just CSS (no js)(如何仅使用 CSS(无 js)选择所有其他 div 类元素)
检查未使用的 CSS 选择器的工具?Tool for checking unused CSS selectors?(检查未使用的 CSS 选择器的工具?)