--- title: ':nth-child' slug: 'Web/CSS/:nth-child' tags: - ':nth-child' - CSS - CSS 伪类 translation_of: 'Web/CSS/:nth-child' ---
:nth-child(an+b)
这个 CSS 伪类首先找到所有当前元素的兄弟元素,然后按照位置先后顺序从1开始排序,选择的结果为CSS伪类:nth-child括号中表达式(an+b)匹配到的元素集合(n=0,1,2,3...)。示例:
0n+3
或简单的 3
匹配第三个元素。1n+0
或简单的 n
匹配每个元素。(兼容性提醒:在 Android 浏览器 4.3 以下的版本 n
和 1n
的匹配方式不一致。1n
和 1n+0
是一致的,可根据喜好任选其一来使用。)2n+0
或简单的 2n
匹配位置为 2、4、6、8...的元素(n=0时,2n+0=0,第0个元素不存在,因为是从1开始排序)。你可以使用关键字 even
来替换此表达式。2n+1
匹配位置为 1、3、5、7...的元素。你可以使用关键字 odd
来替换此表达式。3n+4
匹配位置为 4、7、10、13...的元素。a
和 b
都必须为整数,并且元素的第一个子元素的下标为 1。换言之就是,该伪类匹配所有下标在集合 { an + b; n = 0, 1, 2, ...} 中的子元素。另外需要特别注意的是,an
必须写在 b
的前面,不能写成 b+an
的形式。
{{csssyntax}}
tr:nth-child(2n+1)
tr:nth-child(odd)
tr:nth-child(2n)
tr:nth-child(even)
span:nth-child(0n+1)
span:nth-child(1)
span:nth-child(-n+3)
<h3><code>span:nth-child(2n+1)</code>, WITHOUT an <code><em></code> among the child elements.</h3> <p>Children 1, 3, 5, and 7 are selected.</p> <div class="first"> <span>Span 1!</span> <span>Span 2</span> <span>Span 3!</span> <span>Span 4</span> <span>Span 5!</span> <span>Span 6</span> <span>Span 7!</span> </div> <br> <h3><code>span:nth-child(2n+1)</code>, WITH an <code><em></code> among the child elements.</h3> <p>Children 1, 5, and 7 are selected.<br> 3 is used in the counting because it is a child, but it isn't selected because it isn't a <code><span></code>.</p> <div class="second"> <span>Span!</span> <span>Span</span> <em>This is an `em`.</em> <span>Span</span> <span>Span!</span> <span>Span</span> <span>Span!</span> <span>Span</span> </div> <br> <h3><code>span:nth-of-type(2n+1)</code>, WITH an <code><em></code> among the child elements.</h3> <p>Children 1, 4, 6, and 8 are selected.<br> 3 isn't used in the counting or selected because it is an <code><em></code>, not a <code><span></code>, and <code>nth-of-type</code> only selects children of that type. The <code><em></code> is completely skipped over and ignored.</p> <div class="third"> <span>Span!</span> <span>Span</span> <em>This is an `em`.</em> <span>Span!</span> <span>Span</span> <span>Span!</span> <span>Span</span> <span>Span!</span> </div>
html { font-family: sans-serif; } span, div em { padding: 5px; border: 1px solid green; display: inline-block; margin-bottom: 3px; } .first span:nth-child(2n+1), .second span:nth-child(2n+1), .third span:nth-of-type(2n+1) { background-color: lime; }
{{EmbedLiveSample('Detailed_example', 550, 550)}}
规范 | 状态 | 备注 |
---|---|---|
{{SpecName('CSS4 Selectors', '#nth-child-pseudo', ':nth-child')}} | {{Spec2('CSS4 Selectors')}} | 未变化。 |
{{SpecName('CSS3 Selectors', '#nth-child-pseudo', ':nth-child')}} | {{Spec2('CSS3 Selectors')}} | 初始化定义。 |
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 1.0 | {{CompatGeckoDesktop("1.9.1")}} | 9.0 | 9.5 | 3.1 |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | 2.1 | {{CompatGeckoMobile("1.9.1")}} | 9.0 | 9.5 | 3.1 |