The :root pseudo-class represents the document’s root element — usually <html>. It is the go-to place for defining global CSS custom properties that power consistent themes across your site.
01
Root element
<html> in HTML.
02
Variables
--token-name.
03
var()
Use tokens.
04
Themes
Color palette.
05
Responsive
Media queries.
06
vs html
Specificity.
Fundamentals
Introduction
The CSS :root selector targets the highest-level parent in the document tree. In an HTML page, that is the <html> element.
It is especially useful for defining CSS custom properties (variables) and global defaults that every component can inherit and reuse with var().
Definition and Usage
Declare shared design tokens inside :root, then reference them anywhere: color: var(--primary-color). This keeps colors, spacing, and typography consistent and easy to update in one place.
💡
Beginner Tip
Think of :root as your project’s settings file. Store brand colors and font sizes there once, then use var(--name) throughout your CSS instead of repeating hex codes.
Foundation
📝 Syntax
The syntax for the :root pseudo-class is:
syntax.css
:root{/* CSS properties */}
The :root pseudo-class targets the document root and behaves like html with higher specificity.
Dark theme when prefers-color-scheme: dark is active.
How It Works
Components keep using var(--bg) and var(--text). Only the token values on :root change, so the entire page theme flips automatically.
Tips
💬 Usage Tips
Name semantically — Use --text-primary not --blue-500 when possible.
Provide fallbacks — var(--color, #2563eb) guards against missing variables.
Keep :root lean — Store tokens globally; put component rules elsewhere.
Override locally — Redefine --accent on .card for scoped variants.
Pair with media queries — Responsive and dark-mode tokens live in nested :root blocks.
Prefer :root over html — Slightly higher specificity protects global variables.
Watch Out
⚠️ Common Pitfalls
Putting all CSS in :root — It is for global tokens, not every layout rule.
Missing -- prefix — Custom properties must start with two hyphens.
Forgetting var() — color: --primary is invalid; use var(--primary).
Over-nesting variables — Keep token layers simple for beginners.
Assuming instant IE support — CSS variables need modern browsers (fine for new projects).
Duplicating html and :root — Pick one primary place for design tokens.
A11y
♿ Accessibility
Contrast in themes — Ensure --text on --bg meets WCAG contrast in light and dark modes.
Respect prefers-color-scheme — Offer readable dark tokens when users prefer dark UI.
Respect prefers-reduced-motion — Store animation durations as tokens you can zero out.
Don’t shrink text too much — Keep minimum font-size tokens at least 1rem for body text.
Test focus colors — Define --focus-ring on :root for consistent keyboard visibility.
🧠 How :root Works
1
Declare tokens on :root
Custom properties like --primary-color are set on the document root.
CSS
2
Variables inherit
Every element in the document tree can access inherited custom properties.
Cascade
3
Components use var()
Rules reference var(--token) instead of hard-coded values.
var()
=
🎨
One place to update
Change a token on :root and the whole site reflects it.
Compatibility
🖥 Browser Compatibility
The :root pseudo-class and CSS custom properties are supported in all modern browsers.
✓ Baseline · Modern browsers
Global variables everywhere
:root and var() work in Chrome, Firefox, Safari, Edge, and Opera.
98%Global support
Google Chrome49+ · Desktop & Mobile
Full support
Mozilla Firefox31+ · Desktop & Mobile
Full support
Apple Safari9.1+ · macOS & iOS
Full support
Microsoft Edge15+
Full support
Opera36+
Full support
:root + CSS variables98% supported
Bottom line: Safe for modern projects. Use fallbacks in var() when supporting very old browsers.
Wrap Up
🎉 Conclusion
The CSS :root selector is a powerful tool for defining and managing global styles, particularly CSS custom properties. Its slightly higher specificity makes it the standard home for design tokens that keep your site consistent and maintainable.
Start with a small token set on :root, consume values with var(), and extend with responsive or dark-mode overrides as your project grows.
Use these points when setting up global CSS variables.
5
Core concepts
🌐01
Document root
<html> element.
Target
--02
Custom props
--token-name.
Syntax
var()03
Use tokens
Anywhere.
Consume
html04
Higher spec
Than html.
Tip
🌐05
98% support
All browsers.
Compat
❓ Frequently Asked Questions
The :root pseudo-class targets the document's root element — the <html> element in HTML. It is the standard place to define global CSS custom properties (variables) and site-wide defaults.
In HTML documents they select the same element, but :root has higher specificity (a pseudo-class vs an element selector). Developers prefer :root for global CSS variables because it won't be accidentally overridden by a bare html rule.
Custom properties declared on :root are inherited by every element in the document. Any component can use var(--token-name) to access shared colors, spacing, or font sizes from one central location.
Yes. You can redefine :root variables inside media queries or @media (prefers-color-scheme: dark) to create responsive or theme-aware design tokens.
:root is supported in all modern browsers. CSS custom properties used inside :root are also widely supported in current browsers.