:root
:root
该:rootCSS伪类与表示该文档的文档树的根元素相匹配。在HTML中,:root表示<html>元素,并且与选择器html相同,只是其特异性更高。
/* Selects the root element of the document */
/* <html> in the case of HTML */
:root {
background: yellow;
}
语法
:root
实例
:root
可以用于声明全局CSS变量:
:root {
--main-color: hotpink;
--pane-padding: 5px 42px;
}
规范
Specification | Status | Comment |
---|---|---|
Selectors Level 4The definition of ':root' in that specification. | Working Draft | No change |
Selectors Level 3The definition of ':root' in that specification. | Recommendation | Initial definition |
浏览器兼容性
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 1 | (Yes) | 1.0 (1.7 or earlier) | 9 | 9.5 | 1.0 |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | (Yes) | ? | ? | ? | ? |