Specificity and the Cascade
How the browser picks a winner when several rules set the same property on the same element, and how to read and control specificity instead of guessing.
Specificity and the Cascade
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- When rules conflict, the cascade compares them in a fixed order: origin and `!important`, inline styles, cascade layers, specificity, @scope proximity, and finally source order. Later criteria only matter if earlier ones tie.
- Specificity is a three-part count: IDs, then classes/attributes/pseudo-classes, then types/pseudo-elements. Compare column by column from the left, not as one number.
- One ID outranks any number of classes. I ran a selector with over two hundred classes against a single ID, and the ID won.
- `:where()` always contributes zero specificity. `:is()` and `:not()` contribute the highest specificity of their arguments.
- The fix for a specificity fight is almost never `!important`. Lower the specificity of the strong rule, use a layer, or match the specificity and order.
Why there has to be a tiebreaker
Several rules can match one element and set the same property. The page’s stylesheet says links are blue; a component says this one is white; the user-agent stylesheet says underline. The browser cannot apply all of them, because a property has one value. It needs a deterministic way to pick, one that gives the same answer on every machine. That is the cascade: a ladder of comparisons the browser walks down until one side wins.
The ladder
In order, for two competing declarations:
- Origin and importance. Browser defaults, author styles and user styles are ranked, and
!importantflips the order. - Element-attached styles. A
style=""attribute beats rules in stylesheets. - Cascade layers. A rule in a later layer beats an earlier one, regardless of selector. (Next lessons, in cascade layers and scope.)
- Specificity. The more specific selector wins.
- Scope proximity. Under
@scope, the closer scoping root wins. - Order of appearance. The later rule wins.
Each step only runs if all steps above it tied. Most day-to-day conflicts are settled at specificity or source order, so those are the two worth knowing cold.
Counting specificity
Specificity is three counts, written as (A,B,C):
- A: ID selectors (
#nav) - B: class selectors, attribute selectors and pseudo-classes (
.card,[type="text"],:hover) - C: type selectors and pseudo-elements (
a,p,::before)
The universal selector * and combinators count for nothing.
a /* (0,0,1) */
.nav a /* (0,1,1) */
#main .card a:hover /* (1,2,1) */
Compare left to right. A bigger A wins immediately; B only matters if A ties; C only if both tie. These are not digits of one number: eleven classes do not turn into an ID. I tested a rule made of more than two hundred class names against a plain #z { }, and the ID rule won. Browsers do not overflow from one column into the next.
Inline style="" sits above all selector specificity, which is why people reach for it and regret it.
The modern selectors and specificity
:is(a, b) and :not(a, b) take the specificity of their most specific argument. :where(...) is defined as zero, always. That makes it the tool for writing defaults that are easy to override:
/* (0,0,1): any class beats this */
:where(.prose) a { color: royalblue; }
/* (1,0,1): the #main inside :is() counts */
:is(#main, .prose) a { color: royalblue; }
I checked this with .note { color: red } on a paragraph. A rule :where(#main) p lost to it and :is(#main) p beat it. More on the selector side of this in :is, :where and :has.
Debugging instead of guessing
Open DevTools, select the element, and read the Styles panel. Declarations that lost are struck through, and the rules are listed from strongest to weakest. When the rule you wanted is crossed out, compare its selector to the one that beat it, count the triples, and decide which to change. This takes a minute, and it beats stacking another class on the selector.
The !important escape hatch
!important lifts a declaration into a higher tier of the ladder. It works, and it is how you end up with a stylesheet where the only way to override an !important is another one. Two honest uses: utility classes that are meant to always win (.visually-hidden), and overriding third-party CSS you cannot edit. For everything else, change the structure: lower the weight of the strong rule, put the third-party code in an earlier layer, or give your selector enough specificity to win honestly.
A style that keeps conflicts small
Most specificity pain comes from selectors that were heavier than they needed to be. Style with single classes where you can, avoid IDs for styling, and don’t chain #main .sidebar ul li a when .sidebar-link would do. When every selector sits at roughly (0,1,0), source order does the deciding and no one has to count.
What the video covers
Kevin Powell’s clip is “HTML & CSS for Absolute Beginners: Specificity”, about seven minutes, from his beginner course. By its title it introduces specificity at a beginner level; the triple notation and the :where and :is examples here are mine and not a transcript of it. The runner-up is a longer 2018 video that predates :where() and layers, so use it for the scoring rules only.
Where this fits
This is the second half of the picture started in selectors: selectors choose candidates, the cascade picks the winner. Next, inheritance and computed values covers what happens when no rule matches an element at all. There is a briefer pass over the same ideas in the frontend roadmap’s box model and cascade.
Check yourself
Which wins for `<p class="note">`: `p.note { color: red }` or `.note { color: blue }`, written in that order or reversed?
`p.note` wins either way. It scores (0,1,1) against (0,1,0) for `.note`. Order only matters when the specificity ties.
What is the specificity of `#main .card a:hover`, and what is it as a triple?
(1,2,1): one ID (`#main`), two from classes and pseudo-classes (`.card`, `:hover`), one type selector (`a`).
A rule `.btn { color: white }` loses to a library's `nav a { color: gray }` on a link inside a nav. What are two fixes that don't use `!important`?
Raise your selector to at least (0,1,1), e.g. `nav .btn`, since it then beats `nav a` (0,0,2) outright. Or put the library's CSS in an earlier cascade layer so that your unlayered rule wins regardless of selector weight.
`:where(#main) p` versus `:is(#main) p` against a rule `.note { }` on a paragraph. Which one beats `.note`?
`:is(#main) p` does, because `:is()` takes the weight of its most specific argument, so it scores (1,0,1). `:where(#main) p` scores only (0,0,1), so `.note` at (0,1,0) beats it. I confirmed this in Chromium.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.