Sub topic 9
Calculating Specificity
This chapter assumes you can read a CSS rule, you know the type, class, ID and attribute selectors, and you have used combinators such as > and +. Here you will learn how to turn any selector into a score and compare two scores.
What specificity measures
When two rules target the same element and set the same property, the browser needs a tiebreaker. Specificity is a score computed from the selector alone. It is written as a triple of counts: (IDs, classes/attributes/pseudo-classes, types/pseudo-elements).
| Column | What it counts | Examples |
|---|---|---|
| First | ID selectors | `#main` |
| Second | Class, attribute and pseudo-class selectors | `.note`, `[href]`, `:hover` |
| Third | Type selectors and pseudo-elements | `p`, `a`, `::before` |
The universal selector * and the combinators ( , >, +, ~) add nothing.
Counting step by step
- Count the ID selectors.
- Count the class, attribute and pseudo-class selectors.
- Count the type selectors and pseudo-elements.
- Write the three counts as a triple.
p { color: black; } /* (0,0,1) */
.note { color: green; } /* (0,1,0) */
p.note { color: blue; } /* (0,1,1) */
#main p.note { color: purple; } /* (1,1,1) */
ul li.active > a[href] { color: red; } /* (0,2,3) */The last selector has two types in ul li, one more in a, one class in .active and one attribute in [href]. The > adds nothing, so the score is (0,2,3).
Comparing triples
Compare the columns from left to right. The first column that differs decides the winner, and the later columns are ignored after that.
#box { color: blue; } /* (1,0,0) */
.box.a.b.c.d { color: red; } /* (0,5,0) */On an element matching both rules, the text is blue. One ID outranks any number of classes. The columns do not carry over into each other, so ten classes never add up to an ID.
Ties and source order
If the triples are identical, the rule that appears later in the stylesheet wins.
.btn { background: gray; }
.btn { background: teal; }The button is teal.
Functional pseudo-classes
:is() and :not() take the specificity of their most specific argument. :where() always contributes zero.
:is(.a, #b) p { color: blue; } /* (1,0,1) */
:where(.a, #b) p { color: blue; } /* (0,0,1) */
p:not(.hidden) { color: blue; } /* (0,1,1) */:is(.a, #b) counts as an ID even when the element matched through .a. The score depends on the selector as written, not on which branch matched.
Common errors
- Counting combinators or
*as part of the score. - Treating the triple as a decimal number such as 0.1.1. Compare column by column instead.
- Forgetting that pseudo-elements such as
::beforecount in the third column, while pseudo-classes such as:hovercount in the second. - Assuming a selector that is longer or nested more deeply is always stronger. One ID beats any number of classes.
Exercise
Which rule sets the color of <a class="nav" href="/"> inside <nav id="top">: nav a.nav { color: red; } or #top a { color: blue; }?
Show answer
The first rule scores (0,1,2). The second scores (1,0,1). The first column differs, so the second rule wins and the link is blue.