:nth-child

  • Revision slug: CSS/:nth-child
  • Revision title: :nth-child
  • Revision id: 290595
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

{{ CSSRef() }}

Summary

The :nth-child 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.

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)
Three first span elements.

Usage example

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

...where...

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

... should look like ...

   This span is limed!
   This span is not. :(
   But this one is!
   Sadly, this one is not...

Specifications

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

<p>{{ CSSRef() }}</p>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code>:nth-child</code> <a href="/en-US/docs/CSS" title="/en-US/docs/CSS">CSS</a> <a href="/en/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>Among other things, this allows selectors to match every other row in a table.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="eval">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>
</dl>
<dl>
  <dt>
    <code>span:nth-child(-n+3)</code></dt>
  <dd>
    Three first span elements.</dd>
</dl>
<p>Usage example</p>
<pre class="brush: css">span:nth-child(2n+1)
{
    background-color: lime;
}
</pre>
<p>...where...</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;span&gt;But this one is!&lt;/span&gt;
    &lt;span&gt;Sadly, this one is not...&lt;/span&gt;
  &lt;/div&gt;
</pre>
<p>... should look like ...</p>
<div>
  <pre class="eval">   <span style="background-color: lime;">This span is limed!</span>
   <span>This span is not. :(</span>
   <span style="background-color: lime;">But this one is!</span>
   <span>Sadly, this one is not...</span>
</pre>
</div>
<h2 id="Specifications">Specifications</h2>
<ul>
  <li><a class="external" href="http://www.w3.org/TR/css3-selectors/#nth-child-pseudo">CSS3 </a></li>
</ul>
<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