Full answer
:nth-child(n) counts all sibling elements, whatever their tag. :nth-of-type(n) counts only siblings that have the same tag name as the element being tested.
For the selector p:nth-child(2), the browser asks two things: is this element a p, and is it the second child of its parent? For p:nth-of-type(2), it asks: is this a p, and is it the second p among its siblings?
Both accept odd, even, a number, or a formula an+b such as 3n+1 (every third item starting from the first).
In the first block, the heading is child number 1, so the first paragraph is child 2 and turns red. In the second block, the second paragraph turns red, because it is the second p. In the third block, p:nth-child(1) matches nothing (the heading is first), but p:nth-of-type(1) matches the only paragraph. Rows 1 and 4 of the table get the blue background from 3n+1.
Use nth-of-type when other tags are mixed in and you care about one tag. Use nth-child when position among all items matters, for example striping every second item in a list that holds only li elements. A newer form, :nth-child(2 of .selected), counts only the siblings that match a selector list. Check the support tables before using it in production.
<style>
li { padding: 4px; }
.a p:nth-child(2) { color: red; }
.b p:nth-of-type(2) { color: red; }
.c p:nth-child(1) { color: red; }
.c p:nth-of-type(1) { background: #ffd; }
tr:nth-child(3n+1) { background: #def; }
</style>
<div class="a" id="a"><h2>Heading</h2><p id="a1">First p</p><p id="a2">Second p</p></div>
<div class="b" id="b"><h2>Heading</h2><p id="b1">First p</p><p id="b2">Second p</p></div>
<div class="c" id="c"><h2>Heading</h2><p id="c1">First p</p></div>
<table>
<tr id="r1"><td>1</td></tr><tr id="r2"><td>2</td></tr><tr id="r3"><td>3</td></tr><tr id="r4"><td>4</td></tr>
</table>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- nth-child counts all sibling elements regardless of tag.
- nth-of-type counts only siblings with the same tag name.
- Counting starts at 1, and an+b formulas start with n = 0.
- p:nth-child(1) matches nothing when a heading comes first.
- nth-last-child and the of S syntax extend the idea; check support for of S.
Common mistakes
- expecting nth-child(2) to mean the second element with that class. It means the second child of any kind, and the class is only an extra filter.
- forgetting that counting starts at 1, not 0, and that n in a formula starts at 0 and goes up.
Follow-ups
- how do you select the last three items?
- How do you select the first three items of a list?
- What is the difference between :first-child and :first-of-type?
Tips
- Draw the siblings on paper and number them. Most nth-child mistakes disappear once you list every element, including headings, that sits between the items.
- Draw the children and number them while you explain. It prevents mistakes in a live round.
- Use odd and even for striping, and 3n+1 to show you know the formula.