nth-child and other pseudo-classes in CSS
31 Pseudo-classes that pick and react
fairHighlight every third row of a list starting from the first, style the last item differently, and give the links inside a hover and a keyboard focus state that are not the same rule.
Stuck? Three hints, smallest first
- 1.
:nth-child(3n+1)matches items 1, 4, 7 — the offset is the starting item. - 2.
:focus-visiblefires for keyboard focus but not for a mouse click, which is why it replaced:focusfor rings. - 3.
:last-childis cleaner than adding a.lastclass you have to maintain.
<ul class="board">
<li><a href="#r1">06:30 Abuja</a></li>
<li><a href="#r2">08:00 Kano</a></li>
<li><a href="#r3">11:15 Enugu</a></li>
<li><a href="#r4">14:45 Ibadan</a></li>
<li><a href="#r5">17:00 Benin</a></li>
<li><a href="#r6">19:30 Warri</a></li>
<li><a href="#r7">21:30 Jos</a></li>
</ul>
.board {
list-style: none;
margin: 0;
padding: 0;
max-width: 240px;
border: 1px solid #c9cad1;
border-radius: 8px;
overflow: hidden;
}
.board li { border-bottom: 1px solid #e4e5e9; }
/* Items 1, 4 and 7. The +1 is where the run starts, not how many are skipped. */
.board li:nth-child(3n + 1) { background: #eef1fd; }
.board li:last-child { border-bottom: 0; font-weight: 600; }
.board a {
display: block;
padding: 8px 12px;
color: #17171a;
text-decoration: none;
font-size: 0.88rem;
}
.board a:hover { background: #3055d8; color: #ffffff; }
/* Not the same rule: a keyboard user gets a ring, a mouse user does not. */
.board a:focus-visible {
outline: 2px solid #3055d8;
outline-offset: -2px;
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
// Work out which items 3n + 1 selects, for the seven that are there.
const count = countTag(html, 'li');
const picked = Array.from({ length: count }, (_, i) => i + 1).filter((i) => i % 3 === 1);
test('seven items', count, 7);
test('3n + 1 picks the first, fourth and seventh', picked, [1, 4, 7]);
test('that selector is the one in the stylesheet', /nth-child\(3n \+ 1\)/.test(css), true);
test('the last item is styled without a helper class', /:last-child/.test(css), true);
test('and no such class exists in the markup', /class="last"/.test(html), false);
test('hover and keyboard focus are separate rules', /a:hover/.test(css) && /a:focus-visible/.test(css), true);
test('the outline is offset inwards so the rounded corner does not clip it', /outline-offset:\s*-2px/.test(css), true);
test('the whole row is the target, not just the text', /\.board a\s*\{[^}]*display:\s*block/.test(css), true);
Output
3n + 1 reads as “every third, starting at the first”, and the second check spells out which items that is — the offset trips people up because 3n alone starts at 3. Two habits in the rest: :last-child instead of a class means nothing to update when a row is added, and display: block on the link makes the entire row clickable rather than the six characters of text. Keeping :hover and :focus-visible as separate rules is deliberate: a mouse user does not want a ring, a keyboard user cannot work without one.