CSS Reference ยท Pseudo-classes
The :nth-last-of-type() pseudo-class
Reference for the CSS :nth-last-of-type() pseudo-class: match elements by position among same-tag siblings, counted from the last one, with examples.
The :nth-last-of-type() pseudo-class combines two ideas: it counts only siblings with the same tag name (like :nth-of-type()), and it counts from the end (like :nth-last-child()).
Use it to style the last few paragraphs, images or list items of a container that also holds other kinds of elements, such as a closing note after a series of figures.
p:nth-last-of-type(1) { margin-bottom: 0; }
Try it
The footer is the last child, yet "Three" and "Four" are still the last two paragraphs and are highlighted.
Syntax
:nth-last-of-type( <nth> ) { } /* <nth> is one of: odd | even | <integer> | <an+b> */
- Specificity: (0, 1, 0).
- Position 1 is the last element of that type.
Values
| Syntax and arguments | Description |
|---|---|
1 |
The last element of its type. Same as :last-of-type. |
2 |
The second to last of its type. |
-n + 2 |
The last two of their type. |
n + 2 |
All but the last one. |
odd / even |
Odd or even positions counted from the end. |
Usage notes
- Always add a tag.
figure:nth-last-of-type(2)is readable; the bare form matches the second-to-last element of each tag at once. - Other tags are invisible to it. That is the difference from
:nth-last-child(), where any sibling shifts the count. - Removing trailing space. A common use is
p:nth-last-of-type(1)to drop the bottom margin of the last paragraph in a card that ends with a button or footer. - Classes do not create types. Two
<li>elements with different classes still belong to the same type. - No
of S. Theof Sfilter is only available on the-childvariants.
Browser support
Supported in every browser.
Related
:nth-of-type()โ counted from the start:nth-last-child()โ from the end, all siblings:last-of-type- Lesson: Structural Pseudo-classes