CSS Pseudo-class
A pseudo-class is a keyword added to a selector with a single colon that targets an element in a particular state or position, such as a:hover or li:first-child. It lets you style things that are not written in the HTML: whether a link has been visited, whether an input has focus, or whether a list item is third in its list. You use pseudo-classes whenever styling should react to user interaction or document structure without extra classes.
State-based pseudo-classes
These match elements based on what the user is doing or what state a form control is in.
:hovermatches while the pointer is over the element.:activematches while the element is being pressed.:focusmatches the element that currently has keyboard focus.:focus-visiblematches only when the browser decides a focus ring should show, usually for keyboard users, so mouse clicks on buttons do not leave an outline.:checked,:disabled,:required,:invalidand:placeholder-showndescribe form controls.:linkand:visiteddescribe links that have and have not been visited.
Structural pseudo-classes and the an+b formula
Structural pseudo-classes pick elements by their position among siblings. :first-child, :last-child and :only-child are straightforward. :nth-child() takes a formula an+b: the browser counts n up from 0 and selects every resulting position. So 2n gives 2, 4, 6 (even rows), 2n+1 gives 1, 3, 5 (odd rows), and -n+3 gives the first three items. The keywords even and odd are shortcuts.
/* Zebra-striped table rows */
tbody tr:nth-child(even) { background: #f6f8fa; }
/* No divider after the last item */
.menu li:not(:last-child) { border-bottom: 1px solid #e5e5e5; }
/* Keyboard-only focus ring */
button:focus-visible { outline: 3px solid #2563eb; outline-offset: 2px; }
/* Style a label next to a checked checkbox */
input[type="checkbox"]:checked + label { text-decoration: line-through; }
/* Highlight a form field that contains an invalid value */
input:invalid:not(:placeholder-shown) { border-color: #dc2626; }
/* Links in the classic LVHA order */
a:link { color: #1d4ed8; }
a:visited { color: #6d28d9; }
a:hover { color: #1e40af; }
a:active { color: #b91c1c; }
Logical helpers: :not(), :is() and :where()
:not() excludes elements matching its argument, which is cleaner than writing a rule and then undoing it. :is() groups several selectors so you avoid repetition: :is(h1, h2, h3) a means links inside any of those headings. :where() works like :is() but adds zero specificity, which makes it ideal for default styles that should be easy to override.
On specificity in general, a pseudo-class counts the same as a class. So a:hover beats a plain a rule but loses to .nav a.
Where people go wrong
The first confusion is :first-child versus :first-of-type. p:first-child matches a paragraph only if it is the very first child of its parent. If a heading comes first, no paragraph matches. p:first-of-type matches the first paragraph regardless of other element types before it. The same applies to :nth-child() and :nth-of-type().
The second is link order. Because all four link pseudo-classes have the same specificity, the last rule wins. If :hover is written before :visited, visited links never show the hover colour. Keep the order link, visited, hover, active.
Finally, avoid removing focus outlines with outline: none without adding a replacement. Keyboard users rely on them to see where they are.
Takeaway
Pseudo-classes let CSS respond to state and structure. Learn the hover and focus family for interaction, :nth-child() for patterns, and :not(), :is() and :where() for writing shorter, cleaner selectors. The next lesson covers pseudo-elements, which use two colons and style parts of an element rather than its state.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.