Skip to content
Kuza Ndoto Academy

Specificity & the Cascade

Beginner → IntermediateChapter 4 · Page 15 of 225 min readUpdated 15 Nov 2026

When two rules set the same property on the same element, only one can win. The cascade is the algorithm the browser uses to decide, and specificity is its best-known step. Understanding both turns "why won't my CSS apply?" from a mystery into a quick fix.

How the cascade decides

For each property on each element, the browser collects every declaration that applies and compares them in this order, stopping as soon as one step produces a winner:

  1. Origin and importance. Declarations marked !important beat normal ones. (Styles also come from the browser's defaults and user settings; your author styles beat browser defaults.)
  2. Inline styles. A declaration in an element's style attribute beats any selector in a stylesheet.
  3. Cascade layers. If you use @layer, later layers beat earlier ones, and styles not in any layer beat all layered styles.
  4. Specificity. The more specific selector wins.
  5. Order of appearance. If everything else is equal, the declaration that comes last wins.

Most day-to-day conflicts are settled by the last two steps.

Calculating specificity

Specificity is like a scoreboard with columns. Count the parts of a selector and put them in the right column:

  • Inline: a style attribute (written first so it outranks everything).
  • IDs: each #id.
  • Classes: each .class, attribute selector ([type="text"]) and pseudo-class (:hover, :nth-child()).
  • Elements: each type selector (p, nav) and pseudo-element (::before).

Compare scores column by column from the left, like comparing football league positions by points first and goal difference only if points are tied. One ID beats any number of classes, and one class beats any number of elements. The universal selector * and combinators (space, >, +, ~) add nothing.

Selector Inline IDs Classes Elements style="" #id . [ ] : el :: p 0 0 0 1 .card p 0 0 1 1 nav a:hover 0 0 1 2 #nav .menu li a 0 1 1 2 #hero .btn:hover span 0 1 2 1 style="color: red" 1 0 0 0 weaker stronger
Figure 1. Read each score from the left. #hero .btn:hover span scores 0,1,2,1 and beats #nav .menu li a (0,1,1,2) because they tie on IDs and the first has more classes; the element column is never reached.

A worked conflict

<nav id="main-nav">
  <a class="link" href="#">Home</a>
</nav>
#main-nav a { color: white; }   /* 0,1,0,1 */
.link       { color: orange; }  /* 0,0,1,0 */
nav a.link  { color: green; }   /* 0,0,1,2 */

The link is white. The ID rule scores 1 in the ID column, which no number of classes or elements can beat, even though it comes first in the file.

Special cases

  • :is(), :not() and :has() add nothing themselves but take the specificity of their most specific argument: :is(#a, .b) counts as an ID.
  • :where() always has zero specificity, which makes it ideal for default styles that are easy to override.
  • Inherited values have no specificity at all. If body is set to green but a rule targets p directly with blue, the paragraph is blue, however specific the body rule was.

!important

Adding !important after a value lifts that declaration above all normal declarations, including inline styles. If two important declarations clash, the usual steps (layers, specificity, order) decide between them.

.hidden {
  display: none !important;
}
Warning

Reaching for !important to fix a stubborn rule starts an arms race: the next fix needs another !important, and soon nothing can be overridden cleanly. Reserve it for small utility classes like the one above, or for overriding third-party styles you can't edit. Usually the real fix is to lower the specificity of the other rule.

Cascade layers in brief

@layer lets you group styles into named layers and decide their priority up front, whatever their specificity. The order is set by the first statement:

@layer reset, base, components;

@layer components {
  .btn { background: teal; }
}
@layer base {
  #app button { background: grey; }   /* higher specificity, but earlier layer */
}

Buttons with class btn are teal, because components was declared after base and layer order is checked before specificity. Layers are especially useful for keeping a framework or reset in a low-priority layer. One twist: for !important declarations the layer order flips, so important styles in earlier layers win.

Try it

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Specificity</title>
  <style>
    body { font-family: system-ui, sans-serif; margin: 1.5rem; color: green; }
    p { color: black; }                  /* 0,0,0,1 */
    .note { color: purple; }             /* 0,0,1,0 */
    #special { color: crimson; }         /* 0,1,0,0 */
    p.note { color: darkorange; }        /* 0,0,1,1 */
    .note { color: teal; }               /* 0,0,1,0, later */
  </style>
</head>
<body>
  <p>1. A plain paragraph.</p>
  <p class="note">2. A paragraph with class note.</p>
  <p class="note" id="special">3. Class note and id special.</p>
  <p class="note" style="color: blue">4. Class note plus an inline style.</p>
  <div>5. Text in a div (inherits from body).</div>
</body>
</html>

Line 1 is black: the p rule targets it directly, so it beats the inherited green. Line 2 is dark orange: p.note (0,0,1,1) beats both .note rules (0,0,1,0), even the later teal one. Line 3 is crimson, because the ID wins. Line 4 is blue, because inline styles beat selectors. Line 5 is green, inherited from body since no rule targets the div. Try deleting p.note and line 2 turns teal, the last of the two equal .note rules.

Key points

  • The cascade checks importance, inline styles, layers, specificity and finally source order.
  • Specificity columns: inline, IDs, classes/attributes/pseudo-classes, elements/pseudo-elements; compare from the left.
  • Inherited values lose to any rule that targets the element directly.
  • Keep specificity low (prefer classes) and use !important rarely.
Up next · Selectors, Cascade & EffectsTransitions & AnimationsSmooth changes and keyframe animations.
→

Tip: use the ← and → keys to move between pages.