:root
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
:root 这个 CSS 伪类匹配文档树的根元素。对于 HTML 来说,:root 表示 <html> 元素,除了优先级更高之外,与 html 选择器相同。
css
/* 选择文档的根元素(HTML 中的 <html>) */ :root { background: yellow; } 语法
css
:root { /* ... */ } 示例
>声明全局 CSS 变量
在声明全局 CSS 变量时 :root 会很有用:
css
:root { --main-color: hotpink; --pane-padding: 5px 42px; } 规范
| Specification |
|---|
| Selectors Level 4> # root-pseudo> |