---
title: ':nth-child'
slug: 'Web/CSS/:nth-child'
tags:
  - ':nth-child'
  - CSS
  - CSS 伪类
translation_of: 'Web/CSS/:nth-child'
---
<div>{{CSSRef}}</div>

<h2 id="Summary" name="Summary">概述</h2>

<p><strong><code>:nth-child(an+b)</code></strong> 这个 <a href="/en-US/docs/CSS/Pseudo-classes">CSS 伪类</a>首先找到所有当前元素的兄弟元素,然后按照位置先后顺序从1开始排序,选择的结果为CSS伪类:nth-child括号中表达式(an+b)匹配到的元素集合(n=0,1,2,3...)。示例:</p>

<ul>
 <li><code>0n+3</code> 或简单的 <code>3</code> 匹配第三个元素。</li>
 <li><code>1n+0</code> 或简单的 <code>n</code> 匹配每个元素。(兼容性提醒:在 Android 浏览器 4.3 以下的版本 <code>n</code> 和 <code>1n</code> 的匹配方式不一致。<code>1n</code> 和 <code>1n+0</code> 是一致的,可根据喜好任选其一来使用。)</li>
 <li><code>2n+0</code> 或简单的 <code>2n</code> 匹配位置为 2、4、6、8...的元素(n=0时,2n+0=0,第0个元素不存在,因为是从1开始排序)。你可以使用关键字 <strong><code>even</code></strong> 来替换此表达式。</li>
 <li><code>2n+1</code> 匹配位置为 1、3、5、7...的元素。你可以使用关键字 <strong><code>odd</code></strong> 来替换此表达式。</li>
 <li><code>3n+4</code> 匹配位置为 4、7、10、13...的元素。</li>
</ul>

<p><code><em>a</em></code> 和 <code><em>b</em></code> 都必须为整数,并且元素的第一个子元素的下标为 1。换言之就是,该伪类匹配所有下标在集合 { an + b; n = 0, 1, 2, ...} 中的子元素。另外需要特别注意的是,<code><em>an</em></code> 必须写在 <code><em>b</em></code> 的前面,不能写成 <code><em>b+an</em></code> 的形式。</p>

<h2 id="Syntax" name="Syntax">语法</h2>

{{csssyntax}}

<h2 id="示例">示例</h2>

<h3 id="选择器示例">选择器示例</h3>

<dl>
 <dt><code>tr:nth-child(2n+1)</code></dt>
 <dd>表示HTML表格中的奇数行。</dd>
 <dt><code>tr:nth-child(odd)</code></dt>
 <dd>表示HTML表格中的奇数行。</dd>
 <dt><code>tr:nth-child(2n)</code></dt>
 <dd>表示HTML表格中的偶数行。</dd>
 <dt><code>tr:nth-child(even)</code></dt>
 <dd>表示HTML表格中的偶数行。</dd>
 <dt><code>span:nth-child(0n+1)</code></dt>
 <dd>表示子元素中第一个且为span的元素,与 <span style="background-color: #ffffff; color: #4d4e53; font-family: open sans,arial,sans-serif; font-size: 14px; line-height: 21px;">{{Cssxref(":first-child")}} </span>选择器作用相同。</dd>
 <dt><code>span:nth-child(1)</code></dt>
 <dd>表示父元素中子元素为第一的并且名字为span的标签被选中</dd>
 <dt><code>span:nth-child(-n+3)</code></dt>
 <dd>匹配前三个子元素中的span元素。</dd>
</dl>

<h3 id="Detailed_example">Detailed example</h3>

<h4 id="HTML">HTML</h4>

<pre class="brush:html">&lt;h3&gt;&lt;code&gt;span:nth-child(2n+1)&lt;/code&gt;, WITHOUT an
   &lt;code&gt;&amp;lt;em&amp;gt;&lt;/code&gt; among the child elements.&lt;/h3&gt;
