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:
- Origin and importance — normal author styles beat the browser's UA sheet and user styles;
!importantinverts the ranking, so user!importantand UA!importantbeat your!important. - Cascade layer —
@layernames, shipped March 2022 across engines (Chrome 99, Firefox 97, Safari 15.4). Declarations in an earlier layer lose to later layers regardless of specificity. - 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. - 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) h2contributes nothing to the tuple — specificity (0,0,1) — so resets written with it can always be overridden by a bare selector.@layerputs whole stylesheets in a fixed order:@layer reset, vendor, components;— yourcomponentsbeatvendorno 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?
Keep reading
Related articles from the SudoSecurity blog.
Building a Modern App With React, Vite, and Tailwind CSS: The Complete 2025 Guide
React for the UI, Vite for the build tooling, Tailwind for the styles. This is the stack most new frontend projects start with in 2025. This guide wires all three together from scratch and builds a real application structure you can actually start a production project from.
What is Tailwind CSS? The Utility-First CSS Framework Developers Either Love or Hate
Tailwind is a utility-first CSS framework. You style elements by stacking single-purpose class names directly in your markup. Half the dev community hated this idea, then started using it. Here's why it works and what you need to know.
What is Vite? The Build Tool That Made Frontend Development Fast Again
Vite replaced Webpack for a lot of teams without much fanfare. Native ES modules, a dev server that starts in milliseconds, and a production build that doesn't require understanding 300 lines of config. Here's how it works and why the switch is worth it.
What is React? The JavaScript Library That Changed Frontend Development
React is a JavaScript library for building user interfaces. Facebook built it in 2013, open-sourced it, and the entire frontend ecosystem reorganized itself around it. Here is how the component model works, why it took over, and what you need to know before writing a single line.