Sub topic 2
A quick specificity review
This chapter assumes you can read type, class, ID and attribute selectors, and that you know the combinators and comma-separated selector lists. It reviews how specificity decides between conflicting rules. The later chapters on :is() and :where() depend on it.
Why specificity matters here
When two rules set the same property on the same element, the browser must pick one. Specificity is the main tiebreaker. :is() and :where() differ from each other only in how they affect specificity, so you need a clear model of it first.
The three-part score
Specificity is a score with three components, written (a,b,c):
| Component | Counts | Examples |
|---|---|---|
| a | ID selectors | `#main` |
| b | Class selectors, attribute selectors, pseudo-classes | `.card`, `[href]`, `:hover` |
| c | Type selectors, pseudo-elements | `p`, `a`, `::after` |
The universal selector * and combinators (>, +, ~, the descendant space) add nothing.
a /* (0,0,1) */
ul li a /* (0,0,3) */
.menu > .item:hover /* (0,3,0) */
a[href]::after /* (0,1,2) */
#nav a.active /* (1,1,1) */
* /* (0,0,0) */Each component is compared on its own, from left to right. A larger a always wins, whatever b and c are. Eleven class selectors, (0,11,0), never beat one ID, (1,0,0). Don't read the triple as a decimal number.
Comparing two selectors
Compare the a values first. If they are equal, compare b. If those are equal too, compare c.
p.intro {
color: green; /* (0,1,1) */
}
#page p {
color: red; /* (1,0,1) wins: its a value is larger */
}Both rules match <p class="intro"> inside #page. The paragraph is red, even though p.intro appears first and has a class.
What decides before and after specificity
Specificity is one step in the cascade. The browser checks these in order and stops at the first one that picks a winner.
Two consequences are worth remembering:
!importantand inlinestyleattributes sit outside the(a,b,c)score. They are not a fourth number that you can match with a longer selector.- Source order matters only when specificity is exactly equal.
.btn {
background: gray;
}
.btn {
background: blue; /* equal specificity, later rule wins */
}Common errors
- Counting the combinator.
ul > liis(0,0,2). The>adds nothing. - Forgetting that pseudo-classes count as classes.
a:hoveris(0,1,1), so it beats a plain.linkrule only ifbties or exceeds it. Here.linkis(0,1,0), soa:hoverwins onc. - Trying to win with order. If a rule is ignored, check its specificity before moving it around in the file.
- Escalating with IDs. A single ID raises
aand forces later overrides to use IDs as well. This is the usual cause of "specificity wars".
Where this leads
Selector lists with commas score each selector separately, so a rule like h1, #title has a different specificity depending on which element matched. :is() and :where() work differently. They put the list inside one selector, so the browser must decide how the whole selector scores. The next chapters give the rules for that.
Exercise
Which rule wins for <a class="nav-link" href="/"> inside <nav id="top">, and why? Rule 1: nav a { color: red; }. Rule 2: .nav-link[href] { color: blue; }. Rule 3: #top a { color: green; }
Show answer
Rule 3 wins and the link is green. The scores are Rule 1 (0,0,2), Rule 2 (0,2,0) and Rule 3 (1,0,1). Rule 3 has the largest a component, so source order and the other components do not matter.
Exercise
Write the specificity of main .card > h2:first-child::before.
Show answer
(0,2,3). The class .card and the pseudo-class :first-child give two in the middle. The types main and h2 and the pseudo-element ::before give three at the end. The > combinator adds nothing.