:root

概述

:root 这个 CSS 伪类匹配文档树的根元素。对于 HTML 来说,:root 表示 <html> 元素,除了优先级更高之外,与 html 选择器相同。

语法

:root

示例

在声明全局 CSS 变量:root 会很有用:

:root {
  --main-color: hotpink;
  --pane-padding: 5px 42px;
}

规范

Specification Status Comment
Selectors Level 4
:root
Working Draft No change
Selectors Level 3
:root
Recommendation Initial definition

浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help! (en-US)

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) ? ? ? ?