:nth-child

  • Revision slug: Web/CSS/:nth-child
  • Revision title: :nth-child
  • Revision id: 494303
  • Created:
  • Creator: kscarfone
  • Is current revision? Yes
  • Comment Updated tags

Revision Content

{{CSSRef}}

Summary

The :nth-child(an+b) CSS pseudo-class matches an element that has an+b-1 siblings before it in the document tree, for a given positive or zero value for n, and has a parent element.

This can more clearly be described this way: the matching element is the bth child of an element after all its children have been split into groups of a elements each.

The values a and b must both be integers, and the index of an element's first child is 1.

In other words, this class matches all children whose index fall in the set { an + b; n = 0, 1, 2, ... }.

Among other things, this allows selectors to match every other row in a table.

Syntax

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

Examples

Example selectors

tr:nth-child(2n+1)
Represents the odd rows of an HTML table.
tr:nth-child(odd)
Represents the odd rows of an HTML table.
tr:nth-child(2n)
Represents the even rows of an HTML table.
tr:nth-child(even)
Represents the even rows of an HTML table.
span:nth-child(0n+1)
Represents a span element which is the first child of its parent; this is the same as the {{Cssxref(":first-child")}} selector.
span:nth-child(1)
Equivalent to the above.
span:nth-child(-n+3)
Matches if the element is one of the first three children of its parent and also a span.

Odd Selector Example

The following HTML...

    <div>
      <span>This span is limed!</span>
      <span>This span is not. :(</span>
      <em>This one is odd. </em>
      <span>Sadly, this one is not...</span>
      <span>But this one is!</span>
    </div>

...using this CSS...

    span:nth-child(2n+1) {
      background-color: lime;
    }

...will result in:

{{EmbedLiveSample('Odd_Selector_Example','100%', '60')}}

Specifications

Specification Status Comment
{{SpecName('CSS4 Selectors', '#nth-child-pseudo', ':nth-child')}} {{Spec2('CSS4 Selectors')}} No change.
{{SpecName('CSS3 Selectors', '#nth-child-pseudo', ':nth-child')}} {{Spec2('CSS3 Selectors')}} Initial definition.

Browser compatibility

{{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

Notes

  • Opera can't handle dynamic insertion of elements.

Revision Source

<div>
 {{CSSRef}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code>:nth-child(an+b)</code> <a href="/en-US/docs/CSS" title="CSS">CSS</a> <a href="/en-US/docs/CSS/Pseudo-classes" title="Pseudo-classes">pseudo-class</a> matches an element that has <code><em>a</em>n+<em>b</em>-1</code> siblings before it in the document tree, for a given positive or zero value for <code>n</code>, and has a parent element.</p>
<p>This can more clearly be described this way: the matching element is the <code><em>b</em>th</code> child of an element after all its children have been split into groups of <code><em>a</em></code> elements each.</p>
<p>The values <code><em>a</em></code> and <code><em>b</em></code> must both be integers, and the index of an element's first child is 1.</p>
<p>In other words, this class matches all children whose index fall in the set { an + b; n = 0, 1, 2, ... }.</p>
<p>Among other things, this allows selectors to match every other row in a table.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
element:nth-child(<em>a</em>n + <em>b</em>) { <em>style properties</em> }
</pre>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example_selectors" name="Example_selectors">Example selectors</h3>
<dl>
 <dt>
  <code>tr:nth-child(2n+1)</code></dt>
 <dd>
  Represents the odd rows of an HTML table.</dd>
 <dt>
  <code>tr:nth-child(odd)</code></dt>
 <dd>
  Represents the odd rows of an HTML table.</dd>
 <dt>
  <code>tr:nth-child(2n)</code></dt>
 <dd>
  Represents the even rows of an HTML table.</dd>
 <dt>
  <code>tr:nth-child(even)</code></dt>
 <dd>
  Represents the even rows of an HTML table.</dd>
 <dt>
  <code>span:nth-child(0n+1)</code></dt>
 <dd>
  Represents a span element which is the first child of its parent; this is the same as the {{Cssxref(":first-child")}} selector.</dd>
 <dt>
  <code>span:nth-child(1)</code></dt>
 <dd>
  Equivalent to the above.</dd>
 <dt>
  <code>span:nth-child(-n+3)</code></dt>
 <dd>
  Matches if the element is one of the first three children of its parent and also a span.</dd>
</dl>
<h3 id="Odd_Selector_Example" name="Odd_Selector_Example">Odd Selector Example</h3>
<p id="The_following_HTML...">The following HTML...</p>
<pre class="brush:html">
    &lt;div&gt;
      &lt;span&gt;This span is limed!&lt;/span&gt;
      &lt;span&gt;This span is not. :(&lt;/span&gt;
      &lt;em&gt;This one is odd. &lt;/em&gt;
      &lt;span&gt;Sadly, this one is not...&lt;/span&gt;
      &lt;span&gt;But this one is!&lt;/span&gt;
    &lt;/div&gt;
</pre>
<p id="...using_this_CSS...">...using this CSS...</p>
<pre class="brush:css; highlight:[1]">
    span:nth-child(2n+1) {
      background-color: lime;
    }
</pre>
<p id="...will_result_in.3A">...will result in:</p>
<div>
 {{EmbedLiveSample('Odd_Selector_Example','100%', '60')}}</div>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS4 Selectors', '#nth-child-pseudo', ':nth-child')}}</td>
   <td>{{Spec2('CSS4 Selectors')}}</td>
   <td>No change.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS3 Selectors', '#nth-child-pseudo', ':nth-child')}}</td>
   <td>{{Spec2('CSS3 Selectors')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</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</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="Notes">Notes</h2>
<ul>
 <li>Opera can't handle dynamic insertion of elements.</li>
</ul>
Revert to this revision