CSS Basic
CSS :root Selector
Photo Credit to CodeToFun
π Introduction
The :root
selector in CSS represents the highest-level parent element in the document tree, which is usually the <html>
element in an HTML document.
It is especially useful when defining global CSS variables (also known as CSS custom properties) and when applying global styles that affect the entire webpage.
π‘ Syntax
The signature of the :root
Selector is as follows:
:root {
/* CSS properties */
}
The :root
selector targets the root element of the document, and it behaves similarly to the html
selector, but with a higher specificity, making it more suitable for certain use cases like custom properties.
π Example
Hereβs an example demonstrating the use of the :root
selector to define CSS variables that are applied globally across the page.
β οΈ HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS :root Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello World</h1>
<p>This is a simple example demonstrating the power of the :root selector in CSS.</p>
</body>
</html>
π¨ CSS
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--font-size: 16px;
}
body {
font-size: var(--font-size);
color: var(--primary-color);
}
h1 {
color: var(--secondary-color);
}
In this example:
- CSS variables are declared within the
:root
selector to store values for primary color, secondary color, and font size. - These variables are then used throughout the CSS file with the
var()
function to apply consistent styles across the webpage.
π¬ Usage Tips
- The
:root
selector is often used when defining custom properties (CSS variables) to ensure they are available globally across the entire document. - Because
:root
has higher specificity than thehtml
selector, it can be a good option when you need to override styles more precisely. - You can combine
:root
with media queries to create responsive variables for dynamic designs.
π Example: Responsive Design with :root
:root {
--font-size: 16px;
}
@media (min-width: 768px) {
:root {
--font-size: 18px;
}
}
body {
font-size: var(--font-size);
}
In this case, the :root
selector adjusts the global font size based on the screen width, providing a more responsive user experience.
β οΈ Common Pitfalls
- Avoid overusing the
:root
selector to define all styles, as it is mainly intended for global settings, like variables. Itβs best to use it for properties that need to be accessible across the entire document. - Be mindful of browser support for CSS variables. While most modern browsers fully support them, some older versions might not render styles using
var()
correctly.
π Conclusion
The CSS :root
selector is a powerful tool for defining and managing global styles, particularly when working with CSS variables. Its higher specificity makes it a great choice for applying global values, ensuring consistency across your web pages.
By using :root
efficiently, you can simplify your CSS and make it more maintainable and scalable.
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS :root Selector), please comment here. I will help you immediately.