nth-of-type property CSS Reference



Definition and Usage

The :nth-of-type CSS pseudo-class matches an element that has an+b-1 siblings with the same element name before it in the document tree, for a given positive or zero value for n, and has a parent element. See :nth-child for a more thorough description of the syntax of its argument. This is a more flexible and useful pseudo selector if you want to ensure you're selecting the same type of tag no matter where it is inside the parent element, or what other different tags appear before it.


Syntax

element:nth-of-type(an + b) { style properties }

Examples

This example causes the text alignment of paragraphs to alternate between left and right.

CSS Content

p:nth-of-type(2n+1) { text-align: left; }
p:nth-of-type(2n) { text-align: right; }

HTML Content

<div>
    <p>First paragraph (left aligned)</p>
    <p>Second paragraph (right aligned)</p>
    <p>Third paragraph (left aligned)</p>
</div>

Compatibility

Desktop browsers

FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support1.03.5 (1.9.1)9.09.53.1

Mobile browsers

FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support2.11.0 (1.9.1)9.09.53.1

Relative articles