CSS 中的全局变量在内存或效率方面是否比局部 CSS 变量低?
所以基本上我的问题是,与在特定选择器的代码块中声明并相对于选择器本地作用域的变量相比,在全局范围内声明并且可以在CSS中的任何位置访问的变量是否有任何好处。
当谈论全球范围时,我的意思是:
:root { --mainColor: red }
本地范围意味着:
.element { --mainColor: red; }
.element p { color: var(--mainColor) }
希望我说得足够清楚:)
我不同意这样的定义和使用local and global变量,因为 CSS 不是一种编程语言,它是关于级联的。
你说:
本地范围意味着:
.element { --mainColor: 红色; }
.element p { 颜色:var(--mainColor) }
根据你可以说这是一个本地范围?你不知道该类将在哪里使用。如果我们将这样的类添加到html
那么所有元素都将访问/继承自定义属性,我们可以说自定义属性在 DOM 中全局可用。它将与定义属性完全相同:root
.
Custom properties are ordinary properties, so they can be declared on any element, are resolved with the normal inheritance and cascade rules ref so I don't think that performance will change based on where you declare the property. The perfermance will depend on the HTML used with your CSS. A CSS definition has no meaning without a DOM where it's applied.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)