Sub topic 6
Counting with :nth-child() and An+B
This chapter assumes you know basic CSS selectors and that you have met :first-child and :last-child. Here you will learn the An+B notation that :nth-child() uses to pick elements by position.
What :nth-child() counts
:nth-child() matches an element based on its position among all of its siblings. Counting starts at 1, not 0. Every sibling counts, whatever its tag.
li:nth-child(3) {
color: #c00;
}This rule matches an li only if it is the third child of its parent. If the parent's third child is a p, nothing matches.
The An+B formula
Instead of a single number, you can give a formula. The browser tries n = 0, 1, 2, 3, and so on. Any element whose position equals An+B for some n of 0 or more is selected.
Ais the step size, the gap between matches.nis the counter, starting at 0.Bis the offset, where counting starts.
| Expression | Positions matched | Meaning |
|---|---|---|
| `2n` or `even` | 2, 4, 6, ... | Every even child |
| `2n+1` or `odd` | 1, 3, 5, ... | Every odd child |
| `3n` | 3, 6, 9, ... | Every third child |
| `3n+1` | 1, 4, 7, ... | Every third child, starting at the first |
| `n+4` | 4, 5, 6, ... | The fourth child and all after it |
| `-n+3` | 3, 2, 1 | The first three children |
Positions below 1 never match, so -n+3 stops at 1. Spaces are allowed around the sign in -n + 3, but not between the sign and the number in the way - n would suggest, so keep the sign attached to n.
Worked example: a striped table with a highlighted header area
tr:nth-child(even) {
background: #f5f5f5;
}
li:nth-child(-n + 3) {
font-weight: bold;
}
li:last-child {
border-bottom: 0;
}The first rule stripes every even row. The second makes the first three list items bold. The third removes the border from the final item, because :last-child counts from the end.
Counting from the end and filtering
:nth-last-child() uses the same formulas but counts from the last sibling.
li:nth-last-child(-n + 2) {
color: #767676;
}This dims the last two items, however many there are.
In modern browsers you can add of S to count only siblings that match a selector:
li:nth-child(odd of :not(.hidden)) {
background: #eef;
}Here hidden items are skipped when counting, so the striping stays even. Check browser support before relying on of S.
Common errors
- Starting at 0.
:nth-child(0)matches nothing. The first child is 1. - Forgetting that other tags count. In
<div><h2>…</h2><p>…</p><p>…</p></div>,p:nth-child(2)matches the firstp, since theh2is child 1. To count only elements of one type, use:nth-of-type(), covered in the next chapter. - Confusing
n+3with3n. The first matches 3 and everything after. The second matches every third element. - Hiding items with
display: none. Hidden siblings still count, so stripes can look uneven.
Exercise
Write one selector that picks every third li starting with the second one (positions 2, 5, 8, ...). Then write a selector for all li elements except the first two.
Show answer
li:nth-child(3n + 2) picks positions 2, 5, 8. li:nth-child(n + 3) picks position 3 and everything after it, which excludes the first two.
Exercise
In a list of 10 items, which positions does li:nth-last-child(-n + 3) select?
Show answer
Positions 8, 9 and 10. It counts from the end, so the last three items match.