agyanadda@gmail.com

CSS Pseudo-classes

Advanced CSS 1 min read Updated Dec 1, 2026 Topic 30 of 39

A pseudo-class styles an element in a special state. It starts with one colon :.

Pseudo-class Selects
:hover mouse over
:focus focused input or link
:first-child first child of its parent
:last-child last child
:nth-child(2) 2nd child; also odd, even, 3n
:not(.active) elements without class active
:checked checked checkbox or radio
:disabled disabled form field

Example

Example — Pseudo-classes on a list HTML
<style>
  li { padding: 6px; }
  li:first-child { font-weight: bold; }
  li:last-child { color: gray; }
  li:nth-child(even) { background: #f0f6ff; }
  li:hover { background: #fff3cd; }
  li:not(.done) { border-left: 4px solid tomato; }
</style>

<ul>
  <li class="done">Learn HTML</li>
  <li>Learn CSS</li>
  <li>Learn JavaScript</li>
  <li>Build a project</li>
</ul>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which selects the third list item?

Exercise 2 True / False

Pseudo-classes start with a single colon.

Exercise 3 Fill in the Blank

li:___-child selects the last item.

Frequently Asked Questions

:nth-child counts all siblings; :nth-of-type counts only siblings of the same tag.