×

CSS :root selector

CSS :root selector

This CSS pseudo-class matches any document’s root element. It chooses the parent of the highest-level inside the document DOM or tree.

An element of <html> is always a root element in HTML. But the :root selector is similar to the html selector. It is because both the elements target the same element, although the selector :root includes the higher specificity.

Syntax:

:root {  
    // CSS property  
}  

Example:

<!DOCTYPE html>
<html>
<head>
<title> :root selector </title>
<style>
:root
{
 background: yellow;
 color: red;
 text-align: center;
}
</style>
</head>
<body>
<h1> Welcome to this Page </h1>
<h2> The :root selector is specified in this example. </h2>
</body>
</html>

Output:

CSS :root selector

Let’s try another method in which :root selector or html selector will implement simultaneously. If both these selector will implement within the specificity matter, the :root selector will win.

In the following example, we are using similar properties in both the selectors, i.e., :root selector and html selector. The :root selector properties will execute due to the higher specificity. Although a few properties are defined in the html selector but not in the :root selector, the html selector’s properties will work.

Example:

<!DOCTYPE html>
<html>
<head>
<title> :root selector </title>
<style>
:root
{
 background: violet;
 color: purple;
 text-align: center;
}
html
{
 background: yellow;
 color: red;
 font-size: 30px;
}
</style>
</head>
<body>
<h1> Welcome to this Page </h1>
<h2> The html selector and :root selector is specified in this example. </h2>
</body>
</html>

As we can define in the above illustration that the color and background-color properties both are defined in :root selector and also in the html selector. Although in the result, the :root selector properties will execute, due to the :root selector’s higher specificity.

Output:

CSS :root selector

Related Topics

CSS Text-stroke

Text-stroke Text-stroke CSS property inserts the stroke to any text and gives a decoration option to them also. It describes the stroke’s width and color for various text characters. It is...

2 minutes read.

CSS Border

CSS Border: The border property of CSS allows us to define the style, color, width, and radius of the element’s border. It is applied to various elements to set-up the...

4 minutes read.

How to create blinking text using CSS

The blinking effect is used in HTML and CSS to take the attraction towards a particular word or section. If you need such a blinking effect, we need to take...

2 minutes read.

CSS Content Property

Content Property The content attribute produces dynamic content. It can be applied with any pseudo-element ::after and ::before. These CSS properties are entirely supported inside every browser and applied to include...

5 minutes read.

CSS Background Color

It is one of the CSS properties to apply styling to html elements.This property is used to apply the background color to any HTML element.The background of an element includes...

2 minutes read.

How to make smooth bounce animation using CSS

The smooth bouncing animation project is done with the help of HTML and CSS. This project gives so much fun and excitement to the users. What is HTML? The HTML or HyperText...

2 minutes read.

CSS Hover

CSS Hover: CSS :hover is used to select the various elements whenever we point the mouse on these elements. We can also use this selector on all the HTML elements,...

3 minutes read.

CSS z-index Property

CSS z-index: CSS z-index permits us to represent any visual hierarchy over a 3D plane that is the z-axis. This index can be applied to describe the positioned element’s stacking...

2 minutes read.

CSS Justify Content

Justify Content: These properties are applied for the alignment of items of a flexible box container if any item does not cover every available space over the main-axis, i.e., horizontally....

4 minutes read.

How to center a button in CSS

How to center a button in CSS? In the tutorial, we will learn how to center a button in CSS. CSS (Cascading Style Sheets) gives an HTML web page the greatest possible...

4 minutes read.

Create a 3D text effect using HTML and CSS

A3D text effect is a beautiful and attractive part of a webpage. As a web page developer, it is crucial to know how to create a 3D text effect with...

3 minutes read.

CSS Tutorial

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

9 minutes read.

CSS White Space Property

White Space in CSS The white-space CSS property describes how to show the content inside an element. This property is applied to manage a white space within an element. Values of White-space...

2 minutes read.

What is CSS used for

What is CSS used for? CSS stands for Cascading Style Sheet. CSS is a stylesheet language for web pages. It specifies how a document created in a markup language looks and...

4 minutes read.

CSS Introduction

CSS stands for Cascading Style Sheet. It is a design language which is used to design the web pages. It was developed by Håkon Wium Lie on October 10, 1994. It provides...

1 minute read.

What is CSS

CSS stands for Cascading Style Sheet. It gives an additional style to the HTML document. A cascading style sheet is a language that is designed to define the document formatting...

3 minutes read.

CSS Pseudo-elements

Pseudo-elements The pseudo-classes in CSS can be represented as any keyword which is connected to the selector that represents any selected element’s unique state. On the other hand, pseudo-elements can be...

6 minutes read.

CSS Text-Transform Property

Text-Transform in CSS The text-transform property permits us to alter the text case. It controls the capitalization of the text. This property is employed to design the text appearance in every...

3 minutes read.

How To Use CSS

You can use CSS(Cascading Style Sheet) in four ways. These are given below: The given above CSS type, in which various technique will be used. These are different from each other and...

2 minutes read.

How to change font size in CSS

How to change font size in CSS? Font Size: - Users may attract attention to content on a website page in various ways. One element in a sentence can be highlighted. You...

5 minutes read.