Do custom CSS properties use one leading dash or two?(自定义 CSS 属性是否使用一个或两个前导破折号?)
问题描述
#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 属性是否使用一个或两个前导破折号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:自定义 CSS 属性是否使用一个或两个前导破折号
基础教程推荐
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
