:root CSS Pseudo Class
Description
The :root pseudo-class in CSS represents the highest-level parent of the document, typically the <html> element in an HTML document. It provides a convenient way to define global CSS variables, set default values, and apply styles that can cascade throughout the entire page. Because it has a very high specificity, styles declared on :root can override normal declarations on standard elements unless more specific selectors are used. This pseudo-class does not require any additional attributes or classes and is recognized by all modern browsers.
One of the primary uses of :root is to define CSS custom properties (variables). For example, you might define a color palette or font sizes that can be reused across multiple elements, making your stylesheet more maintainable and scalable:
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--base-font-size: 16px;
}
body {
font-size: var(--base-font-size);
color: var(--primary-color);
}
a {
color: var(--secondary-color);
}
In this example, the color and font-size of multiple elements are controlled via variables defined on :root. This makes it easier to implement themes or switch values dynamically using JavaScript.
Another important aspect of :root is its role in the context of media queries or global style overrides. Since it targets the top-level element, you can define base measurements, responsive breakpoints, or high-level settings that other selectors inherit naturally. For instance:
:root {
--spacing-unit: 8px;
}
.container {
padding: calc(var(--spacing-unit) * 2);
}
This ensures consistent spacing across all elements, as all values are derived from a single source.
Additionally, while :root commonly mirrors the <html> element, using it instead of directly styling <html> can enhance specificity management, especially when combining with other pseudo-classes like :hover or :focus. This helps prevent unintended overrides and ensures your global variables and base styles remain reliable throughout your CSS architecture.
Syntax
:root {
/* ... */
}
Example
Browser Support
The following information will show you the current browser support for the CSS :root pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS psuedo class.
This psuedo class is supported by all modern browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 31st December 2025
