CSS - Pseudo-classes

Overview

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()

Hover me

  • 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

  1. How do you select every odd row in a list?
Show answers
  1. li:nth-child(odd)

Exercises

  1. Build a focus-visible style for links that’s distinct from hover.
  2. Stripe a list using :nth-child(odd) and highlight the 3rd item.
Suggested answers
  1. a:focus-visible{ outline:3px solid #34d399; outline-offset:2px; }
  2. li:nth-child(odd){background:rgba(148,163,184,.08)}; li:nth-child(3){font-weight:700}