Sub topic 10
How Pseudo Selectors Affect Specificity
This chapter assumes you can read CSS rules and already know type, class, ID, and attribute selectors. It also assumes you know the basic specificity idea: when two rules target the same element and set the same property, the more specific selector wins, and the later rule wins a tie. Here you will learn how pseudo-classes and pseudo-elements fit into that calculation.
The specificity model in one page
Specificity is a three-part score, usually written (A, B, C).
- A counts ID selectors such as
#main. - B counts class selectors (
.card), attribute selectors ([type="text"]), and pseudo-classes (:hover). - C counts type selectors (
p,a) and pseudo-elements (::before).
The universal selector * and combinators ( , >, +, ~) add nothing. Compare the parts left to right. One ID beats any number of classes, and one class beats any number of type selectors. The columns do not carry over, so (0, 1, 0) always beats (0, 0, 50).
Pseudo-classes count like classes
A pseudo-class adds one to column B, the same as a class or an attribute selector. The table shows common cases.
| Selector | Score | Why |
|---|---|---|
| `a` | (0, 0, 1) | One type selector |
| `a:hover` | (0, 1, 1) | Type plus one pseudo-class |
| `a.nav` | (0, 1, 1) | Type plus one class |
| `a.nav:hover` | (0, 2, 1) | Class plus pseudo-class plus type |
| `li:nth-child(2)` | (0, 1, 1) | `:nth-child()` counts as one pseudo-class |
| `input:required:invalid` | (0, 2, 1) | Two pseudo-classes plus type |
| `#nav a:hover` | (1, 1, 1) | ID, pseudo-class, type |
This has a practical effect. a:hover has the same weight as a.nav. If the class rule comes later in the stylesheet, it overrides the hover color, and the link seems to ignore the hover state.
a.nav {
color: #333;
}
a:hover {
color: #c00;
}Here both selectors score (0, 1, 1). The hover rule comes later, so it wins on hover. If you swap the two rules, a.nav wins and the hover color never appears. This is the same ordering problem that makes the link-state order (link, visited, hover, active) matter.
Pseudo-elements count like types
A pseudo-element adds one to column C, the same as a type selector. It does not count in column B.
| Selector | Score | Why |
|---|---|---|
| `p` | (0, 0, 1) | One type selector |
| `p::first-line` | (0, 0, 2) | Type plus one pseudo-element |
| `.card::before` | (0, 1, 1) | Class plus pseudo-element |
| `li::marker` | (0, 0, 2) | Type plus pseudo-element |
| `::selection` | (0, 0, 1) | One pseudo-element |
A pseudo-element is weaker than a pseudo-class. In practice this rarely causes surprises, because a pseudo-element rule usually targets generated content or a part of an element that no other rule can reach. Conflicts mostly show up when two rules both style the same pseudo-element, such as .card::before and section .card::before.
Pseudo-class and pseudo-element together
When a selector holds both, add the scores.
.card:hover::before {
background: #0b57d0;
}This selector has one class, one pseudo-class, and one pseudo-element. The score is (0, 2, 1). It beats .card::before at (0, 1, 1), even if the plain rule comes later.
The logical pseudo-classes break the pattern
The four logical pseudo-classes do not simply add one to column B. Each follows its own rule.
| Pseudo-class | What it contributes |
|---|---|
| `:is(list)` | The specificity of the most specific selector in its list |
| `:not(list)` | The specificity of the most specific selector in its list |
| `:has(list)` | The specificity of the most specific selector in its list |
| `:where(list)` | Always zero |
The pseudo-class itself contributes nothing. Only its argument counts.
:is(), :not(), and :has() take the highest argument
:is(#sidebar, .panel, p) a {
color: #333;
}The list contains an ID selector, a class selector, and a type selector. The most specific is #sidebar at (1, 0, 0). The whole selector is therefore (1, 0, 1), which is the ID plus the a. This is true even when the matched element is a p. The browser does not check which item matched. It always uses the highest one.
This is the most common source of surprise with :is(). A single ID in a list raises the weight of every item in that list.
:not() works the same way:
.nav a:not(.active) {
opacity: 0.7;
}The score is .nav at (0, 1, 0), plus a at (0, 0, 1), plus .active at (0, 1, 0), giving (0, 2, 1). The :not() adds nothing. Only .active is counted.
:has() follows the same rule. .card:has(img) scores (0, 1, 1), from .card and the img argument. .card:has(.hero) scores (0, 2, 0).
:where() always adds zero
:where(h1, h2, h3) {
margin-block: 0 0.5em;
}The score is (0, 0, 0), whatever is inside the parentheses. Any other selector overrides it, even *-free low-weight rules such as a plain h2. Authors use :where() for base or reset styles that should be easy to override.
:where(.prose) a {
color: #0645ad;
}
.link {
color: #c00;
}The first selector scores (0, 0, 1), from the a only. .link scores (0, 1, 0), so it wins no matter where it appears in the file. With a plain .prose a at (0, 1, 1), the .link rule would lose.
Worked comparison
The following rules all target one link.
:is(.prose, #content) a {
color: blue;
}
.prose a.external:hover {
color: green;
}
:where(.prose) a:hover {
color: orange;
}Applied to <a class="external" href="https://example.com"> inside <div class="prose">, the scores are:
- The first rule scores
(1, 0, 1), because#contentis the highest item in the:is()list. It applies to the link even though the link is not inside#content, since.prosematches. - The second rule scores
(0, 3, 1), from.prose,.external,:hover, anda. - The third rule scores
(0, 1, 1), from:hoveranda.
On hover, all three rules match. The first has the highest score, so the link stays blue. The green hover rule never shows. The fix is to remove #content from the :is() list, or to move that list into :where().
:where(.prose, #content) a {
color: blue;
}With this change the first rule scores (0, 0, 1), and the green hover rule wins on hover.
Diagram: scoring a selector
Common mistakes
- Expecting
:where()and:is()to behave alike. They match the same elements but score differently. Choose:where()when you want low weight, and:is()when you want the weight of the list. - Putting an ID inside
:is()or:not(). The ID raises the score of the whole selector. - Counting the pseudo-class name in
:not().:not()itself adds nothing, only its argument does. - Treating
::beforelike:hover. A pseudo-element goes in column C, not column B. - Reaching for
!important. If a hover or focus style does not apply, compute both scores first. Lowering the weight of the winning rule is usually a better fix than raising the weight of the losing one.
Debugging tips
Open your browser's developer tools and inspect the element. The Styles panel lists the matching rules and shows overridden declarations with a strikethrough. Hovering over a selector in the panel usually shows its specificity. Use the panel's option to force :hover, :focus, or :active so you can examine state rules without holding the mouse in place.
Exercise
What is the specificity of ul li:not(#home):nth-child(odd)::before, and does it beat .menu li::before at the same position in the file?
Show answer
Score the parts. ul and li are types, giving (0, 0, 2). :not(#home) takes the argument's weight, an ID, giving (1, 0, 0). :nth-child(odd) is a pseudo-class, giving (0, 1, 0). ::before is a pseudo-element, giving (0, 0, 1). The total is (1, 1, 3). The rule .menu li::before scores (0, 1, 2). The first selector wins because its A column is 1 and the other is 0.
Exercise
A reset uses :where(button, a) { color: inherit; }. Later, a component sets a { color: red; } earlier in the file. Which color applies to an a, and why?
Show answer
Red applies. The :where() rule scores (0, 0, 0), and a { color: red; } scores (0, 0, 1). Specificity decides before source order, so the earlier rule still wins.