Definition and Usage
The :root pseudo-class selector selects the root element of the document.
This selector is used to clearly indicate that the top-level root element is being selected.
It is primarily used for declaring global CSS variables.
Basic Example
:root { /* In HTML documents, this selects the root <html> element. */
background-color: gold;
}
Using the :root pseudo-class selector explicitly indicates that CSS variables are declared at the root of the document
:root {
--main-color: #222;
--ci-color: #00a0e9;
--gutter: 25px;
}
In HTML documents, the root element is the <html> element. Therefore, both the :root pseudo-class selector and the element name selector html select the <html> element. However, there are two important differences between these selectors.
:root {
/* ... */
}
html {
/* ... */
}
- The
:rootpseudo-class selector provides greater semantic clarity because it explicitly targets the top-level element, rather than simply matching the element name selectorhtml. - The
:rootpseudo-class selector takes precedence over thehtmlelement selector due to its higher CSS specificity.
Things to Keep in Mind
The :root pseudo-class selector selects the root element of an HTML document.
In HTML documents, it selects the root <html> element. However, it can also be applied to documents and document structures other than HTML.
CSS is not limited to HTML; it can be applied to other documents and document structures as well.
For example, XML or SVG (Scalable Vector Graphics) documents can also be styled using CSS. In these cases, the root element may not be <html>.
Therefore, under specific circumstances, the :root pseudo-class selector may target the root element of a non-HTML document. However, since the vast majority of web development involves HTML documents, the :root pseudo-class selector is widely recognized as representing the HTML root element.
The following example demonstrates how CSS can be used to style an XML document. In this case, the :root pseudo-class selector targets the root element of the XML document.
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J.K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
In the XML example above, <bookstore> is the root element of the XML document. If you want to apply CSS to this XML document, you can use the :root pseudo-class selector as shown below.
:root { /* In this XML document, this selects the root <bookstore> element. */
font-family: Arial, sans-serif;
}
book {
display: block;
margin-bottom: 20px;
}
title {
font-weight: bold;
}
author {
font-style: italic;
}
year, price {
color: #666;
}
This demonstrates how the :root pseudo-class selector can be effectively used to style XML documents as well.
Specifications
| Specification | |
|---|---|
:root
|
Selectors Level 4 #root-pseudo |
Browser Compatibility
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
:root
|
1 | 12 | 1 | 1 |