SyntaxStudy
Sign Up
CSS CSS Specificity & Cascade
CSS Beginner 1 min read

CSS Specificity & Cascade

CSS Specificity & the Cascade

When multiple CSS rules target the same element, the browser must decide which one wins. This is governed by specificity and the cascade.

Specificity Score (highest wins)

  1. !important — Overrides everything (use sparingly)
  2. Inline styles (style="...")
  3. ID selectors (#id)
  4. Class / pseudo-class / attribute selectors (.class, :hover)
  5. Element selectors (p, div)

The Cascade Rules

When specificity is equal, the last rule in the stylesheet wins (order matters).

💡 Avoid !important in most cases — it makes debugging very hard.
Example
/* Specificity: 0-0-1 (element) */
p { color: gray; }

/* Specificity: 0-1-0 (class) — wins over element */
.intro { color: blue; }

/* Specificity: 1-0-0 (id) — wins over class */
#main { color: red; }

/* !important overrides everything */
p { color: green !important; }