Selectors & the Cascade Lesson 25 of 33
CSS Pseudo-classes — :hover, :focus, :nth-child and More
Style elements based on their state or position — hover, focus, checked, first-child, nth-child, not, and form validation states like valid and invalid.
A pseudo-class selects elements based on their state (being hovered, focused, checked) or their position (first, last, every other). It's written with a single colon after a selector: button:hover, li:first-child. You've already used a few in the Links lesson. Here's the full toolbox.
User-action pseudo-classes
:hover— the mouse pointer is over the element:active— the element is being pressed:focus— the element has focus (clicked into an input, or reached by Tab):focus-visible— has focus and the browser thinks a focus ring should show (usually keyboard users)
<style>
input {
padding: 10px;
border: 2px solid #ccc;
border-radius: 8px;
font-size: 16px;
outline: none;
}
input:hover { border-color: #888; }
input:focus { border-color: #0f5c4d; box-shadow: 0 0 0 4px rgba(15, 92, 77, 0.2); }
button { padding: 10px 16px; border: 0; border-radius: 8px; background: #0f5c4d; color: white; font-size: 16px; }
button:hover { background: #127a65; }
button:active { transform: scale(0.96); }
button:focus-visible { outline: 3px solid #f2b33d; outline-offset: 2px; }
</style>
<input type="text" placeholder="Your LRN">
<button>Check</button>
<p>Hover, click into the box, and press the button.</p>
Notice that the input removes the default outline but replaces it with a clear border color and glow on focus. That's the right way: never remove the focus indicator without adding a visible replacement.
Structural pseudo-classes
These select elements by their position among their siblings.
:first-child— the first child of its parent:last-child— the last child:nth-child(n)— by number or pattern:only-child— the only child
nth-child patterns
| Selector | Selects |
|---|---|
:nth-child(3) | The 3rd child |
:nth-child(odd) | 1st, 3rd, 5th… |
:nth-child(even) | 2nd, 4th, 6th… |
:nth-child(3n) | Every 3rd: 3, 6, 9… |
:nth-child(-n + 3) | The first 3 |
:nth-last-child(1) | The last one (same as :last-child) |
<style>
ol { font-family: Arial, sans-serif; padding-left: 24px; }
li { padding: 6px 10px; border-radius: 6px; margin-bottom: 4px; }
li:nth-child(-n + 3) { font-weight: bold; background: #fdf3dc; }
li:first-child { background: #f2b33d; }
li:last-child { color: #888; }
</style>
<h3>Science Quiz Bee — Ranking</h3>
<ol>
<li>Andrea Santos — 48</li>
<li>Miguel Reyes — 45</li>
<li>Bea Cruz — 44</li>
<li>Paolo Garcia — 40</li>
<li>Lorna Dizon — 38</li>
</ol>
Removing the extra border on the last item
A very common real-world use: every item has a bottom border except the last one.
.menu li { border-bottom: 1px solid #ddd; }
.menu li:last-child { border-bottom: none; }
:not()
:not() selects elements that do not match something. The same example in one rule:
.menu li:not(:last-child) { border-bottom: 1px solid #ddd; }
Form state pseudo-classes
:checked— a checked checkbox or radio button:disabled/:enabled:required/:optional:valid/:invalid— whether the value passes HTML validation (liketype="email"):placeholder-shown— the input is empty and showing its placeholder
<style>
input { padding: 10px; font-size: 16px; border: 2px solid #ccc; border-radius: 8px; width: 260px; }
input:not(:placeholder-shown):invalid { border-color: #b4372b; background: #fbe9e6; }
input:valid { border-color: #1d7a43; background: #e4f4e9; }
label { display: block; font-family: Arial, sans-serif; margin-bottom: 6px; }
</style>
<label for="email">School email</label>
<input id="email" type="email" placeholder="juan@school.edu.ph" required>
Type an incomplete email: the box turns red. Finish it: the box turns green. The :not(:placeholder-shown) part stops the field from turning red before the student has typed anything, which would feel unfriendly.
A to-do list with :checked
<style>
label { display: block; font: 18px Arial, sans-serif; margin: 6px 0; }
input:checked + span { text-decoration: line-through; color: #999; }
</style>
<label><input type="checkbox"> <span>Answer Module 3 in Math</span></label>
<label><input type="checkbox"> <span>Print the Araling Panlipunan project</span></label>
<label><input type="checkbox"> <span>Practice for the Sabayang Pagbigkas</span></label>
The + means "the element right after". So input:checked + span is "the span that comes right after a checked input". You'll see more of these combinations in the next lesson.
Other useful ones
:target— the element whose id matches the URL's#part. For example, after clicking a link to#faq, the element withid="faq"can be highlighted.:empty— elements with nothing inside them.:is()— shortens long selector lists::is(h1, h2, h3):hover.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04