Skip to content
Kuza Ndoto Academy

Pseudo-classes & Pseudo-elements

Beginner → IntermediateChapter 4 · Page 14 of 224 min readUpdated 15 Nov 2026

Pseudo-classes and pseudo-elements let you style things that plain selectors can't reach: an element's state (being hovered, focused or checked), its position among siblings, or a part of it, such as its first letter or decorative content placed before it.

Pseudo-classes: one colon

A pseudo-class is added to a selector with a single colon and matches elements in a particular state or position. a:hover means "a link while the pointer is over it".

User-action states

Pseudo-classMatches when…
:hoverThe pointer is over the element (phones have no real hover)
:activeThe element is being pressed or clicked
:focusThe element has focus (from a click, a tap or the keyboard)
:focus-visibleThe element has focus and the browser thinks a focus ring should show, typically keyboard focus
:visitedA link the user has already visited (only colours can change, for privacy)
a:hover {
  text-decoration: underline wavy;
}
button:focus-visible {
  outline: 3px solid #f59e0b;
  outline-offset: 2px;
}
Common mistake

Never write :focus { outline: none; } without a replacement. Keyboard users, including many people with disabilities, rely on the focus ring to see where they are. Style :focus-visible instead so the ring appears for keyboard users but not after every mouse click.

Form states

:checked (ticked checkboxes and radio buttons), :disabled, :required, :invalid, :valid and :placeholder-shown let you style forms without JavaScript. :user-invalid is a newer, friendlier version of :invalid that only matches after the user has interacted with the field, so a form doesn't shout "error" before anyone has typed.

Structural pseudo-classes

These match by position among siblings:

  • :first-child and :last-child: the first or last child of its parent.
  • :nth-child(n): by position. :nth-child(3) is the third child; :nth-child(odd) and :nth-child(even) are perfect for striped table rows.
  • :nth-child(3n+1): a formula where n counts 0, 1, 2…, so it matches children 1, 4, 7, 10 and so on.
  • :nth-of-type(), :first-of-type: like the above, but counting only siblings of the same element type.
tr:nth-child(even) {
  background-color: #f1f5f9;
}
li:last-child {
  border-bottom: none;
}

Logical pseudo-classes

:not() excludes matches: li:not(:last-child) selects every list item except the last, which is a neat way to add dividers between items. :is() shortens long selector lists (:is(h1, h2, h3) a), and :has(), the "parent selector", is covered in Modern CSS Features.

Pseudo-elements: two colons

A pseudo-element styles a part of an element, or creates a box that isn't in the HTML. Modern CSS writes them with two colons to tell them apart from pseudo-classes.

Pseudo-elementWhat it styles
::before / ::afterA generated box inserted as the first / last child of the element
::first-letterThe first letter of a block, for drop caps
::first-lineThe first line of text as currently displayed
::placeholderThe placeholder text in an input
::selectionText the user has highlighted
::markerThe bullet or number of a list item

::before and ::after

These two need the content property, even if it's empty (content: ""); without it, nothing appears. They're used for decorative touches: icons, quotation marks, underlines and badges.

.external::after {
  content: " ↗";
}
.required-label::after {
  content: " *";
  color: crimson;
}

Links with the external class get a small arrow after them, and labels with required-label get a red asterisk. Screen readers may or may not read generated content, so never put essential information in content.

Try it

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Pseudo-classes</title>
  <style>
    body { font-family: system-ui, sans-serif; margin: 1.5rem; }
    ul { list-style: none; padding: 0; max-width: 300px; }
    li { padding: 0.5rem; }
    li:nth-child(odd) { background: #f1f5f9; }
    li:not(:last-child) { border-bottom: 1px solid #cbd5e1; }
    li:first-child::before { content: "★ "; color: #d97706; }
    p::first-letter {
      font-size: 2.5rem;
      font-weight: bold;
      color: #1e3a8a;
    }
    button {
      padding: 0.6rem 1.2rem;
      background: #1e3a8a;
      color: white;
      border: none;
      border-radius: 6px;
    }
    button:hover { background: #2563eb; }
    button:active { transform: scale(0.96); }
    button:focus-visible { outline: 3px solid #f59e0b; outline-offset: 2px; }
  </style>
</head>
<body>
  <ul>
    <li>Gor Mahia</li>
    <li>AFC Leopards</li>
    <li>Tusker FC</li>
    <li>Kakamega Homeboyz</li>
  </ul>
  <p>Football is one of the most popular sports in Kenya, and the big derbies fill stadiums.</p>
  <button>Vote now</button>
</body>
</html>

The list shows grey stripes on the first and third rows, thin dividers between items (but none under the last) and a gold star before the first team. The paragraph begins with a large dark blue "F". The button turns brighter blue when you hover, shrinks slightly while pressed, and shows an amber ring when you reach it with the Tab key.

Key points

  • Pseudo-classes (:hover, :focus-visible, :nth-child(), :not()) match states and positions.
  • Pseudo-elements (::before, ::after, ::first-letter) style parts of an element or add generated boxes.
  • ::before and ::after need content to appear.
  • Always keep a visible focus style for keyboard users.
Up next · Selectors, Cascade & EffectsSpecificity & the CascadeWhich rule wins, and why.
→

Tip: use the ← and → keys to move between pages.