Skip to content
Justice Tech School

Pseudo-classes

A pseudo-class starts with a colon and selects elements in a particular state or position: :hover, :focus, :checked, :first-child, :last-child, :nth-child(2n) and :not(.x).

Pseudo-classes keep your HTML clean. Zebra striping a list needs no extra classes: li:nth-child(even) does the job.

Example
<style>
  li:nth-child(even) { background: #e0f2fe; }
  li:first-child { font-weight: bold; }
  li:not(:last-child) { border-bottom: 1px solid #94a3b8; }
  li:hover { color: crimson; }
</style>
<ul>
  <li>One</li><li>Two</li><li>Three</li><li>Four</li>
</ul>

Exercise

Style every even list item.

li:nth-child() {
  background: #eee;
}

Quiz Test

Which selector matches the first child of its parent?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.