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>