Lesson 3 · Web development foundations

The cascade and specificity

Learn exactly which CSS rule wins, and why.

About 20 minutes

Tab indents. Press Escape, then Tab, to leave the editor.

Sooner or later every developer writes a CSS rule that does nothing. The selector is right, the property is right, and the element stubbornly stays the wrong color. The usual reaction is to add !important and move on, which works until the next rule also needs !important. The real fix is to know the algorithm the browser uses to choose between competing rules. It's called the cascade, it's precisely defined, and once you know it, "why isn't my CSS working?" becomes a two-minute investigation.

Why there's a contest at all

On any real site, many rules match the same element. A reset stylesheet sets every link's color, the design system sets a color for links in the navigation, and a component sets a color for the active page. Each of these is a declaration (one property and value, like color: crimson) in a rule whose selector matches the element. For every element and every property, the browser collects all the declarations that apply and picks exactly one winner.

Two consequences trip people up:

  • The contest is per property, not per rule. A rule can lose on color and still win on font-weight, because nothing else set font-weight.
  • Losing declarations aren't errors. The browser silently ignores them, which is why the developer tools (covered below) are essential.

The order the browser checks

The browser compares competing declarations using these criteria, in order. It moves to the next one only when there's a tie.

  1. Origin and importance. Styles come from the browser's defaults (the user agent stylesheet), from the user's own settings, and from the site's authors. Normal author styles beat browser defaults. !important reverses the order, so an important style from the user beats an important style from the site: that's how someone who needs large text can always have it.
  2. Inline styles. A declaration in an element's style="…" attribute beats any selector in a stylesheet.
  3. Cascade layers. Rules inside @layer blocks are ranked by layer order: a later layer beats an earlier one, and styles in no layer beat all layered styles. Teams use layers to make "reset", "components" and "utilities" predictable without specificity tricks.
  4. Specificity. The more specific selector wins. This is the step that causes most surprises, so it has its own section next.
  5. Order of appearance. If everything else ties, the declaration that comes last wins, including across files, in the order they load.

If no declaration matches at all, the property is either inherited from the parent element (text properties like color, font-family and line-height are) or set to its initial value (most box properties, like margin and border, are not inherited).

Specificity, scored

Specificity is a score with three columns, written as (ids, classes, types). Count the parts of the selector:

Part of the selectorCounts asExamples
IDids#site-nav
Class, attribute, pseudo-classclasses.active, [type="email"], :hover
Element type, pseudo-elementtypesa, li, ::before
Universal selector, combinatorsnothing*, >, +, ~, a space

Compare the columns left to right; the first column that differs decides. So #site-nav a is (1, 0, 1) and .nav .link.active is (0, 3, 0). The ID selector wins, because 1 id beats 0 ids no matter how many classes the other one has. There's no carrying over: eleven classes never add up to one ID.

Modern CSS adds a few rules for its newer pseudo-classes. :is(), :not() and :has() count as their most specific argument, so :is(#a, .b) is worth an ID. :where() always counts zero, which makes it perfect for defaults that are easy to override.

Try a few pairs in the calculator. Start with #site-nav a versus .active, then see what it takes for the second to win.

Lab

Specificity calculator

When two rules set the same property on the same element, the more specific selector wins. Compare two.

#nav␣a
1,0,1
Wins
nav␣ul␣li␣a.active:hover
0,2,4

Selector A wins. Specificity is compared column by column from the left: one id beats any number of classes.

  • #id ids
  • .class [attr] :hover classes, attributes, pseudo-classes
  • p ::before types, pseudo-elements
  • :is() :not() :has() count their most specific argument; :where() and * count zero
Question

Quick check

Which selector wins for an <a class="link active"> inside <nav id="site-nav" class="nav">?
Pick an answer.

Inheritance loses to everything

A common mistake is to expect an inherited value to beat a rule. It never does. Inheritance only fills in a property when no declaration matched the element at all.

.card { color: darkgreen; }
a { color: blue; }

A link inside .card is blue. .card is more specific than a, but it targets the card, not the link. The link only inherits from the card if nothing sets the link's color, and a { color: blue; } does.

Question

Quick check

Your CSS is body { color: green; } and * { color: black; }. What color is the text in a <p>?
Pick an answer.

Debugging a losing rule

When a style doesn't apply, open the developer tools, right-click the element and choose Inspect. The Styles panel lists every rule that matches, most important first. Declarations that lost are shown struck through. The Computed panel shows the final value of each property and, when you expand it, which rule it came from.

From there, the fix is usually one of these, from best to worst:

  1. Match the existing specificity with a selector that says what you mean: #site-nav .active rather than .active.
  2. Lower the other rule's specificity, for example by replacing an ID with a class, or wrapping it in :where(), so it stops dominating.
  3. Reorder the rules if they tie.
  4. Use !important only for true overrides, such as a utility class that must always win. Every !important makes the next override harder.

Now fix two real cases.

Challenge

Make the right rules win

Two rules are losing:

  1. The active link should be crimson, but it's gray. (Notice it is bold: the cascade is decided property by property.)
  2. The notice box should have a light yellow background, but it's white.

Fix both by changing selectors or the order of rules. Don't use !important or inline style attributes, and keep the other link gray and the plain box white.

  • The active link is crimson
  • The other links are still gray
  • The notice box is light yellow
  • The plain box is still white
  • You didn't use !important or inline styles

Recap

  • For each element and property, the browser collects every matching declaration and picks one winner; losing declarations are silently ignored.
  • The cascade checks, in order: origin and !important, inline styles, cascade layers, specificity, then order of appearance.
  • Specificity is (ids, classes, types), compared column by column; :where() counts zero and :is(), :not(), :has() count their most specific argument.
  • Inherited values only apply when no declaration matches the element at all.
  • Debug with the Styles and Computed panels, and fix with a clearer selector before ever reaching for !important.

Finished this lesson?

Mark it complete to track your progress on this device.