Sub topic 7
Position among same-type siblings: :nth-of-type() and friends
This chapter assumes you can write type, class, and attribute selectors. It also assumes you know the :nth-child() An+B syntax from the previous chapters and that you understand the difference between a child and a sibling. The new idea here is that a position can be counted among elements of one tag name instead of among all siblings.
What "of type" means
The -of-type family counts siblings that share the same element name. Siblings are elements with the same parent. The type is the tag name, such as p, li, or img. Classes, attributes, and text content play no part in the count.
Take this markup:
<article>
<h2>Release notes</h2>
<p class="intro">What changed this month.</p>
<img src="chart.png" alt="Monthly usage chart">
<p>Performance improved.</p>
<p class="warning">The old API is removed.</p>
</article>The children of article have these positions:
| Element | Position among all children | Position among its type |
|---|---|---|
| `h2` | 1 | 1st `h2` |
| `p.intro` | 2 | 1st `p` |
| `img` | 3 | 1st `img` |
| `p` (Performance) | 4 | 2nd `p` |
| `p.warning` | 5 | 3rd `p` |
The two counting methods give different results on the same elements:
/* Matches the p.intro element: a p that is also the 2nd child */
p:nth-child(2) {
font-size: 1.25rem;
}
/* Matches the p in position 4: the 2nd p among its siblings */
p:nth-of-type(2) {
font-style: italic;
}p:nth-child(2) asks two questions: is this a p, and is it the second child overall? p:nth-of-type(2) asks whether this is a p and whether it is the second p among its siblings. The h2 and img do not enter the count.
The family of selectors
All of these are pseudo-classes, so each uses a single colon and adds one class-level unit of specificity.
| Selector | Matches an element that is... |
|---|---|
| `:first-of-type` | the first of its tag name among its siblings |
| `:last-of-type` | the last of its tag name among its siblings |
| `:only-of-type` | the only one of its tag name among its siblings |
| `:nth-of-type(An+B)` | at a counted position among same-name siblings |
| `:nth-last-of-type(An+B)` | at a counted position, counting from the end |
:nth-of-type() takes the same An+B argument as :nth-child(). You can pass a number (3), a keyword (odd, even), or a formula (3n + 1, -n + 2). :first-of-type is the same as :nth-of-type(1), and :last-of-type is the same as :nth-last-of-type(1).
/* Every other paragraph, ignoring headings and images between them */
article p:nth-of-type(odd) {
margin-inline-start: 1rem;
}
/* The last paragraph in each article */
article p:last-of-type {
margin-block-end: 0;
}
/* The second-to-last list item */
li:nth-last-of-type(2) {
border-bottom: 1px solid #ccc;
}
/* An img that is the only img among its siblings */
figure img:only-of-type {
max-width: 100%;
}Counting from the end is useful when a list grows at the bottom and you want to style "the last three entries" without adding classes in your templates:
li:nth-last-of-type(-n + 3) {
color: #555;
}-n + 3 produces 3, 2, 1, 0, and so on. Because positions start at 1, this selects the last three items. The formula works the same way in :nth-of-type() and :nth-last-of-type(), but the count starts from a different end.
A worked example: a mixed-content article
Suppose a CMS emits headings, paragraphs, and figures in any order. You want the first paragraph after the title to look like a lead paragraph, and you want every figure to alternate sides.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Of-type demo</title>
<style>
article > p:first-of-type {
font-size: 1.2rem;
font-weight: 600;
}
article > figure:nth-of-type(odd) {
float: left;
margin-right: 1rem;
}
article > figure:nth-of-type(even) {
float: right;
margin-left: 1rem;
}
article > h2:only-of-type {
border-bottom: 2px solid #1a73e8;
}
</style>
</head>
<body>
<article>
<h2>Field notes</h2>
<figure>Photo one</figure>
<p>Lead paragraph.</p>
<p>Second paragraph.</p>
<figure>Photo two</figure>
<p>Third paragraph.</p>
</article>
</body>
</html>Here p:first-of-type still finds the lead paragraph even though a figure comes before it. With p:first-child the rule would fail, because the first child is the figure. The figure rules alternate sides without counting the paragraphs between them.
Common errors
Expecting a class to change the count
.note:nth-of-type(2) does not mean "the second element with class note". It means "an element with class note that is also the second of its tag name among its siblings." If the first two p elements have no class and the third has note, then p.note:nth-of-type(2) matches nothing, because the third p is in position 3.
/* Matches only if the .note element is itself the 2nd p */
p.note:nth-of-type(2) {
background: #fffbe6;
}To count only elements that match a selector, use the of S syntax of :nth-child(). Browser support is recent (Chrome 111, Firefox 113, Safari 9), so check your support targets first:
:nth-child(2 of .note) {
background: #fffbe6;
}Using `:first-of-type` when you meant "first in the document"
:first-of-type applies once per parent. A page with five article elements, each containing a p, has five matches for article p:first-of-type. If you want only one match on the page, add a class or use a more specific parent selector.
Forgetting that the type is the tag name
An h2 and an h3 are different types. section > :nth-of-type(2) with no type in front is allowed, but the count is still kept per tag name, so it can match more than one element. If a section contains an h2, a p, a second h2, and a second p, then :nth-of-type(2) matches both the second h2 and the second p. Whenever you want a single element, put the tag name in the selector.
Counting text
Text nodes and comments are not elements, so they never count. Whitespace between tags does not change positions. A <br> element is an element and does count toward :nth-child(), but it only counts toward :nth-of-type() for other br elements.
Dynamic changes
Positions are recalculated when the DOM changes. If a script inserts a new li at the top of a list, every li:nth-of-type(odd) assignment shifts. Striping that "jumps" on insert is expected behavior. It is not a bug.
When to choose which
- Use
:nth-child()when the structure is uniform, such as table rows or a list of identical items, or when you really want a position among all siblings. - Use
:nth-of-type()and:first-of-typewhen different tags are mixed under one parent and you care about the sequence of one tag. Content from a CMS or Markdown is the typical case. - Use
:nth-child(n of S)when the filter is a class or attribute rather than a tag name.
Specificity
Each of these pseudo-classes adds (0,1,0) to specificity, the same as a class. So p:nth-of-type(2) has (0,1,1): one class-level unit for the pseudo-class and one type-level unit for p. It beats a plain p rule and loses to p.note-style rules that add a class on top. A later chapter on specificity covers how the rest of the logical pseudo-classes behave.
Exercise
Given a parent with these children in order: <h3>, <p>, <p class="note">, <ul>, <p>. Which element(s) does p:nth-of-type(2) match, and which element(s) does p:nth-child(2) match?
Show answer
p:nth-of-type(2) matches the <p class="note">, because it is the second p among the siblings (the h3 and ul are not counted). p:nth-child(2) matches the first <p>, because it is the second child overall (after the h3). The <p class="note"> is the third child, and the last <p> is the fifth.
Exercise
Write one selector that styles the last two li elements of every ul, even if the ul also contains script or template elements between items.
Show answer
ul li:nth-last-of-type(-n + 2). It counts only li siblings, from the end, so other element types in the list do not affect the result. The formula -n + 2 yields 2 and 1.