CSS :root Selector

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The :root pseudo-class matches the root element of the document. In HTML that is the <html> element.

It selects the same node as the html type selector, but with higher specificity (0-1-0 vs 0-0-1). Authors usually declare global CSS custom properties on :root so every element can use var(--name).

Baseline · Widely available.

Not deprecated. A long-standing structural pseudo-class; commonly paired with custom properties.

CSS Syntax

syntax.css
:root { { … }

Example: :root { --main-color: hotpink; }

Selector Forms

FormDescriptionTry it
:rootSelect the document root (html in HTML)Try it
Global variablesDeclare --tokens on :root for the whole pageTry it
Use with var()Read a root token on any descendantTry it
Theme tokensGroup color and spacing tokens on :rootTry it

How to Use :root

Define shared tokens once on the root, then consume them with var().

  1. Open a global stylesheet.
  2. Write a :root { … } rule.
  3. Declare custom properties such as --main-color.
  4. Use color: var(--main-color); (or similar) on components.
PieceRequired?Role
:rootYesTargets the document root
Custom propertiesOptionalShared tokens like --main-color
var(--name)When using tokensReads the value on descendants

Live Preview

Switch token presets. The demo box reads variables the same way descendants read :root tokens.

:root token presets

Changing tokens on the root updates every place that uses var(--main-color).

Token demo

Styled with var(--main-color)

Preset: --main-color: hotpink

CSS Examples

Each snippet focuses on :root. Extra styles stay minimal for study. Use Try It Yourself for a live editor.

Example 1 — Style the Document Root

Set a background on the root element.

CSS
<style>
  :root {
    background: yellow;
  }
</style>

<p>The page root has a yellow background.</p>
Try It Yourself

How It Works

  1. Match the root. :root selects <html> in an HTML document.
  2. Set background. Yellow applies to the root element.
  3. Cascades down. The page canvas reflects that root background.
  4. Same target as html. Prefer :root when you want higher specificity.

Example 2 — Global CSS Variables

Declare a token on :root and use it with var().

CSS
<style>
  :root {
    --main-color: hotpink;
  }

  p {
    color: var(--main-color);
  }
</style>

<p>This text uses the root color token.</p>
Try It Yourself

How It Works

  1. Declare once. --main-color lives on :root.
  2. Inherit everywhere. Descendants can see that custom property.
  3. Read with var(). p uses var(--main-color).
  4. Change one place. Updating the token updates every use.

Example 3 — Theme Tokens

Group a few design tokens on :root.

CSS
<style>
  :root {
    --main-color: teal;
    --pane-padding: 12px 16px;
  }

  .card {
    color: white;
    background: var(--main-color);
    padding: var(--pane-padding);
  }
</style>

<div class="card">Card using root tokens</div>
Try It Yourself

How It Works

  1. Define tokens. Color and padding sit on :root.
  2. Reuse both. The card reads each with var().
  3. Keep names clear. --main-color is easier to scan than a raw hex.
  4. Scale later. Add more tokens the same way for fonts or gaps.

Edge Cases & Pitfalls

Most confusion is about specificity and where variables are defined.

vs html

:root beats html in specificity

Same element in HTML, but :root wins over a bare html rule when both set the same property.

scope

Not every variable must be global

You can declare --token on a component instead of :root to limit inheritance.

case

Names are case-sensitive

--Color and --color are different. Keep a consistent naming style.

media

Override tokens in media queries

Redefine :root variables inside @media (for example color scheme) to theme the site.

shadow

Document root ≠ shadow root

:root is the document tree root. Shadow DOM trees use other selectors such as :host.

Browser Support

:root is a browser CSS selector. Node.js, Deno, and Bun do not evaluate it.

✓ Baseline · Widely available

:root

Supported in all modern browsers (Chrome 1+, Firefox 1+, Safari 1+, Edge 12+, IE 9+). Custom properties on :root need IE-free modern engines.

98% Selector support
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1+
Full support
Microsoft Edge 12+
Full support
Opera 9.5+
Full support
Samsung Internet 1+
Full support
Internet Explorer 9+
Selector only*
:root Widely supported

Bottom line: *IE 9+ understands :root as a selector, but CSS custom properties need modern browsers. It is not a JavaScript runtime API.

Key Takeaways

  • Role: select the document root (html in HTML).
  • Tokens: put global --variables on :root.
  • vs html: same element, higher specificity with :root.
  • Consume: use var(--name) anywhere in the tree.

One line: :root targets the document root—ideal for global design tokens via custom properties.

Frequently Asked Questions

It matches the root element of the document. In HTML that is the <html> element. It is the usual place to declare global CSS custom properties.
In HTML they select the same element, but :root has higher specificity (0-1-0) than the html type selector (0-0-1).
Custom properties on :root inherit to every descendant, so any rule can read them with var(--name) from one central place.
Yes. You can reset :root custom properties inside @media (for example prefers-color-scheme) to switch themes or responsive tokens.
Yes. --main-color and --Main-color are different custom properties.
No. It is a CSS selector evaluated by the browser layout engine, not a Node.js or server runtime feature.

Next: :target Pseudo-class

Continue through the CSS tutorial reference.

:target Pseudo-class tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful