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