DEV Community

Cover image for CSS Specificity
Shraddhaaa ☾︎
Shraddhaaa ☾︎

Posted on

CSS Specificity

As a Frontend Developer, you might have came across the situation, where your CSS isn't working as expected or not getting applied where it should be. And then you end up wasting your time writing wrong CSS or refactoring all styles here and there.

ugh-meme

Ugh!!
Here CSS Specificity comes into picture. And why you should know it as a frontend developer.

What is CSS Specificity?

What internet says,

CSS Specificity is the set of rules applied to the CSS selectors to determine which style will be applied to an element.

We know CSS stands for Cascading Style Sheet, and cascading means the sequence of order in which CSS rules will be applied.

Before going ahead, let' revise CSS Selectors.

CSS Selectors

  • Universal Selector ( * )
* {
    background-color: red;
  }
Enter fullscreen mode Exit fullscreen mode

Universal Selector's style will apply throughout the webpage but it has no specificity. That means any other style with more points will take over it.

  • Inline Styles
<div style="color: red;">Hello World!</h1>
Enter fullscreen mode Exit fullscreen mode

Inline style has highest priority. Which will override other style.

  • IDs ( # )
<div id="my-heading">Hello World</h1>
Enter fullscreen mode Exit fullscreen mode
#my-heading {
  color: blue;
}
Enter fullscreen mode Exit fullscreen mode

ID have second highest priority.

  • Classes, pseudo-classes and attributes
.my-heading {
  color: blue;
}
Enter fullscreen mode Exit fullscreen mode

Classes, pseudo-classes and attributes will have less priority as compare to IDs.

  • Elements and Pseudo Elements
div {
  color: gray;
}

//pseudo element
::selection {
  color: yellow;
}
Enter fullscreen mode Exit fullscreen mode

Elements and Pseudo elements has lowest priority.

  • !important
.my-heading {
  color: pink !important;
}
Enter fullscreen mode Exit fullscreen mode

An !important has the highest specificity and will take over all the other CSS properties.
When we use !important, no other style like inline style, ID, class will work.

specificity-

Let's see some example,

Example 1

ul > li {
  color: red;
}
Enter fullscreen mode Exit fullscreen mode

Specificity: 0 - 0 - 0 - 2

list > list-items {
  color: blue;
}
Enter fullscreen mode Exit fullscreen mode

Specificity: 0 - 0 - 2 - 0

So, the second will override the first as it has the high specificity.

Example 2

  a.my-class.another[href]:hover {
    //some properties
  }
Enter fullscreen mode Exit fullscreen mode

Specificity: 0 - 0 - 4 - 1

Visualizing Specificity

For inline styling, the specificity will be

inline-visual

For IDs styling, the specificity will be

id-visual

For Classes, pseudo-classes and attributes styling, the specificity will be

classes-visual

For Elements and pseudo elements styling, the specificity will be

pseudo-element-visual


Hope now have understood the CSS Specificity.

Resources

Specificity

CSS Selectors

Top comments (2)

Collapse
 
shubhamkumar648 profile image
Shubham Kumar

well articulate

Collapse
 
shraddhaaa7 profile image
Shraddhaaa ☾︎

Thank you!