&lt;p&gt;Children 1, 3, 5, and 7 are selected.&lt;/p&gt;
&lt;div class="first"&gt;
  &lt;span&gt;Span 1!&lt;/span&gt;
  &lt;span&gt;Span 2&lt;/span&gt;
  &lt;span&gt;Span 3!&lt;/span&gt;
  &lt;span&gt;Span 4&lt;/span&gt;
  &lt;span&gt;Span 5!&lt;/span&gt;
  &lt;span&gt;Span 6&lt;/span&gt;
  &lt;span&gt;Span 7!&lt;/span&gt;
&lt;/div&gt;

&lt;br&gt;

&lt;h3&gt;&lt;code&gt;span:nth-child(2n+1)&lt;/code&gt;, WITH an
   &lt;code&gt;&amp;lt;em&amp;gt;&lt;/code&gt; among the child elements.&lt;/h3&gt;
&lt;p&gt;Children 1, 5, and 7 are selected.&lt;br&gt;
   3 is used in the counting because it is a child, but it isn't
   selected because it isn't a &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;div class="second"&gt;
  &lt;span&gt;Span!&lt;/span&gt;
  &lt;span&gt;Span&lt;/span&gt;
  &lt;em&gt;This is an `em`.&lt;/em&gt;
  &lt;span&gt;Span&lt;/span&gt;
  &lt;span&gt;Span!&lt;/span&gt;
  &lt;span&gt;Span&lt;/span&gt;
  &lt;span&gt;Span!&lt;/span&gt;
  &lt;span&gt;Span&lt;/span&gt;
&lt;/div&gt;

&lt;br&gt;

&lt;h3&gt;&lt;code&gt;span:nth-of-type(2n+1)&lt;/code&gt;, WITH an
   &lt;code&gt;&amp;lt;em&amp;gt;&lt;/code&gt; among the child elements.&lt;/h3&gt;
&lt;p&gt;Children 1, 4, 6, and 8 are selected.&lt;br&gt;
   3 isn't used in the counting or selected because it is an &lt;code&gt;&amp;lt;em&amp;gt;&lt;/code&gt;,
   not a &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;, and &lt;code&gt;nth-of-type&lt;/code&gt; only selects
   children of that type. The &lt;code&gt;&amp;lt;em&amp;gt;&lt;/code&gt; is completely skipped
   over and ignored.&lt;/p&gt;
&lt;div class="third"&gt;
  &lt;span&gt;Span!&lt;/span&gt;
  &lt;span&gt;Span&lt;/span&gt;
  &lt;em&gt;This is an `em`.&lt;/em&gt;
  &lt;span&gt;Span!&lt;/span&gt;
  &lt;span&gt;Span&lt;/span&gt;
  &lt;span&gt;Span!&lt;/span&gt;
  &lt;span&gt;Span&lt;/span&gt;
  &lt;span&gt;Span!&lt;/span&gt;
&lt;/div&gt;
</pre>

<h4 id="CSS">CSS</h4>

<pre class="brush: css">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;
}</pre>

<h4 id="最终效果:">最终效果:</h4>

<p>{{EmbedLiveSample('Detailed_example', 550, 550)}}</p>



<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS4 Selectors', '#nth-child-pseudo', ':nth-child')}}</td>
   <td>{{Spec2('CSS4 Selectors')}}</td>
   <td>未变化。</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS3 Selectors', '#nth-child-pseudo', ':nth-child')}}</td>
   <td>{{Spec2('CSS3 Selectors')}}</td>
   <td>初始化定义。</td>
  </tr>
 </tbody>
</table>

<h2 id="兼容性">兼容性</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1.0</td>
   <td>{{CompatGeckoDesktop("1.9.1")}}</td>
   <td>9.0</td>
   <td>9.5</td>
   <td>3.1<span style="font-size: 14px; line-height: 1.5;"> </span></td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>2.1</td>
   <td>{{CompatGeckoMobile("1.9.1")}}</td>
   <td>9.0</td>
   <td>9.5</td>
   <td>3.1</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="注意">注意</h2>

<ul>
 <li>Opera 不能处理动态插入的元素。</li>
</ul>

<h2 id="参见">参见</h2>

<ul>
 <li>{{ Cssxref(":nth-of-type") }}, {{ Cssxref(":nth-last-child") }}</li>
</ul>