Sub topic 4
Specificity: counting ids, classes and elements
This chapter assumes you can write CSS rules with selectors and declarations, know how HTML classes and ids work, and can inspect an element in browser developer tools. It covers one step of the cascade: how the browser compares selectors when two rules set the same property on the same element.
Why specificity exists
Several rules often match one element and set the same property. After origin and importance, and after cascade layers, the browser compares the specificity of the selectors. The rule with the more specific selector wins. If specificity is equal, source order breaks the tie, which a later chapter covers.
Specificity is a score computed from the selector alone. It does not depend on where the rule sits in the file or on how many times it appears.
Counting the three columns
A selector's specificity is written as three numbers, (a, b, c).
| Column | What it counts | Examples |
|---|---|---|
| a | Id selectors | `#intro` |
| b | Class selectors, attribute selectors, pseudo-classes | `.note`, `[type="text"]`, `:hover` |
| c | Type selectors and pseudo-elements | `p`, `a`, `::before` |
The universal selector * and the combinators (space, >, +, ~) add nothing.
Compare the columns from left to right. A higher a wins no matter what b and c are. Only when a is equal does the browser look at b, and so on. The numbers are not decimal digits. Eleven classes, (0,11,0), still lose to a single id, (1,0,0).
p { color: black; }
.note { color: blue; }
#intro { color: green; }
/* <p id="intro" class="note">Hello</p> renders green:
id (1,0,0) beats class (0,1,0) beats element (0,0,1). */All three rules match the paragraph. The id selector scores (1,0,0), so it wins even though it appears last by coincidence. Moving it to the top of the file would not change the result.
Worked scoring examples
Score each selector by listing its parts.
| Selector | Breakdown | Score |
|---|---|---|
| `a` | one type | (0,0,1) |
| `nav a` | two types | (0,0,2) |
| `.menu a:hover` | class, pseudo-class, type | (0,2,1) |
| `ul.menu li.active` | two classes, two types | (0,2,2) |
| `#nav .item` | id, class | (1,1,0) |
| `input[type="text"]` | attribute, type | (0,1,1) |
| `p::first-line` | type, pseudo-element | (0,0,2) |
The diagram shows the decision the browser makes when comparing two scores.
Functional pseudo-classes
:is() and :not() take the specificity of their most specific argument. :where() always contributes zero.
:is(#a, .b, p)scores (1,0,0), because#ais the largest argument.:not(.hidden)scores (0,1,0). The:not()itself adds nothing, but its argument does.:where(.prose) ascores (0,0,1), because:where(.prose)adds nothing.
:where(.prose) a { color: blue; }
a { color: red; }Both selectors score (0,0,1), so the later rule wins and links are red. This is why :where() suits low-priority default styles that other rules should override easily.
Inline styles and !important
A style attribute on an element beats any selector in a stylesheet, because it sits above the id column. It is not a selector, so it has no (a,b,c) score. !important is not part of specificity either. It moves a declaration into a different, higher-priority group before specificity is compared.
p { color: red !important; }
#intro { color: green; }The paragraph with id intro stays red, even though #intro is more specific.
Common errors
- Reading the score as a number. (0,10,0) is not "ten", and it never beats (1,0,0).
- Counting the element a rule is placed near rather than the selector text. Only the selector counts, and the rule's nesting in the HTML does not.
- Forgetting that pseudo-classes such as
:hovercount as classes, soa:hover(0,1,1) beats.link(0,1,0). - Escalating with ids or
!importantto fix a conflict. That makes later overrides harder. Prefer lowering the specificity of the losing rule's competitor, using:where(), or using cascade layers.
Exercise
Which wins on <a class="link" href="#">: nav .link:hover (when hovered) or #menu a? Give both scores.
Show answer
nav .link:hover scores (0,2,1): one class, one pseudo-class, and two types would be wrong, since nav and the implied element make (0,2,1) only if nav is the sole type. Counting exactly: nav is one type, .link is one class, :hover is one pseudo-class, giving (0,2,1). #menu a scores (1,0,1). The id column is compared first, so #menu a wins, assuming both match the element.
Exercise
Two rules match the same element: :is(.a, #b) p and div.card p. Which has higher specificity?
Show answer
:is(.a, #b) p takes the score of its most specific argument, #b, plus the type p, giving (1,0,1). div.card p scores (0,1,2). The first wins because its id column is higher.