Sitemap
Lu-Vuong Le

Lu-Vuong Le

Software Engineer 💻 Mentor 👥 Technology Enthusiast 💾 Blogger 📝 Aspiring Speaker 💬 Content Creator 📺 Learning code every day 📖

What Exactly is CSS Specificity?

4 min readAug 18, 2018

--

Press enter or click to view image in full size
Photo ?by Pankaj Patel on Unsplash

As a front-end developer, It is expected that you would know CSS fairly well in order to create layouts that match what the client is asking for. While I’m sure many of us know CSS well, I would like to touch on about a topic which is CSS Specificity. I would imagine that not many people who are new or even intermediate at CSS may know what this is. For me, I did not know about this for a while during my journey.

What is CSS Specificity?

CSS Specificity is the means in which a browser decide which CSS property values are used. This is based on matching rules made up of different CSS selectors. CSS Specificity becomes important as it could be a reason as to why you find that your CSS doesn’t always work as expected.

As an example, here we have some HTML which has an unordered list with some list items. The first list item has a class of ‘first-item’.

<body>    <ul>        <li class="first-item">Test 1</li>        <li>Test 2</li>        <li>Test 3</li>        <li>Test 4</li>        <li>Test 5</li>    </ul></body>

Here we have our CSS, we want to target the list item with the class ‘first-item’ and change the color of the text but we have two selectors targeting the same thing. Take a guess and see if you can figure out what color the text will be and why?

.first-item {
color: red;
}
ul li.first-item {
color: blue;
}

If you guessed blue, you are correct! This is because the specificity is higher in the second selector below. We will talk about why in the next section.

How To Calculate CSS Specificity

You might be wondering how to actually calculate specificity, well it is actually quite simple. Specificity is based on this order

  1. Inline Styles
  2. ID’s
  3. Classes, attributes, and pseudo-classes
  4. Elements and Pseudo Elements

For the selector we wish to calculate, we go through it and +1 for each of the above categories. We will go into more depth using the examples above.

Get Lu-Vuong Le’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s calculate the examples above now:

So first, we have just a simple class,

.first-item { color: red; } 
ul li.first-item { color: blue; }

Now then, using the order we showed above:

Inline Styles: 0 (There are no inline styles on the HTML)
ID’s: 0 (There are no ID’s present)
Classes & Attributes & Pseudo Classes: 1 (There is only the class ‘first-item’)
Elements & Pseudo-Elements: 0 (There are no Element and Pseudo-Elements)

As a result, the specificity is 0,0,1,0 which you can merge into a number such as 0010 which can be looked at as 10.

For the second example:

ul li.first-item {
color: blue;
}

Inline Styles: 0 (There are no inline styles on the HTML)
ID’s: 0 (There are no ID’s present)
Classes & Attributes & Pseudo Classes: 1 (There is only the class ‘first-item’)
Elements & Pseudo-Elements: 2 (There are no pseudo elements but there are 2 regular elements ‘ul’ and ‘li’)

As a result, the specificity is 0,0,1,2 or 0012.

In this case, 0012 > 0010, and because of this the second CSS selector would be set and the text color will be blue.

Of course, the order in which you write does matter to some degree, for example, if we had both selectors of the same specificity:

.first-item {
color: blue;
}
/* Other Styles */
...
.first-item {
color: red;
}

The selector written last will take effect and as a result, the color would be red because it was written later than the first one.

Finally, It can be tedious to always calculate this manually as such there is a good calculator for this at https://specificity.keegan.st/. This calculator can help you calculate the specificity by just pasting in the CSS selector.

Some other good reads if you are interested in this topic are:

https://css-tricks.com/specifics-on-css-specificity/

https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity

Important Notes

  • The Universal Selector * contributes nothing to the specificity, in other words, it has 0,0,0,0. The same also goes for any combinators such as (~) or (+).
  • The :not() pseudo-class also contributes nothing, however, elements or classes in the parentheses do count.
  • Inline styles always overwrite styles in an external CSS document so they have the highest specificity.
  • The !important is an exception in CSS specificity as anything marked with !important will always take the highest priority. Although it is best to avoid using !important and trying to level the specificity.
  • You should use !important for specific use cases, for example only for when you need to override styles used in a third party library or use of utility classes.

--

--

Lu-Vuong Le
Lu-Vuong Le