CSS Reference ยท Pseudo-classes
The :root pseudo-class
Reference for the CSS :root pseudo-class: match the root element of the document, why it is used for custom properties, and how its specificity compares to html.
The :root pseudo-class matches the root element of the document. In an HTML page that is the <html> element; in an SVG file it is the outer <svg> element.
Almost everyone meets :root as the place to declare global custom properties, because every element inherits from the root. Its specificity (0, 1, 0) is higher than the html type selector (0, 0, 1), which is sometimes useful for overriding.
:root { --brand: #0ea5e9; font-size: 100%; }
Try it
Both tiles read the variables set on :root. The second tile redefines --brand for itself, which shows that the root only provides defaults.
Syntax
:root { /* declarations */ }
- Arguments: none.
- Specificity: (0, 1, 0).
- Matches: the one element that is the root of the document tree. There is only ever one match.
Values
| Syntax and arguments | Description |
|---|---|
:root |
No arguments. Matches the document's root element. |
:root.dark |
The root element that also has the class dark, a common way to switch themes. |
:root[data-theme="dark"] |
The same idea with a data attribute. |
Usage notes
- Custom properties. Properties declared on
:rootare inherited by every element, so it is the standard home for design tokens such as colours, spacing and font stacks. htmlversus:root. In HTML both select the same element, but:rootwins a specificity tie againsthtml. Use whichever reads better; just be consistent.- Root font size. The
remunit is relative to the root element's font size. Set the root size in percent (for example100%) or leave it alone, so user font settings are respected. Avoid fixing it inpx. - Theming. Toggling a class or attribute on the root element and redefining custom properties under it re-themes the whole page without touching other rules.
- Inside
@scopeor shadow DOM.:rootstill means the document root. For the root of a scope, use:scope; for a shadow tree host,:host.
Browser support
Supported in every browser.
Related
:scopeโ the scoping root:empty:has()โ for example:root:has(dialog[open])- Lesson: Custom Properties
- Lesson: Pseudo-classes