CSS - Pseudo-classes
Overview
Quick links: Exercises • Checks • Print as PDF
Estimated time: 10–15 minutes
Pseudo-classes match elements in a particular state: interaction, focus, or document structure.
Learning Objectives
- Apply interactive (e.g.,
:hover
,:focus-visible
) and structural pseudo-classes (e.g.,:nth-child
). - Create accessible focus styles.
Details & Examples
Pseudo-classes select elements in a particular state, such as :hover
or :nth-child()
.
- Interactive:
:hover
,:active
,:focus
,:focus-visible
,:disabled
- Structural:
:first-child
,:last-child
,:nth-child()
,:not()
- Row 1
- Row 2
- Row 3 (bold)
- Row 4
Common Pitfalls
- Using
:focus
styles that are hard to see; prefer:focus-visible
with clear outlines.
Checks for Understanding
- How do you select every odd row in a list?
Show answers
li:nth-child(odd)
Exercises
- Build a focus-visible style for links that’s distinct from hover.
- Stripe a list using
:nth-child(odd)
and highlight the 3rd item.
Suggested answers
a:focus-visible{ outline:3px solid #34d399; outline-offset:2px; }
li:nth-child(odd){background:rgba(148,163,184,.08)}; li:nth-child(3){font-weight:700}