×

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 Background-clip

CSS Background-clip: The background-clip property describes the background’s painting area. It limits an area where the image or color of the background appears by using any clipping box. Everything will...

2 minutes read.

How to use google fonts in CSS

Fonts: - Fonts are used to make our website look more beautiful. Choosing appropriate font according to the web design or layout is also important. Many typefaces are accessible in...

4 minutes read.

CSS Letter-spacing

CSS Letter-spacing: Letter-spacing attribute is applied to manage the space among all letters within the block or element of the text. This property sets the behavior of the spacing between...

2 minutes read.

CSS Selector

CSS Selector: There are some selectors used in CSS, which allows us to choose the content we wish to style. Various selectors are considered as the separation of the set...

5 minutes read.

CSS Zoom

CSS Zoom: Zoom property in CSS extents the content. This property controls the content’s level of magnification. Rather than the use of this CSS property, we may also apply the...

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.

CSS Box-shadow

CSS Box-shadow This property is referred to include effects as shadow-like around an element’s frame. Syntax box-shadow: h-offset v-offset blur spread color |inset|inherit|none; Property values Following are the various property values which can be used...

3 minutes read.

CSS Dropdowns

Dropdowns are one of the maximum crucial parts of an interactive internet site. CSS is a kind used to design the drop-down menus. A drop-down essentially is a group of...

5 minutes read.

CSS Text-Overflow

Introduction: The text-overflow property of CSS is used to format the text which is overflowing the boundaries of the container. This property helps user to clip the text, to display the...

5 minutes read.

Hover condition for a:before and a:after in CSS

In CSS, :after and :before are used to add content after and before the element. The CSS pseudo-class is used to add some styling effect to the webpage. We can change...

4 minutes read.

CSS Order

CSS Order CSS order property in CSS describes the flex item’s order inside the flex or any grid container. It is applied to order any flex item. Note: If any component is...

3 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.

CSS 3D Transform

Introduction To understand 3D transformation property of transform you need to first understand the difference between 2D and 3D transform. 2D transform is a CSS effect which transforms the elements according to...

6 minutes read.

CSS Superscript and Subscript

Superscript and Subscript To specify the superscript and the subscript text, HTML provides us two tags that are <sup> and <sub>. The superscript content looks in the shorter font and half...

2 minutes read.

CSS Page-break-inside Attribute

Page-break-inside Attribute As its name implies, the page-break-before property is applied to include page break inside any element, at the time of printing any document. It adds the page break before...

3 minutes read.

Grid Vs Flexbox in CSS

Grid: - CSS Grid Layout is a 2D grid-based matrix-based layout method with columns and rows that simplifies web pages created by eliminating the need for floats and placement. A grid layout, like...

4 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.

CSS Checkbox style

CSS Checkbox style: It is an element of HTML, which is used to get input from various users. It is difficult to style any checkbox. However, some elements make it...

6 minutes read.

CSS Form

You can create attractive HTML form by using CSS. Style input Fields The Input Field width properties are used to determine the width of the input field. Let us see an example of CSS style...

2 minutes read.

CSS Loader

CSS Loader The loader is the animation that can alert a visitor regarding the loading of the page. We need to wait for some time. It will be helpful if any...

4 minutes read.