Skip to content

Lesson 6 of 13 · 20m

The Cascade Is the Language

Origin, layers, specificity, source order — the actual sort that decides which declaration wins, and how `@layer` and `:where()` end the `!important` wars.

"Last rule wins" is the mental model everyone starts with, and it's about a quarter of the truth. When two declarations target the same element, the browser runs the cascade: origin first, then cascade layer, then specificity, then source order — and !important doesn't win the fight, it moves it to a bracket you control even less.

The actual sort order

Declarations get sorted through these filters in sequence:

  1. Origin and importance — normal author styles beat the browser's UA sheet and user styles; !important inverts the ranking, so user !important and UA !important beat your !important.
  2. Cascade layer — @layer names, shipped March 2022 across engines (Chrome 99, Firefox 97, Safari 15.4). Declarations in an earlier layer lose to later layers regardless of specificity.
  3. Specificity — the tuple (a, b, c): ID selectors, then class/attribute/pseudo-class selectors, then type/pseudo-element selectors. Compared left to right like a version number — .a.b.c (0,3,0) never beats #a (1,0,0), no matter how many classes you stack.
  4. Source order — the last declaration wins only when everything above ties.

Inline styles sit above selectors entirely — only !important declarations outrank them, which is why JavaScript that writes el.style.color forces CSS authors into !important. If a library mutates inline styles and you must override it, !important is the correct tool. The sin is reaching for it against other selectors.

The specificity war is a process failure

The production smell is an arms race: someone's .card loses to a plugin's #widget .card, so they write #page .card; the next developer can't beat that and goes !important; the third puts !important on !important. Every step makes the stylesheet less editable. The rule that ends it: keep specificity flat — classes only, no IDs in CSS, no !important except single-purpose utility classes where it means "this utility is mandatory."

Two tools exist specifically to break these wars:

  • :where() is :is() with zero specificity. :where(article) h2 contributes nothing to the tuple — specificity (0,0,1) — so resets written with it can always be overridden by a bare selector.
  • @layer puts whole stylesheets in a fixed order: @layer reset, vendor, components; — your components beat vendor no matter how specific the vendor selectors get. Import third-party CSS with @import url(...) layer(vendor) and its specificity stops mattering entirely.

Inheritance is the fifth lever and the quietest: a property with no matching declaration inherits from the parent, but any direct declaration — even color: inherit or a UA default — beats the inherited value. That is why a { color: inherit } inside a dark card fixes links the UA wants blue.

One trap worth memorizing: :is() takes the maximum specificity of its arguments — div:is(#a, .b) scores as an ID. :where() never does.

Where !important legitimately lives

Three places: single-purpose utility classes (.hidden { display: none !important } means "this is mandatory"), overriding inline styles a third-party script injected, and print stylesheets that must defeat whatever the page declared. Everywhere else is an escalation the next developer inherits.

The reversal keywords deserve a mention: revert rolls a declaration back to the previous cascade origin (author back to user, user back to UA), and revert-layer rolls it back to the previous @layer. They are how a component strips a reset's opinion without re-stating the property.

Try it yourself

const score = (ids, classes, types) => `${ids},${classes},${types}`;
console.log("#nav a:        ", score(1, 0, 1));
console.log(".nav a.active: ", score(0, 2, 1));
console.log("Winner: (1,0,1) — an ID always beats stacked classes");

The practice

Flat specificity, @layer for third-party CSS, :where() for resets, !important reserved for utility classes. When something refuses to be overridden, open DevTools — the applied-rules pane shows winning declarations with losers struck through, and hovering a selector prints its exact specificity. The fight you're in is always written down there.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.