CSSCSS · Lesson 03

Cascade, Specificity & Inheritance

Why one rule wins over another.

Video tutorialTrack course · freeCodeCamp
Speed
Next lesson
Watch 00:00 · 1 checkpoints Watch on YouTube More on this topic
Transcript & captions
9/9

When two rules target the same element, the winner is decided by specificity, then by source order. Specificity is counted as (inline, id, class/attribute/pseudo-class, element).

  • Inline style = 1,0,0,0
  • #id = 0,1,0,0
  • .class, [attr], :hover = 0,0,1,0
  • div, p, ::before = 0,0,0,1
  • !important overrides everything — use it as a last resort.
Try it yourself

Knowledge check

0/1 answered

Which selector has the highest specificity?