CSS Specificity
Tags: css
Published: 2019-01-21 12:05:38

---
title: CSS Specificity
published: true
description: CSS Specificity is the set of the rules applied to CSS selectors in order to determine which style is applied to an element. The more specific a CSS style is, the higher point value it accrues, and the likelier it is to be present on the element's style.
tags: CSS
cover_image: https://cdn-images-1.medium.com/max/800/1*6hHvO7awHiPRZ06gK1syFQ.png
---

CSS Specificity is the set of the rules applied to CSS selectors in order to determine which style is applied to an element. The more specific a CSS style is, the higher point value it accrues, and the likelier it is to be present on the element's style.

There are many benefits of understanding CSS Specificity and leveraging it to your benefit.

![Helps understand why styles aren't applied](https://cdn-images-1.medium.com/max/1600/1*jTWDAwjQO4eZG4rbZXUHPw.png)

To understand why your CSS styles aren't being applied to an element, you must first have a basic understanding of CSS Specificity.

It's so easy to just slap an `!important` value on your CSS styles, but this causes your CSS to quickly spiral out of control.

By understanding how styles are applied, you can ensure the styles you want displayed are being rendered.

![Helps organize code](https://cdn-images-1.medium.com/max/1600/1*-Lv_Ydk9z4lZiHiuW5NQdw.png)

CSS can quickly become unruly when we don't stop to think about an architecture for our style sheets, and instead throw a ton of CSS selectors around without thinking about specificity.

One way to combat messy CSS, and ensure your specificity rules are being applied as intended, is to adopt a CSS naming architecture. [Block-element-modifier](http://getbem.com/) (BEM) is one of the most commonly used CSS naming architectures. We won't delve into naming architectures now, but they can help you ensure that your styles aren't overriding each other.

By making the most of CSS Specificity, you ensure that your code is organized, and your selectors won't conflict with one another.

![Helps write cleaner code](https://cdn-images-1.medium.com/max/1600/1*7hNrXKlV3mpayUQf7SjN3Q.png)

It's not uncommon to see a code base riddled with `!important` overrides. Although `!important` gives you a way to say "Forget all those other styles, I want this one to be applied!!!" it can cause serious issues when you need to update a style and you're not sure where to begin.

CSS Specificity allows you to include the CSS necessary to style your elements, in the correct way. It's easier to quickly change a style when you know exactly which selector styles that specific element. Plus, you'll probably find yourself writing less CSS code overall, which will help with maintainability.

![How does it work](https://cdn-images-1.medium.com/max/1600/1*8Bzk70AazLc6XlwxfsNlFw.png)

At a high-level, there are three buckets of specificity your CSS will fall under:

![Type selectors & pseudo-elements](https://cdn-images-1.medium.com/max/1600/1*3fcEAOvbgoouX65LM4Vw9w.png)

![Code](https://cdn-images-1.medium.com/max/1600/1*pAojEPXYnLGQkrIGtvr77g.png)

![Class selectors, attribute selectors, pseudo-classes](https://cdn-images-1.medium.com/max/1600/1*8SiGfO31QhkjuOy41WL_2Q.png)

![Code](https://cdn-images-1.medium.com/max/1600/1*j7O7WcSnTK2BTERfiAXSVQ.png)

![ID](https://cdn-images-1.medium.com/max/1600/1*qdmEFsSbzFqya34nqHRdtg.png)

![Code](https://cdn-images-1.medium.com/max/1600/1*wUbUZq1sumjWOj6x7dv3RQ.png)

![Rules](https://cdn-images-1.medium.com/max/1600/1*e02o8uV7c8rre1XZHuTPhw.png)

- Universal selectors, combinators (>, +, etc.) and negation pseudo-classes `:not()` have no affect on CSS specificity; the styles defined inside of the `:not()` pseudo-class do.
- Inline styles (styles added to an HTML element) always override any styles declared in an external style sheet; these are generally not good practice as it's best to define all of your styles in one place.
- `!important` overrides all other denoted styles; this is bad practice as it can make debugging CSS much more difficult. When two conflicting styles make use of an !important declaration, the most specific style wins.

_You may run into situations when leveraging CSS frameworks, such as Bootstrap, where you can't use CSS specificity to override the native styles. In these instances, using `!important` is not considered bad practice._

![Calculating](https://cdn-images-1.medium.com/max/1600/1*je34B59lXaT5ZClTaDzbJA.png)

You may be asking yourself "Okay, CSS Specificity is great, but how do I actually determine which style is the most specific?" We use a four-category system to give a CSS selector a value. The selector with the most specific value wins.

![Chart](https://cdn-images-1.medium.com/max/1600/1*Z5vDhOz-hSY7GK1UI4RE5A.png)

![Example](https://cdn-images-1.medium.com/max/1600/1*mfGyZ8K24zOGaLgvgYq-AA.png)

![Examples](https://cdn-images-1.medium.com/max/1600/1*vS4ty0x2LCUhAOyDmrryHQ.png)

# Example 1

Let's say we have an unordered list with three list items. The list and all of its list items have class names. And let's say we've created two different CSS selectors. Which one is more specific?

![Code](https://cdn-images-1.medium.com/max/2000/1*3xDCCgiQb6jNvOtvs_9Rqg.png)

Let's break down the calculation of the CSS specificity.

![Example](https://cdn-images-1.medium.com/max/2000/1*xIL2Rl0H7nwaSx4R6X9FYA.png)

# Example 2

Let's suppose you're styling a navigation element, which has the following HTML structure.

![Example](https://cdn-images-1.medium.com/max/2000/1*MqIWzYiRK0O-5zatZnK_Cg.png)

We have two styles which change the color of the navigation list items on hover. We want to change the color of list item two on hover to pink. But it's not working as expected.

![Example](https://cdn-images-1.medium.com/max/1600/1*uPQcVWlyEF_1LfckIj4doA.png)

In order to change the color of list item two on hover to pink, we need to have a more specific CSS selector. Thus, prepending the selector with `nav#nav` or even just `#nav` will do the job.

# Example 3
Let's calculate the CSS Specificity of the code snippet below.

![Example](https://cdn-images-1.medium.com/max/1600/1*YYtYYBKyPobEmUJ3firhiw.png)

![Conclusion](https://cdn-images-1.medium.com/max/1600/1*BultgiKgEILEOeHVMqq8Pg.png)

CSS Specificity isn't hard, but it's a skill which, when learned, will save you immense amount of pain and suffering.

By learning these key specificity rules, you will set yourself up to write organized and optimized CSS code.

Check out the resources below for some more CSS Specificity fun!

[Specifishity](https://medium.com/r/?url=https%3A%2F%2Fspecifishity.com%2F)

[Specificity Calculator](https://medium.com/r/?url=https%3A%2F%2Fspecificity.keegan.st%2F)

[CSS Tricks: Specificity](https://medium.com/r/?url=https%3A%2F%2Fcss-tricks.com%2Fspecifics-on-css-specificity%2F)

[Specificity Star Wars](https://medium.com/r/?url=https%3A%2F%2Fstuffandnonsense.co.uk%2Farchives%2Fcss_specificity_wars.html)

[CSS Specificity](https://medium.com/r/?url=http%3A%2F%2Fcssspecificity.com%2F)