CSS :root Pseudo Class

The :root pseudo-class selects the element that represents the root of the document.

In HTML, the root element is always <html>.

In SVG and XML, the :root pseudo-class can refer to a higher-level ancestor.

The :root pseudo-class can be linked with pseudo-elements (e.g., ::after) and other pseudo-classes (e.g., :hover).

Version

Selectors Level 4

Selectors Level 3

Syntax

:root {
  css declarations;
}

Example of the :root pseudo-class:

<!DOCTYPE html>
<html>
  <head>
    <style>
      :root {
        background-color: #8ebf42;
        padding: 2em;
      }
      body {
        background-color: #eee;
        padding: 1.5em;
      }
    </style>
  </head>
  <body>
    <h2>:root selector example</h2>
    <p>Lorem ipsum is simply dummy text</p>
  </body>
</html>

Browser support

chrome edge firefox safari opera
4.0+ 12.0+ 3.5+ 3.2+ 10.0+

Practice Your Knowledge

What is the purpose of the ':root' pseudo-class selector in CSS?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?