Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Basic

CSS :root Selector

Posted in CSS Tutorial
Updated on Oct 27, 2024
By Mari Selvan
πŸ‘οΈ 14 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
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:

Syntax
Copied
Copy To Clipboard
: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

HTML
Copied
Copy To Clipboard
<!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

CSS
Copied
Copy To Clipboard
: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 the html 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

CSS
Copied
Copy To Clipboard
: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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy