Skip to content
Tutorials Center
My learning

Topics

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.

CSS
:root {
  --brand: #0ea5e9;
  font-size: 100%;
}

Try it

Global variables on :root

Both tiles read the variables set on :root. The second tile redefines --brand for itself, which shows that the root only provides defaults.

Syntax

CSS
: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 :root are inherited by every element, so it is the standard home for design tokens such as colours, spacing and font stacks.
  • html versus :root. In HTML both select the same element, but :root wins a specificity tie against html. Use whichever reads better; just be consistent.
  • Root font size. The rem unit is relative to the root element's font size. Set the root size in percent (for example 100%) or leave it alone, so user font settings are respected. Avoid fixing it in px.
  • 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 @scope or shadow DOM. :root still means the document root. For the root of a scope, use :scope; for a shadow tree host, :host.

Browser support

Supported in every browser.