Specificity — CSS

When two CSS rules set the same thing on the same element, the rule with the more specific selector wins. This way of picking the winner is called…

Your paragraph is <p class="text">. The CSS already has .text { color: green; }, and at the very bottom you write p { color: blue; }. You reload the page, and the paragraph is still green. Why does the rule written last lose?

When two CSS rules set the same thing on the same element, the browser has to pick one. It checks which selector is stronger: the more specific selector wins. This way of comparing selectors is called specificity.

Three rules, one paragraph

This example has one paragraph and three rules that all apply to that paragraph:

<p id="headline" class="text">Color me red!</p>

<style>
p { color: blue; }
.text { color: green; }
#headline { color: red; }
</style>

The result: the text is red, because #headline is the strongest. From weakest to strongest: tag, then class, then id.

The order you write rules in only matters when two selectors have exactly the same strength: then the rule written later wins. When the strength is different, the stronger selector wins, wherever its rule is written. That also explains the green paragraph at the start of this lesson: .text is stronger than p, so .text wins even though p is written later.

Count the score like a medal table

Think of an Olympic medal table. A country with 1 gold stays above a country with 0 gold and 10 silver, because gold is compared first. Silver is only compared when the number of golds is the same.

A selector's score is also three numbers, written (id, class, tag). An id is the gold, a class is the silver, and a tag is the bronze. Count how many ids, classes and tags the selector has:

Of the last two selectors, #header .title wins: it has one gold (an id), and the other selector has no gold. Two more kinds of selector count as classes: attribute selectors like [type] (it selects elements that have a type attribute), and pseudo-classes like :hover (covered in the next lesson).

The strongest: inline style

CSS can also be written directly on an element's tag, in its style attribute. This is called inline CSS, or an inline style. An inline style beats every selector inside <style>, including an id:

<p id="text" style="color: green;">Color me green!</p>

The style attribute goes inside the opening tag, next to the other attributes. This paragraph stays green even if a rule for its id in <style> sets another colour. So from strongest to weakest: inline style, id, class, tag.

⚠️ When your selector keeps losing

TL;DR: When two CSS rules set the same thing on the same element, the rule with the more specific selector wins. This way of picking the winner is called specificity. Every selector has a score (id, class, tag): the number of ids, classes and tags in it. Two scores are compared from the left, like gold, silver and bronze on a medal table, so one id beats any number of classes. An inline style (CSS in a tag's style="..." attribute) beats every selector. The order you write rules in only matters when the scores are equal: then the rule written later wins.

⚡ The more specific one wins

Write:

p { color: blue; }
.text { color: green; }
#headline { color: red; }
Guess first: all three rules apply to <p id="headline" class="text">. What colour is that paragraph?

Red. #headline (the id selector) is the most specific, because an id belongs to only one element on a page.

Meaning: The more specific selector wins. A tag selector (p) is the weakest, then a class (.text), then an id (#headline). The order you write rules in only matters when their strength is exactly the same.

⚡ Count it like a medal table

Guess first: #header .title against .header .title.main. Which one wins?

#header .title. It has one id, and the other selector only has classes. One id beats three classes.

Meaning: A selector's score is written (id, class, tag): the number of ids, classes and tags in it. #header .title scores (1, 1, 0), and .header .title.main scores (0, 3, 0). Scores are compared from the left, like gold, silver and bronze on a medal table: silver is only compared when the number of golds is the same.

⚡ The strongest: inline style

Write:

<p id="text" style="color: green;">Color me green!</p>

Meaning: CSS written in a tag's style attribute is called an inline style. It beats every selector inside <style>, including an id. From strongest to weakest: inline style, id, class, tag.

When an exercise says to leave the existing CSS as it is, don't edit the old rule. Beat that rule with a stronger selector, or with an inline style.

What you will learn