TExcelTAD
Home › CSS › CSS Pseudo-classes

CSS Pseudo-classes

A pseudo-class selects elements based on a state or position that isn't written in the HTML: being hovered, being the first child, being checked, being invalid. It's written with a single colon after a selector: button:hover, li:first-child.

User-action pseudo-classes

Pseudo-class Matches when…
:hover The pointer is over the element
:active The element is being pressed
:focus The element has focus
:focus-visible Focused and the browser decides a ring should show (keyboard)
:focus-within The element or any descendant has focus
:visited / :link Visited / unvisited links
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 16px; }
  .btn { padding: 12px 20px; font-size: 16px; border: none; border-radius: 8px; background: #3949ab; color: white; transition: .15s; }
  .btn:hover { background: #5c6bc0; transform: translateY(-2px); }
  .btn:active { background: #1a237e; transform: translateY(1px); }
  form { margin-top: 16px; padding: 12px; border: 2px solid #ccc; border-radius: 10px; }
  form:focus-within { border-color: #43a047; background: #f1f8e9; }
  input { padding: 6px; font-size: 15px; }
</style>
</head>
<body>
  <button class="btn">Hover and press me</button>
  <form>The whole form highlights when you click into it: <input placeholder="focus me"></form>
</body>
</html>

Structural pseudo-classes

These select elements by their position among siblings.

Pseudo-class Matches
:first-child / :last-child First / last child of its parent
:only-child The only child
:nth-child(n) By position: 2, odd, even, 3n, 3n+1, -n+3 (first three)
:nth-last-child(n) Counting from the end
:first-of-type / :nth-of-type(n) Same, but only counting siblings of the same tag
:empty Elements with no children or text
:root The <html> element
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 4px; }
  .grid div { padding: 10px 0; text-align: center; background: #eceff1; border-radius: 4px; }
  .grid div:nth-child(odd) { background: #cfd8dc; }
  .grid div:nth-child(3n) { background: #ffab91; }
  .grid div:first-child { background: #66bb6a; color: white; }
  .grid div:last-child { background: #42a5f5; color: white; }
  .grid div:nth-child(-n+2) { font-weight: 900; }
</style>
</head>
<body>
  <div class="grid">
    <div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div>
    <div>7</div><div>8</div><div>9</div><div>10</div><div>11</div><div>12</div>
  </div>
  <p>green = first-child, blue = last-child, orange = 3n, grey shades = odd, bold = first two (-n+2).</p>
</body>
</html>

Tip: In an+b, think "every a-th element, starting at b". 3n+1 gives 1, 4, 7, 10…

Form state pseudo-classes

Pseudo-class Matches
:checked Checked checkboxes / radios / selected options
:disabled / :enabled Disabled or enabled controls
:required / :optional Fields with or without required
:valid / :invalid Passing / failing validation
:user-invalid Invalid after the user has interacted
:placeholder-shown An input currently showing its placeholder
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  input[type=email] { padding: 8px; font-size: 15px; border: 3px solid #bdbdbd; border-radius: 6px; width: 220px; }
  input[type=email]:valid { border-color: #43a047; }
  input[type=email]:invalid:not(:placeholder-shown) { border-color: #e53935; }
  button:disabled { opacity: .4; cursor: not-allowed; }
  .opt:checked + span { background: #fff176; }
</style>
</head>
<body>
  <p><input type="email" placeholder="type an email" required></p>
  <p><button disabled>Disabled button</button></p>
  <label><input class="opt" type="checkbox"> <span>Highlight me when checked</span></label>
</body>
</html>

Logical pseudo-classes

Pseudo-class Meaning
:not(sel) Elements that do not match
:is(a, b, c) Matches any of the list (takes the highest specificity)
:where(a, b, c) Same as :is but with zero specificity
:has(sel) Elements that contain something matching (the "parent selector")
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; gap: 10px; flex-wrap: wrap; }
  .card { width: 130px; padding: 10px; border: 2px solid #bdbdbd; border-radius: 10px; }
  .card:has(.sale) { border-color: #e53935; background: #ffebee; }
  .card:not(:has(.sale)) { opacity: .7; }
  .sale { color: #e53935; font-weight: bold; }
  :is(h4, h5) { margin: 0 0 6px; }
</style>
</head>
<body>
  <div class="card"><h4>Shoes</h4>$80</div>
  <div class="card"><h4>Jacket</h4><span class="sale">SALE $45</span></div>
  <div class="card"><h5>Socks</h5>$6</div>
</body>
</html>

Test yourself

Which selector matches every third list item, starting with the first?

  1. li:nth-child(3)
  2. li:nth-child(3n)
  3. li:nth-child(3n+1)
  4. li:nth-of-type(odd)

What does .card:has(img) select?

  1. Images inside cards
  2. Cards that contain an img
  3. Cards that are images
  4. The first img

Which pseudo-class styles a form when any input inside it is focused?

  1. :focus
  2. :focus-visible
  3. :focus-within
  4. :active