Skip to content
BENIBIX Learn

CSS Specificity

Development🌐 Web DevelopmentBrowser Code

Also known as Specificity, Selector Specificity, CSS Specificity Rules

The score that picks which CSS rule wins. It counts when two rules hit one part.

Think of it like

It is like a game of cards where a king beats any number of jacks.

Example

Emma sets .note to blue, but the text stays red. A rule with #intro .note scores higher, so it wins until she changes it.

Why it matters

It explains the most common CSS puzzle: a style you wrote that does nothing. Low scores keep styles easy to change.

Definition

CSS specificity is the score a browser uses to choose between two rules that style the same thing. Each selector gets three counts, written ID-CLASS-TYPE. An ID adds 1-0-0, a class adds 0-1-0, and a tag name adds 0-0-1. The browser compares from the left, so one ID beats any number of classes. If two scores tie, the rule written last wins.

Example code

css
p { color: black; }        /* 0-0-1 */
.note { color: blue; }     /* 0-1-0 */
#intro p { color: red; }   /* 1-0-1, wins */
Three rules hit the same paragraph. The one with an ID has the top score, so the text is red.
Scores compare from the left. The ID column decides first.

Common mistake

Adding !important to win a fight. It beats the score but starts a new fight that only another !important can win.

Key takeaways

  1. 1
    Three counts. IDs, then classes, then tags.
  2. 2
    Left column first. One ID beats any number of classes.
  3. 3
    Ties go last. The later rule wins a tie.

Learn it properly