:nth-child

  • Revision slug: CSS/:nth-child
  • Revision title: :nth-child
  • Revision id: 57390
  • Created:
  • Creator: Miken32
  • Is current revision? No
  • Comment no wording changes

Revision Content

{{ CSSRef() }} {{ Fx_minversion_header("3") }}

Summary

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

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

See also

Revision Source

<p>{{ CSSRef() }} {{ Fx_minversion_header("3") }}</p>
<h3 name="Summary">Summary</h3>
<p>The <code>:nth-child</code> pseudo-class 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>
<h3 name="Syntax">Syntax</h3>
<pre class="eval">element:nth-child(<em>a</em>n + <em>b</em>) { <em>style properties</em> }
</pre>
<h3 name="Examples">Examples</h3>
<h4 name="Example_selectors">Example selectors</h4>
<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>
<h4 name="Usage_example">Usage example</h4>
<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>
<h3 name="See_also">See also</h3>
<ul> <li><a class="external" href="http://www.w3.org/TR/css3-selectors/#nth-child-pseudo">CSS3 Reference</a></li>
</ul>
Revert to this revision