Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 139 ยท The Cascade

Specificity

Learn how browsers score CSS selectors with the (a, b, c) specificity model, compare selectors with a live calculator, and keep specificity low with :where().

When two rules in the same origin and layer set the same property on the same element, the browser does not simply take the last one. First it asks: which selector is more specific? A selector that names an exact ID is treated as more targeted than one that names a class, which is more targeted than one that only names an element type. That ranking is called specificity.

Specificity is the step of the cascade that causes the most confusion, mostly because people try to guess it. You don't have to guess. In this lesson you will learn to calculate it exactly, try it with a live calculator, and pick up the habits that keep it from becoming a problem.

Three columns: (a, b, c)

Every selector gets a score made of three numbers, usually written (a, b, c):

Column What it counts Examples
a ID selectors #cart, #main-nav
b Class selectors, attribute selectors and pseudo-classes .card, [type="email"], :hover, :nth-child(2)
c Type selectors and pseudo-elements p, li, ::before, ::marker

Some parts of a selector count for nothing:

  • The universal selector *.
  • Combinators: the space, >, + and ~.
  • :where(), together with everything inside it.

Here are a few selectors worked out by hand:

Selector IDs Classes etc. Types etc. Specificity
li 0 0 1 (0, 0, 1)
ul li 0 0 2 (0, 0, 2)
.menu li 0 1 1 (0, 1, 1)
.menu a:hover 0 2 1 (0, 2, 1)
input[type="checkbox"]:checked 0 2 1 (0, 2, 1)
#sidebar .menu > li 1 1 1 (1, 1, 1)
p::first-line 0 0 2 (0, 0, 2)
* + * 0 0 0 (0, 0, 0)

Comparing scores

To compare two selectors, look at the a column first. The higher number wins, and you are done. Only if the a values are equal do you look at b, and only if those are equal too do you look at c. If all three are equal, specificity is tied and the order of appearance decides.

This is like comparing words in a dictionary, not like adding numbers. The columns never "carry over":

  • #nav is (1, 0, 0). A selector with eleven classes, (0, 11, 0), still loses to it, because 1 beats 0 in the first column.
  • .title is (0, 1, 0). html body main article section h2 is (0, 0, 6), and it still loses to the single class.
Columns never carry over

Remove the #headline rule and the four-class rule takes over. The four-type rule never wins while either of the others exists.

Try the specificity calculator

Type two selectors to see each score and which one wins. Try the presets, then experiment with your own. The calculator handles IDs, classes, attributes, pseudo-classes, pseudo-elements, comma lists and the :is(), :not(), :has() and :where() functions.

Specificity calculator

A few things to notice while you play:

  • In a comma-separated list, such as h1, .title, each selector is scored on its own. An element matched by h1 gets (0, 0, 1) from that rule, not the score of the most specific item in the list. The calculator shows a score for each item and compares the first one of each.
  • :is(#a, .b) p scores (1, 0, 1) even for an element that matched .b. More on this below.
  • li.done.done is legal, and repeating a class really does add to specificity. It is a hack, but you will see it in the wild.

Pseudo-classes that behave differently

Four functional pseudo-classes do not follow the "one pseudo-class = one point in b" rule:

  • :is(), :not() and :has() take the specificity of their most specific argument. :not(.hidden) counts like .hidden: (0, 1, 0). :is(header, #top) counts like #top: (1, 0, 0), even when it matched a plain <header>.
  • :where() always has zero specificity, whatever you put inside it.
  • :nth-child(An+B of S) counts as one pseudo-class plus the most specific selector in S.

The full story of these selectors lives in :is(), :where(), :not() and :has(). For specificity, the main lesson is: :is() can quietly raise a selector's weight, while :where() is a tool to lower it.

Where inline styles and !important fit

Two things sit outside the (a, b, c) score:

  • A style attribute is not a selector, so it has no specificity at all. It beats every selector-based declaration because the cascade checks inline styles before it looks at specificity. People sometimes describe it as a fourth, higher column; the effect is the same.
  • !important is also checked before specificity. Among important declarations, specificity works normally again. See !important.

Keep specificity low

Specificity problems grow over time. Someone writes #content .sidebar ul li a to style a link. Later, someone wants a special link and has to write something even heavier. Eventually !important appears. The way out is to keep selectors as light as possible from the start:

  • Style with classes. A single class, (0, 1, 0), is enough for almost everything and is easy to override with another single class placed later.
  • Avoid IDs in selectors. IDs are perfect for links, labels and JavaScript, but one ID in a selector outweighs any number of classes.
  • Don't mirror the HTML. .card .card-body .card-title adds weight without adding meaning. .card-title is usually enough.
  • Avoid qualifying classes with types. div.alert is (0, 1, 1) and stops working if the element changes. Use .alert.
Low specificity makes variants easy

Every rule here is (0, 1, 0), so source order does the work: variants come after the base and win. If .button had been written as form div button.button, each variant would need a heavier selector of its own.

Using :where() to remove specificity

Sometimes you need a long selector to match the right elements, but you don't want it to be hard to override. Wrap the heavy part in :where(). It still matches the same elements, but it contributes (0, 0, 0).

:where() keeps defaults easy to override

This pattern is popular for resets, base styles and design-system defaults: the library provides sensible styles, and any single class in your own CSS can adjust them. Cascade Layers solve a similar problem at a larger scale.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the specificity of nav .menu > li:hover?

    .menu and :hover are two in column b; nav and li are two in column c. The > combinator counts for nothing.
  2. 2. Which selector wins against #main (1, 0, 0)?

    Columns never carry over, so no number of classes or types beats an ID. :where() contributes zero.
  3. 3. What is the specificity of :is(p, .note, #tip)?

    :is() takes the specificity of its most specific argument, which is #tip.
  4. 4. Two rules have the same specificity, the same origin and the same layer. Which one wins?

    When specificity ties, order of appearance decides, and the later declaration wins.
  5. 5. Why do many design systems wrap their selectors in :where()?

    :where() matches normally but contributes (0, 0, 0), keeping the defaults easy to override.

Summary

  • Specificity is a three-part score: (IDs, classes/attributes/pseudo-classes, types/pseudo-elements).
  • Compare column by column from the left; a higher column always wins and never carries over.
  • *, combinators and :where() add nothing; :is(), :not() and :has() take their most specific argument.
  • Inline styles and !important are decided before specificity is even checked.
  • Keep specificity low with single classes, no IDs in selectors and no unnecessary nesting.
  • Wrap selectors in :where() when you want them to match but stay easy to override.

Next: Inheritance โ€” how some properties flow from parents to their children.