Lesson 49 +10 XP

The Cascade

The Cascade

The "C" of CSS. When many rules target the same spot, this decides the winner.

The three layers of the algorithm

  1. Origin & importance, !important, inline vs external vs user.
  2. Specificity, id/class/element weights.
  3. Order, later in the file wins (when equal).

Specificity quick math

Selector typePoints
id (#x)100
class .x / attribute [:x] / pseudo :x10
element p / pseudo ::x1

Highest total wins.

The !important keyword

!important upgrades a declaration and jumps it above other specificity. Use sparingly!

p {
  color: red !important;
}

Order matters

p { color: blue; }
p { color: green; }   /* this wins at equal specificity */

Inheritance

Some styles pass to children (color, font), others don't (margin, border). Override with:

  • inherit, initial, unset, revert.

TL;DR

  • Cascade = origin+importance → specificity → order.
  • ID(100)>Class(10)>Element(1).
  • Equal = later wins.
  • !important overrides (use sparingly).
  • Some properties inherit; control with keywords.