×

CSS Specificity

CSS Specificity: If one or more conflicting rules of CSS express a similar element, a browser will pursue a few rules to examine the specific one. Specificity can be specified in a form in which it helps several browsers to determine which value of the property is most convenient for an element. Also, it specifies which declaration of style is used to the element.

Before starting to discuss the specificity, let’s understand some key points, which are listed below:

  • When multiple selectors are stirring any same element, only CSS specificity plays an important role. When any browser requires determining a style to use to any matching element, the CSS specificity comes and does it.
  • When more than two selectors have the same value of the specificity, a current one is considered. 
  • An inherited value and the universal selectors (*) have a lower (0) specificity.
  • The style property in CSS adds a higher specificity as compared to any selector, excluding the !important CSS property inside the selector of style-sheet.
  • The !important CSS property changes the specificity of the selectors. If two or more selectors have the same specificity, it includes !important.

Specificity Hierarchy

All the selectors contain a place inside the hierarchy of specificity. Here are four types in which we are defining the selector’s levels of specificity.

Inline styles: This hierarchy type can be directly fixed to an element that is to be designed. Example: <p style= “color: blue;”>. It will have a greater priority.

IDs: It is a special identifier for a lot of elements of the page that contains the priority as second-greater. Example: #para.

Classes, pseudo-classes, and attributes: It adds classes, pseudo-classes, and attributes (like :hover, :focus, etc.).

Pseudo-elements and elements: It adds the pseudo-elements (like :before and :after) and the element’s name (h1, div). They have the least priority.

Specificity Rules

The CSS specificity can be defined as a weight, which can be used for any declaration of CSS. Also, it is identified by a number of each selector type inside any matching selector.

The rules of specificity are mentioned below with the example.

  1. The ID selector’s specificity is greater than the attribute selectors

Let’s take an example to define it.

Example:

In the example, we are using an id selector along with the CSS property background-color.

<!DOCTYPE html>
<html>
<head>
<style>
body
{
text-align: center;
font-size: 30px;
color: white;
background-color: lime;
}
#div1
{
background-color: lime;
}
div#div1        /*Higher specificity*/
{
background-color: green;
}
div[id=div1]
{
background-color: red;
}
</style>
</head>
<body>
<div id="div1"> Welcome to this Page </div>
</body>
</html>

Output:

CSS Specificity
  • In the equal specificity, the current rule will consider

When any similar rules are used twice, any current rule will be considered in an external style-sheet.

Example:

In the following example, the element name’s specificity is the same. Thus, the current specified name of the element will be used.

<!DOCTYPE html>
<html>
<head>
<style>
body
{
text-align: center;
font-size: 30px;
}
div
{
background-color: lightblue;
color: blue;
}
div
{
background-color: lime;
color: green;
}
</style>
</head>
<body>
<h2> It is an example of The equal specificity </h2>
<div id="div1"> Welcome to this Page </div>
</body>
</html>

Output:

CSS Specificity
  • The class selector specificity is higher than any element selector

Class selector (.high, .nav, etc.) has higher specificity when compared to any element selector ( such as p, h1, and div).

Example:

<!DOCTYPE html>
<html>
<head>
<style>
.intro
{
background-color: lightblue;
color: blue;
text-align: center;
font-size: 40px;
}
div
{
background-color: lime;
color: green;
text-align: right;
}
</style>
</head>
<body>
<h1> Hello World </h1>
<div class= "intro"> Welcome to this Page </div>
</body>
</html>

Output:

CSS Specificity

Related Topics

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 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 Box Model

Introduction The Box Model property of CSS is used for design and layout of an html page. In CSS, all the elements has a rectangular box around it, you have to...

5 minutes read.

CSS Radio Button

Radio Button A radio button has been defined as an element of HTML. It can support us to take any input from the user. But it is challenging to design a...

4 minutes read.

CSS Word Wrap

Word Wrap in CSS: The word-wrap property in CSS is referred to break any lengthy word and wrap towards the next or following line. It is employed for preventing overflow...

1 minute 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 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.

How to change background color in CSS

How to change background color in CSS? The background-color property in CSS would be used to specify the color of an element's background. As the element's background, it uses solid colors. The...

3 minutes read.

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.

CSS Flex-Box

Introduction The flex-box property of CSS stands for Flexible Box Layout Module. It is specifically used to design a flexible responsive layout. Flex-box is a one dimensional layout model, i.e. it works...

6 minutes read.

CSS Background-blend-mode

Background-blend-mode The background-blend-mode property sets a blend mode of the component’s every background layer (color/image). It illustrates how the element background image blends well with the element background color. We can...

9 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 Browser Support

The desk underneath lists of all CSS properties and the way each belongs to it is supported in the generally unique browsers: - The quantity to the right of the browser...

2 minutes read.

CSS Line Height

CSS Line Height: This property in CSS can be used to describe the line box’s minimal height inside the element. It sets-up the distinction among two lines in our content. It...

3 minutes read.

Untitled Reusable Block

  ...

1 minute 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 Visibility

Visibility in CSS: This visibility property in CSS is employed to describe whether the components are visible or not. Note: An invisible component also grabs the space over the page. If...

2 minutes read.

CSS Page-break-before Attribute

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

5 minutes read.

CSS Sticky

CSS Sticky The position property in CSS is applied to set an element’s position. This property is also applied to position an element behind the other element. It is useful for...

1 minute 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.