Home CSS Selectors :root CSS :root Selector Definition
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.
Syntax
CSS Syntax Example: :root { --main-color: hotpink; }
Approach
How to Use :root Define shared tokens once on the root, then consume them with var().
Open a global stylesheet. Write a :root { … } rule. Declare custom properties such as --main-color. Use color: var(--main-color); (or similar) on components. Piece Required? Role :rootYes Targets the document root Custom properties Optional Shared tokens like --main-color var(--name)When using tokens Reads the value on descendants
Try it
Live Preview Switch token presets. The demo box reads variables the same way descendants read :root tokens.
Code
CSS Examples Each snippet focuses on :root. Extra styles stay minimal for study. Use Try It Yourself for a live editor.
1. Root styles 2. Variables 3. Theme tokens Example 1 — Style the Document Root Set a background on the root element.
<style>
:root {
background: yellow;
}
</style>
<p>The page root has a yellow background.</p> The page root has a yellow background.
How It Works Match the root. :root selects <html> in an HTML document.Set background. Yellow applies to the root element.Cascades down. The page canvas reflects that root background.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().
<style>
:root {
--main-color: hotpink;
}
p {
color: var(--main-color);
}
</style>
<p>This text uses the root color token.</p> This text uses the root color token.
How It Works Declare once. --main-color lives on :root.Inherit everywhere. Descendants can see that custom property.Read with var(). p uses var(--main-color).Change one place. Updating the token updates every use.Example 3 — Theme Tokens Group a few design tokens on :root.
<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> How It Works Define tokens. Color and padding sit on :root.Reuse both. The card reads each with var().Keep names clear. --main-color is easier to scan than a raw hex.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 specificitySame 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.
Compatibility
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.
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.
Remember
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 What does the CSS :root pseudo-class do? 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.
What is the difference between :root and html? In HTML they select the same element, but :root has higher specificity (0-1-0) than the html type selector (0-0-1).
Why put CSS variables on :root? Custom properties on :root inherit to every descendant, so any rule can read them with var(--name) from one central place.
Can I redefine :root variables in a media query? Yes. You can reset :root custom properties inside @media (for example prefers-color-scheme) to switch themes or responsive tokens.
Are custom property names case-sensitive? Yes. --main-color and --Main-color are different custom properties.
Is :root a runtime API? No. It is a CSS selector evaluated by the browser layout engine, not a Node.js or server runtime feature.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion