Sub topic 2
A quick specificity review
This chapter assumes you can read and write type, class, ID and attribute selectors, and that you know the descendant, child and sibling combinators. It also assumes you have used comma-separated selector lists and know that CSS resolves conflicting declarations through the cascade. Nothing here is new material. It is a refresher on the one rule that explains the difference between :is() and :where().
The two pseudo-classes match exactly the same elements. They differ only in the specificity they add to a selector. To predict that difference, you need to be able to calculate specificity by hand.
Where specificity fits in the cascade
When two declarations target the same property on the same element, the browser picks one winner. A simplified order of decision looks like this:
- Origin and importance.
!importantdeclarations beat normal ones, and author styles beat browser defaults. - Cascade layers, if the stylesheet uses
@layer. - Specificity, a score computed from the selector.
- Order of appearance. If everything above is tied, the declaration that comes last wins.
Specificity only matters between declarations that survive the earlier steps. A selector with low specificity still wins over a high-specificity one if the cascade settles the matter at an earlier step. The layers_comparison chapter later in this tutorial returns to this point.
The three columns
Specificity is written as three numbers, (a,b,c). The columns are ordered from strongest to weakest.
| Column | What counts | Examples |
|---|---|---|
| a | ID selectors | `#main` |
| b | Class selectors, attribute selectors, and most pseudo-classes | `.note`, `[type="text"]`, `:hover`, `:first-child` |
| c | Type selectors and pseudo-elements | `p`, `a`, `::before`, `::marker` |
Some things add nothing. The universal selector * counts zero. So do the combinators ( , >, +, ~). Combinators change what the selector matches, but not how strongly it competes.
Here are some selectors and their scores.
| Selector | Specificity | Why |
|---|---|---|
| `p` | (0,0,1) | one type selector |
| `.note` | (0,1,0) | one class |
| `#main` | (1,0,0) | one ID |
| `a:hover` | (0,1,1) | one type, one pseudo-class |
| `input[type="text"]` | (0,1,1) | one type, one attribute |
| `nav > a.active` | (0,1,2) | two types, one class, the combinator counts nothing |
| `#nav .item:first-child` | (1,2,0) | one ID, one class, one pseudo-class |
| `ul li::marker` | (0,0,3) | two types, one pseudo-element |
| `*` | (0,0,0) | the universal selector counts nothing |
Pseudo-classes such as :hover and :first-child count in column b. Pseudo-elements such as ::before count in column c. Pseudo-classes that take a selector list as an argument are a special case, and the next chapters cover them. For now, remember that they do not follow the simple "one pseudo-class equals one class" rule.
Comparing scores
Compare two scores column by column, starting from the left. The first column that differs decides the result. The remaining columns are not looked at.
This means the columns are not digits in a single number. A single ID beats any number of classes, because column a is compared before column b.
/* (1,0,0) */
#main {
color: blue;
}
/* (0,12,0) */
.a.b.c.d.e.f.g.h.i.j.k.l {
color: red;
}An element that matches both rules is blue. The ID rule has a larger value in column a, so the comparison stops there.
A larger count in a lower column never makes up for a smaller count in a higher column. The same applies between columns b and c: .note (0,1,0) beats html body main section article div p (0,0,7).
A worked example
Take this markup.
<main id="main">
<p class="note intro">Hello</p>
</main>And these rules, all of which match the paragraph.
p {
color: black;
}
.note {
color: green;
}
main p {
color: red;
}
.note.intro {
color: purple;
}
#main p {
color: blue;
}The scores are:
| Rule | Specificity |
|---|---|
| `p` | (0,0,1) |
| `.note` | (0,1,0) |
| `main p` | (0,0,2) |
| `.note.intro` | (0,2,0) |
| `#main p` | (1,0,1) |
The paragraph is blue. The #main p rule has the only non-zero value in column a, so it wins regardless of where it sits in the file. If you moved it to the top of the stylesheet, the result would stay the same.
Ties and source order
When two rules have identical specificity, the later one wins.
.card a {
color: navy;
}
.menu a {
color: darkgreen;
}Both selectors score (0,1,1). A link inside an element that matches both .card and .menu is dark green, because .menu a appears later. Reordering the two rules flips the result.
Source order is also the reason low specificity is valuable. A rule with low specificity is easy to override from anywhere later in the file. A rule with high specificity forces every later override to be equally specific or to use !important, which makes stylesheets harder to maintain.
Selector lists score each item separately
A comma-separated selector list is shorthand for several independent rules. Each selector in the list keeps its own specificity.
h1,
.title,
#logo {
margin: 0;
}This is equivalent to three separate rules.
h1 {
margin: 0;
}
.title {
margin: 0;
}
#logo {
margin: 0;
}An <h1> that is matched through the first item gets (0,0,1) for that declaration. The #logo item in the same list does not raise its score. An element that matches #logo gets (1,0,0), but only for itself.
This behavior is a sharp contrast with :is(), which merges its arguments into one compound selector. The next chapters explain how :is() and :where() handle the score of their arguments, and why that differs from a plain list.
Inline styles and `!important`
Two things sit outside the three columns.
- Declarations in a
styleattribute beat any selector-based declaration of the same importance. You can think of them as a column to the left of column a. !importantmoves a declaration to a higher-priority group in the cascade. Among!importantdeclarations, the usual comparison of specificity and order applies again.
p {
color: green !important;
}
#main p.note {
color: red;
}The paragraph is green even though the second rule has much higher specificity. The comparison happens inside the !important group first, and the second rule is not in that group.
Reaching for !important is often a sign that a specificity fight has gone wrong. Tools that keep default specificity low, such as :where(), are a way to avoid that fight.
Common errors
- Counting combinators.
nav > ais (0,0,2). The>adds nothing. - Treating the columns as digits. Eleven classes are not 11 in column a. Ten or more classes never reach an ID.
- Assuming a later rule always wins. Source order only decides ties.
- Forgetting that attribute selectors count as classes.
[type="text"]is (0,1,0), the same as.field. - Expecting a selector list to share a score. Each item competes on its own.
- Counting pseudo-elements as pseudo-classes.
p::first-lineis (0,0,2), whilep:first-childis (0,1,1).
What to carry forward
For the rest of this tutorial, you need these facts:
- Specificity is a triple
(a,b,c)compared from left to right. - IDs go in a, classes, attributes and pseudo-classes go in b, types and pseudo-elements go in c.
*and combinators add zero.- Equal specificity falls back to source order.
- Items in a plain selector list are scored independently.
Exercise
Calculate the specificity of each selector, then say which declaration wins for a link that matches all three: a.external:hover , nav a, #footer .links a.
Show answer
a.external:hover is (0,2,1): one class, one pseudo-class, one type. nav a is (0,0,2): two types. #footer .links a is (1,1,1): one ID, one class, one type. The third selector wins because column a, where it has 1 against 0 for the others, is compared first.
Exercise
A stylesheet contains h1, #logo, .title { color: teal; } and later h1 { color: orange; }. An element <h1 class="title"> matches both. What color is it, and why?
Show answer
Orange. The element matches the list through its h1 item (0,0,1) and its .title item (0,1,0). The list rule applies at the best-scoring matching item, which is .title at (0,1,0). The later h1 rule is only (0,0,1), so the .title item actually wins and the heading is teal. The key point is that #logo does not take part, because the element is not #logo. The score comes from the highest-scoring item that matches the element, never from the items that do not match.