:root CSS Pseudo Class
Description
The :root
pseudo-class in CSS is a special selector that represents the highest-level parent element in an HTML document, typically the <html>
element. It is primarily used to define global CSS variables, also known as CSS custom properties, which can store values like colors, fonts, and other properties that you want to reuse throughout your stylesheet. These variables can then be referenced and updated throughout your CSS code, making it easier to maintain and customize the styling of a webpage.
Here's a simple example of how :root
is used to define and use CSS variables:
:root {
--primary-color: #007bff;
--font-family: Arial, sans-serif;
}
body {
background-color: var(--primary-color);
font-family: var(--font-family);
}
.button {
background-color: var(--primary-color);
color: #fff;
padding: 10px 20px;
border: none;
}
In this example, we define two CSS variables within the
:root
pseudo-class (--primary-color
and --font-family
). These variables are then used throughout the stylesheet, making it easy to maintain a consistent design by changing the variable values in one place rather than updating multiple CSS rules individually.
Syntax
:root { /* ... */ }
Example
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
:root {
background: #988871;
padding-top: 30px;
}
html {
background: red;
padding-top: 0;
}
Browser Support
The following table will show you the current browser support for the CSS :root
pseudo class.
Desktop | |||||
![]() |
![]() |
![]() |
![]() |
![]() |
|
12 | 1 | 1 | 9.5 | 1 |
Tablets / Mobile | |||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
18 | 4 | 14 | 1 | 1 | 37 |
Last updated by CSSPortal on: 1st October 2023