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

编程入门 行业动态 更新时间:2024-10-18 18:29:27
本文介绍了自定义 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 属性是否使用一个或两个前导破折号?

本文发布于:2023-08-02 11:36:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277483.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:破折号   前导   自定义   属性   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!