CSS3 :nth-of-type() Selector
Complete CSS Selector Reference Manual
Example
Specify the background color of the 2nd sibling element of the same type that each p element matches:
p:nth-of-type(2)
{
background:#ff0000;
}
Try it »
Definition and Usage
:nth-of-type(n) selector matches the nth sibling element of the same type.
n can be a number, a keyword, or a formula.
Tip: See :nth-child() selector. This selector matches the nth child element. Look at the :nth-child() .
Browser support
The numbers in the table indicate the first browser version that supports this property.
| Selector | |||||
|---|---|---|---|---|---|
| :nth-of-type() | 4.0 | 9.0 | 3.5 | 3.2 | 9.6 |
More examples
Example 1
Odd and even can be used as keywords to match child elements whose index is odd or even (the first child node has index 1).
Here we assign two different background colors to odd and even p elements:
p:nth-of-type(odd)
{
background:#ff0000;
}
p:nth-of-type(even)
{
background:#0000ff;
}
Try it »
Example 2
Using the formula (an+b). Description: a represents the size of a cycle, N is a counter (starting from 0), and b is the offset.
Here we specify a background color for all p elements whose index is a multiple of 3:
p:nth-of-type(3n+0)
{
background:#ff0000;
}
Try it »