:root
Example
body {
--text: #222;
}
html {
color: var(--text);
}:root {
--text: #222;
color: var(--text);
}Custom properties inherit downward, so tokens on :root are visible everywhere, including the html element.
Common mistakes
- An
htmlrule losing to a:rootrule, because:roothas class-level specificity. - Defining custom properties on
body, sohtml-level styles can't read them.
Key points
- Global design tokens are declared on
:root, like:root { --brand: #0055ff; }. - Theme overrides use
:root[data-theme='dark']or aprefers-color-schememedia query.
Interview questions
Is :root the same as html?
In HTML documents they match the same element, but :root has higher specificity, like a class.
Why are CSS variables defined on :root?
Because :root is the top element, so every element in the page inherits its custom properties.