:root - Selector CSS
Summary of characteristics of the :root selector
Description of the :root pseudo-class.
The pseudo-class :root targets to the root element of the document.
In HTML, :root refers to the html element, which is the root element of any properly constructed HTML page.
The selectors html and :root therefore both refer to the same HTML page element, but be careful, :root has a higher priority
(its specificity). Refer to the page on Priorities for more information.
Thus, in the example below, the page text will be in red.:
:root {color:red;}
html {color:blue;}
Example of use of the pseudo-class :root.
Set the font size.
The goal is to set a default font size on the root element, and to define the font size of other elements using a relative unit. It will then be easy to change the size of all fonts by modifying only that of the root element. And the ratio between font sizes will be maintained. The relative units suitable for defining font sizes are:
- Percentages, calculated relative to the font size of the parent element.
- The
remunit: the size of the characters of the element is determined relative to the size of the characters of the root element. - The
emunit: the size of an element's characters is determined relative to the size of the parent element's characters.
:root {font-size:12pt;}
h1 {font-size:2rem;}
h2 {font-size:1.75rem;}
h3 {font-size:1.5rem;}
Define global variables (custom properties).
In CSS, it is preferable to use the term "custom properties" rather than the word "variables".
Refer to the function var() for more information on using variables in CSS.

These variables have a scope, a visibility, which depends on the selector used to declare them. To define global variables, which can therefore be used
on all elements of the page, the simplest way is to declare them with the :root selector.
:root {
--bg-color: #a0c0f0;
}
Access :root with JavaScript.
Two JavaScript syntaxes are possible to access the element :root. Here are two code examples to change the font size of the root element:
document.documentElement.style.fontSize="12pt";
// or
document.querySelector(':root').style.fontSize="12pt";
Browser compatibility with :root.
The pseudo-class :root poses no compatibility issues with current browsers.
:root targeting the root element of the document.:rootpseudo-class
Browsers on computers :
Mobile browsers :
Outdated or marginal browsers :

Internet Explorer

QQ Browser

Opéra Mobile

Baidu Browser

KaiOS Browser

UC Browser pour Androïd

Safari

Opéra

Chrome

Edge

Firefox

Safari sur IOS

Androïd Brower

Chrome pour Androïd

Firefox pour Androïd

Samsung Internet

Opéra mini
Historic of the :root pseudo-class.
-
Selectors Level 3
Introduction of the pseudo-class:rootin level 3 of the selectors specification.August 03, 1999Working Draft.November 13, 2001Candidate Recommendation.December 15, 2009Proposed for recommendation.September 29, 2011Recommendation . -
Selectors Level 4
No changes regarding the pseudo-class:rootat this level of the specification.September 29, 2011Working Draft.
See also: DOM-related pseudo-classes.
The pseudo-classes below refer to one or more elements based on their position in the document tree (DOM).


















