假设我有以下CSS:
:root { --color: blue; }
div { --color: green; }
#alert { --color: red; }
* { color: var(--color); }
Run Code Online (Sandbox Code Playgroud)
我的标记是:
<p>I inherited blue from the root element!</p>
<div>I got green set directly on me!</div>
<div id="alert">
While I got red set directly on me!
<p>I’m red too, because of inheritance!</p>
</div>
Run Code Online (Sandbox Code Playgroud)
我的问题是上面的CSS是否转化为:
body {
color: blue;
}
div {
color: green;
}
#alert{
color: red;
}
Run Code Online (Sandbox Code Playgroud)
还是有额外的
* {
color: red;
}
Run Code Online (Sandbox Code Playgroud)
没有变量,通用选择器在所有元素上应用相同的CSS.这会改变,造型又取决于元素吗?
我还有一个问题是如果:root转换为bodyCSS. …
#elem {
-myCustom: 99;
}
Run Code Online (Sandbox Code Playgroud)
要么
#elem {
--myCustom: 99;
}
Run Code Online (Sandbox Code Playgroud)
我在网上的例子中看到过以上两种情况.两者有什么区别?
尝试在JavaScript中访问自定义属性返回null ..
#elem {
-myCustom: 99;
}
<div id="elem">some text</div>
elem = document.getElementById("elem");
style= window.getComputedStyle(elem);
value = style.getPropertyValue('-myCustom');
alert(value);
Run Code Online (Sandbox Code Playgroud)