Sub topic 10
How Pseudo Selectors Affect Specificity
This chapter assumes you know CSS rule syntax, type, class, ID and attribute selectors, and the cascade's basic idea that a more specific selector beats a less specific one. Here you will learn how pseudo-classes and pseudo-elements add to a selector's specificity, and how :where(), :is(), :not() and :has() change the count.
The three-part score
Specificity is written as three numbers, (IDs, classes, types). The browser compares them from left to right. A higher number in an earlier column wins, no matter what the later columns hold.
| Selector part | Column | Example |
|---|---|---|
| ID selector | first | `#nav` |
| Class, attribute, pseudo-class | second | `.card`, `[type="text"]`, `:hover` |
| Type, pseudo-element | third | `a`, `::before` |
A pseudo-class counts like a class. A pseudo-element counts like a type selector. This is why the single-colon and double-colon forms matter for scoring as well as for syntax.
Counting examples
a:hover {} /* (0,1,1): one pseudo-class, one type */
a.external:hover {} /* (0,2,1) */
p::first-line {} /* (0,0,2): one type, one pseudo-element */
li:nth-child(2)::before {} /* (0,1,2) */
#menu a:focus {} /* (1,1,1) */The :hover in a:hover adds a full class-level point. So a:hover beats a plain .link rule, because (0,1,1) is higher than (0,1,0).
.link {
color: navy;
}
a:hover {
color: crimson; /* wins on hover: (0,1,1) > (0,1,0) */
}Functional pseudo-classes
Pseudo-classes that take a selector list do not all follow the plain "counts as one class" rule.
:is()and:not()take the specificity of their most specific argument. The pseudo-class itself adds nothing.:has()also takes the specificity of its most specific argument.:where()always counts as zero, whatever is inside it.:nth-child(An+B of S)counts as one pseudo-class plus the most specific selector inS. Without theof Spart, it counts as one pseudo-class.
:is(#main, .page) p {} /* (1,0,1): the ID is the most specific argument */
:where(#main, .page) p {} /* (0,0,1): :where() contributes nothing */
a:not(.active) {} /* (0,1,1): .active counts, :not() does not */
.card:has(img) {} /* (0,2,0): .card plus the type argument img is (0,0,1), so (0,1,1) */Check the last line before relying on it. :has(img) takes the specificity of img, which is (0,0,1). The total is .card (0,1,0) plus (0,0,1), which is (0,1,1). The comment's first figure was wrong, so always add the parts step by step.
A common error
:is() can raise specificity by accident. If one argument is an ID, the whole selector is scored as if that ID were always present, even when the element matched a different argument.
:is(#hero, .banner) a {
color: gray; /* (1,0,1) */
}
.banner .link {
color: teal; /* (0,2,0): loses, even on a .banner element */
}Wrapping the list in :where() fixes this, because its score stays at zero.
Reading a conflict
Exercise
What is the specificity of ul li:first-child::marker, and what is the specificity of :where(ul li:first-child)::marker?
Show answer
The first is (0,1,3): two type selectors (ul, li), one pseudo-class (:first-child) and one pseudo-element (::marker, counted as a type). The second is (0,0,1): :where() contributes zero, leaving only the ::marker pseudo-element.
Exercise
A rule :is(.a, #b) p { color: red; } appears before .a p.note { color: blue; }. A <p class="note"> sits inside an element with class a and no ID. Which color applies, and why?
Show answer
Red. :is(.a, #b) takes the specificity of its most specific argument, #b, even though the element matched .a. The rule scores (1,0,1), which beats (0,2,1) for the blue rule.