:nth-of-type

:nth-of-type

The :nth-of-type(an+b) 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

:nth-of-type( <an-plus-b> ) { style properties }

where
<an-plus-b> = An+B | even | odd

Example

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>
    <span>This doesn't affect the count</span>
    <p>First paragraph (left aligned)</p>
    <p>Second paragraph (right aligned)</p>
    <span>Neither does this</span>
    <p>Third paragraph (left aligned)</p>
</div>

Specifications

Specification Status Comment
Selectors Level 4
The definition of ':nth-of-type' in that specification.
Working Draft No change
Selectors Level 3
The definition of ':nth-of-type' in that specification.
Recommendation Initial definition

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 (Yes) 3.5 (1.9.1) 9.0 9.5 3.1
Feature Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 (Yes) 1.0 (1.9.1) 9.0 9.5 3.1

See also

© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部