:nth-of-type() pseudo-classes
The :nth-of-type() and :nth-last-of-type() CSS functional pseudo-classes match elements based on their position among siblings of the same type. The :first-of-type, :last-of-type, and :only-of-type pseudo-classes match the first, last, and only elements of its type.
Overview
The :nth-of-type() and :nth-last-of-type() CSS functional pseudo-classes match elements based on their position among siblings of the same type. The :first-of-type, :last-of-type, and :only-of-type pseudo-classes match the first, last, and only elements of its type.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 1 | 12 | 3.5 | 3.1 | 18 | 2 | |
| Other | ||||||
| `:last-of-type` | 1 | 12 | 3.5 | 3.1 | 18 | 2 |
| `:nth-last-of-type()` | 4 | 12 | 3.5 | 3.1 | 18 | 2 |
| `:nth-of-type()` | 1 | 12 | 3.5 | 3.1 | 18 | 2 |
| `:only-of-type` | 1 | 12 | 3.5 | 3.1 | 18 | 2 |
- Before Edge 16, Microsoft Edge treats all unknown elements (such as custom elements) as the same element type.
- Before Edge 16, Microsoft Edge treats all unknown elements (such as custom elements) as the same element type.
- Before Edge 16, Microsoft Edge treats all unknown elements (such as custom elements) as the same element type.
- Before Edge 16, Microsoft Edge treats all unknown elements (such as custom elements) as the same element type.
- Before Edge 16, Microsoft Edge treats all unknown elements (such as custom elements) as the same element type.
Syntax
p:first-of-type {
font-size: 1.2em;
}
article p:nth-of-type(2) {
text-indent: 1em;
}
img:only-of-type {
display: block;
margin: 0 auto;
} Live demo
Use cases
Using :nth-of-type() pseudo-classes
The :nth-of-type() and :nth-last-of-type() CSS functional pseudo-classes match elements based on their position among siblings of the same type. The :first-of-type, :last-of-type, and :only-of-type pseudo-classes match the first, last, and only elements of its type.
Cautions
- May not be supported in older browsers.
Accessibility
- Make sure visual changes are conveyed appropriately to assistive technology.
Related links
Powered by web-features