</> Code Corner

CSS · CSS Tutorial · Lesson 29

CSS Pseudo-classes

:hover, :focus, :first-child, :nth-child and more.

A pseudo-class defines a special state of an element, such as when the mouse hovers over it or when it is focused.

Example

<style>
button:hover { background: #04AA6D; color: white; }
input:focus { outline: 3px solid dodgerblue; }
li:first-child { font-weight: bold; }
li:nth-child(odd) { background: #f1f1f1; }
li:last-child { color: tomato; }
p:not(.special) { color: gray; }
</style>
<button>Hover me</button> <input placeholder="Focus me">
<ul><li>One</li><li>Two</li><li>Three</li><li>Four</li></ul>
<p>Gray</p><p class="special">Not gray</p>
Pseudo-class Matches
:hover / :active / :focus Mouse over / being clicked / has focus
:first-child / :last-child First / last child of its parent
:nth-child(n) The nth child (odd, even, 3n…)
:not(selector) Elements that do not match
:checked / :disabled Checked / disabled form controls

Keep reading “CSS Pseudo-classes”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