Web Analytics

CSS Pseudo Classes

Intermediate ~10 min read

What are Pseudo Classes?

Pseudo-classes select elements based on their state or position. They start with a colon : and add dynamic styling without extra HTML.

Interactive States

:hover, :active, and :focus style elements based on user interaction.

HTML
CSS
JS

First & Last Child

:first-child and :last-child select the first or last element among siblings.

HTML
CSS
JS

Nth Child

:nth-child(n) selects elements by position. Use numbers, odd, even, or formulas like 3n.

HTML
CSS
JS

Not Selector

:not() excludes elements from selection. Great for styling everything except specific items!

HTML
CSS
JS

Link States

Style links with :link, :visited, :hover, and :active. Order matters: LVHA!

Form States

Style form elements with :checked, :disabled, :enabled, :valid, :invalid.

HTML
CSS
JS
Pro Tip: Remember LVHA order for link states: Link, Visited, Hover, Active. This ensures hover and active states work correctly!

Summary

  • :hover, :focus, :active - Interactive states
  • :first-child, :last-child - Position-based
  • :nth-child(n) - Select by formula or keyword
  • :not(selector) - Exclude elements
  • :checked, :disabled, :valid - Form states
  • Pseudo-classes add no specificity weight

Quick Quiz

Which pseudo-class selects every other element?

A
:every-other
B
:nth-child(2n)
C
:alternate
D
:second-child