Skip to content

Lesson 1 of 9 · 20m

How the Cascade Really Works

Origin, layer, specificity, order — the actual algorithm the browser runs, in the order it runs it, and why your mental model of 'CSS is unpredictable' is a mental model of nothing.

Twenty years in, the single most valuable thing I can tell a new hire about CSS: it is not random. It feels random when you're tweezing !important at 2am because a button won't turn red, but the cascade is a deterministic algorithm with a fixed priority order, and every professional CSS bug is a case of not knowing which tier was actually deciding the outcome.

The decision order, top to bottom

When two declarations compete for the same property on the same element, the browser resolves them in this order — first winner takes it, later tiers never matter:

  1. Origin and importance — !important user-agent styles beat !important user styles beat !important author styles beat normal author styles beat normal user styles beat normal UA styles. Yes, !important inverts the author/user relationship. This surprises everyone once.
  2. Cascade layer — declarations in an earlier @layer lose to declarations in a later one, regardless of specificity.
  3. Specificity — the (id, class, type) tuple, compared element by element.
  4. Order of appearance — last one wins, but only if everything above tied.
#nav .item a { color: navy; }   /* specificity (1,1,1) */
.menu-link    { color: teal; }  /* specificity (0,1,0) — loses */

The ID in the first selector isn't "a bit stronger" — it's in a completely different weight class. One hundred classes cannot beat one ID, because specificity compares column by column, not by sum.

The trap: specificity escalation

Every codebase that "needs !important" got there the same way. Someone wrote #sidebar .widget h3, someone else needed to override it and added #main #sidebar .widget h3, and five years later new developers grep for how many IDs they need to chain to change a font size. Specificity is a ratchet: every escalation makes the next override worse.

The reset you should actually install

/* in your base layer — low specificity by design */
*, *::before, *::after { box-sizing: border-box; }
h1, h2, h3, h4, p { margin: 0; }
img { max-width: 100%; display: block; }
button, input { font: inherit; }

box-sizing: border-box alone eliminates an entire category of layout math bugs. font: inherit on form controls fixes the UA stylesheet silently changing your typography inside inputs — a bug that looks like "the input font is wrong" and is really "nobody told the input."

Field notes

  • Open DevTools' Styles pane and read it bottom to top — the struck-through rules tell you exactly which declaration won and which cascade tier decided it.
  • !important on a utility (.hidden { display: none !important }) is legitimate: a utility that can lose is a utility that lies. !important to win a fight you created is debt.
  • The :is(), :where(), and :not() pseudo-classes have different specificity rules: :where() is always zero — it's the escape hatch for writing overridable defaults.

Was this lesson helpful?

Sign in to rate