html – 自定义CSS属性是使用一个或两个领先的破折号吗?

前端之家收集整理的这篇文章主要介绍了html – 自定义CSS属性是使用一个或两个领先的破折号吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. #elem {
  2. -myCustom: 99;
  3. }

要么

  1. #elem {
  2. --myCustom: 99;
  3. }

我已经在网上看到了以上两个例子.两者有什么区别?

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

  1. #elem {
  2. -myCustom: 99;
  3. }
  4.  
  5. <div id="elem">some text</div>
  6.  
  7. elem = document.getElementById("elem");
  8. style= window.getComputedStyle(elem);
  9. value = style.getPropertyValue('-myCustom');
  10. alert(value);

解决方法

>单个前导短划线用于供应商前缀
>双前导短划线用于定义 custom properties.

07001

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的一个例子:

  1. :root {
  2. --main-color: #06c;
  3. --accent-color: #006;
  4. }
  5. /* The rest of the CSS file */
  6. #foo h1 {
  7. color: var(--main-color);
  8. }

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

猜你在找的HTML相关文章