Sub topic 6
Pseudo-classes
This chapter assumes you can write type, class, and ID selectors, combine them (for example a.note), and read a CSS rule. It builds on those selectors and feeds into the specificity chapters that follow.
What a pseudo-class does
A pseudo-class is a keyword that starts with a single colon and adds a condition to a selector. The element matches only while the condition is true. The condition might be that the pointer is over the element, that it is the first child of its parent, or that a checkbox is ticked. You don't add a class to the HTML. The browser tracks the state and updates the match for you.
Common groups
| Group | Examples | Matches when |
|---|---|---|
| Links | `:link`, `:visited` | A link is unvisited or visited |
| User action | `:hover`, `:active`, `:focus`, `:focus-visible` | The user points at, presses, or focuses the element |
| Form state | `:checked`, `:disabled`, `:required`, `:invalid` | A form control is in that state |
| Position | `:first-child`, `:last-child`, `:nth-child(2n)`, `:first-of-type` | The element has that place among its siblings |
A worked example
a:link { color: blue; }
a:visited { color: purple; }
a:hover { text-decoration: underline; }
a:active { color: red; }
:focus-visible { outline: 2px solid orange; }
li:first-child { font-weight: bold; }
tr:nth-child(even) { background: #f4f4f4; }
input:checked + label { color: green; }The last rule combines a pseudo-class with the adjacent-sibling combinator. It styles the label that directly follows a checked input.
Specificity and the link order
A pseudo-class counts in the middle column of the specificity triple, the same column as a class. So a:hover is (0,1,1): one pseudo-class and one type. .btn:hover is (0,2,0).
In the example, a:link, a:visited, a:hover, and a:active all have specificity (0,1,1). Because they tie, source order decides. If you put a:hover before a:link, an unvisited link matches both rules and the later a:link color wins. The hover style would then appear to do nothing. The usual order is link, visited, hover, active.
Common errors
- A stray space.
nav:first-childselects anavthat is a first child.nav :first-childhas a descendant combinator and an implied universal selector. It selects any first child inside anav. - Confusing position pseudo-classes.
p:first-childmatches a paragraph only if it is the first child of its parent. If anh2comes first, nothing matches. Usep:first-of-typeto get the first paragraph among its siblings. - Removing focus indicators.
:focus { outline: none; }makes keyboard navigation hard to follow. - Relying on hover. Touch screens have no reliable hover, so don't hide essential information behind
:hover.
Related selectors
Pseudo-classes such as :is(), :where(), and :not() take selectors as arguments and have their own specificity rules. A later chapter covers them. Pseudo-elements such as ::before use a double colon and are a different kind of selector. Don't mix them up.
Exercise
Given <div><h2>Title</h2><p>Intro</p><p>More</p></div>, does p:first-child match the "Intro" paragraph? Which selector would match it, and what is the specificity of p:first-of-type?
Show answer
No. The h2 is the first child of the div, so p:first-child matches nothing. p:first-of-type matches "Intro" because it is the first p among its siblings. Its specificity is (0,1,1): one pseudo-class and one type.
Exercise
A stylesheet lists a:hover { color: red; } first and a:link { color: blue; } after it. Unvisited links never turn red on hover. Why, and how do you fix it?
Show answer
Both selectors have specificity (0,1,1), so the later rule wins. a:link overrides a:hover for unvisited links. Fix it by putting a:hover after a:link and a:visited.