Sub topic 7
Position among same-type siblings: :nth-of-type() and friends
This chapter assumes you know :nth-child() and An+B expressions from the previous chapters, plus type, class, and descendant selectors. Here you will learn how to count siblings by element type instead of by position alone.
Why counting by type matters
:nth-child() counts every sibling element. :nth-of-type() counts only siblings that have the same element name (the same tag). This difference matters when a container mixes element types, such as a heading followed by several paragraphs and images.
Consider this markup:
<article>
<h2>Title</h2>
<p>First paragraph</p>
<img src="photo.jpg" alt="A photo">
<p>Second paragraph</p>
<p>Third paragraph</p>
</article>The second paragraph is the fourth child of <article>, but it is the second <p> among its siblings of type p.
article p:nth-child(2) {
color: red;
}
article p:nth-of-type(2) {
color: green;
}The first rule matches nothing useful here. The second child of <article> is the first <p>, so p:nth-child(2) selects "First paragraph". The second rule selects "Second paragraph", which is the second <p> in the list of paragraphs.
The family of type-based selectors
The type-based selectors mirror the structural ones you already know.
| Selector | Matches |
|---|---|
| `:first-of-type` | The first sibling of its element type |
| `:last-of-type` | The last sibling of its element type |
| `:only-of-type` | An element with no siblings of the same type |
| `:nth-of-type(An+B)` | Siblings of the same type at positions matching the formula |
| `:nth-last-of-type(An+B)` | The same, counted from the end |
The An+B syntax works exactly as it does in :nth-child(). The keywords odd and even are available, and counting starts at 1.
img:first-of-type {
border: 2px solid #1a73e8;
}
p:nth-of-type(odd) {
background: #f5f5f5;
}
p:nth-last-of-type(1) {
margin-bottom: 0;
}
figure:only-of-type {
float: none;
}How the counting works
Each element has one position among all siblings and a separate position among siblings of its own type. :nth-of-type() uses the second number.
Common errors
- Assuming a class counts as a type.
:nth-of-type()looks at the element name only. In.item:nth-of-type(2), the browser first finds the second element of that tag name among siblings, and only then checks for theitemclass. If the second<div>lacks that class, nothing matches. - Expecting the selector before the colon to filter the count.
div.card:nth-of-type(2)does not mean "the second.card". It means "the second<div>, if it also has the classcard". To count by class, use:nth-child(2 of .card), which is supported in current browsers but is newer than the other selectors in this chapter. - Using it with no type selector. Writing
:nth-of-type(2)alone is valid and matches any element that is the second of its own type. This can select several unrelated elements in the same container.
Exercise
In the <article> markup above, which element does p:nth-last-of-type(2) select, and why does p:nth-last-of-type(2) differ from p:nth-last-child(2)?
Show answer
It selects "Second paragraph". Counting <p> elements from the end, "Third paragraph" is number 1 and "Second paragraph" is number 2. p:nth-last-child(2) counts all children from the end, so the second-to-last child is also "Second paragraph" here. In this markup both match the same element, but they would diverge if an <img> came after the last paragraph, because :nth-last-child() counts the image while :nth-last-of-type() ignores it.