Revision 57389 of :nth-child

  • Revision slug: CSS/:nth-child
  • Revision title: :nth-child
  • Revision id: 57389
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment new page

Revision Content

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

Summary

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

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

The values <tt>a</tt> and <tt>b</tt> 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

<tt>tr:nth-child(2n+1)</tt>
Represents the odd rows of an HTML table.
<tt>tr:nth-child(odd)</tt>
Represents the odd rows of an HTML table.
<tt>tr:nth-child(2n)</tt>
Represents the even rows of an HTML table.
<tt>tr:nth-child(even)</tt>
Represents the even rows of an HTML table.
<tt>span:nth-child(0n+1)</tt>
Represents a span element which is the first child of its parent; this is the same as the {{ Cssxref(":first-child") }} selector.
<tt>span:nth-child(1)</tt>
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 <tt><i>a</i>n+<i>b</i>-1</tt> siblings before it in the document tree, for a given positive or zero value for <tt>n</tt>, and has a parent element.
</p><p>This can more clearly be described this way: the matching element is the <tt><i>b</i>th</tt> child of an element after all its children have been split into groups of <tt><i>a</i></tt> elements each.
</p><p>The values <tt><i>a</i></tt> and <tt><i>b</i></tt> 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(<i>a</i>n + <i>b</i>) { <i>style properties</i> }
</pre>
<h3 name="Examples"> Examples </h3>
<h4 name="Example_selectors">Example selectors</h4>
<dl><dt><tt>tr:nth-child(2n+1)</tt>
</dt><dd>Represents the odd rows of an HTML table.
</dd><dt><tt>tr:nth-child(odd)</tt>
</dt><dd>Represents the odd rows of an HTML table.
</dd><dt><tt>tr:nth-child(2n)</tt>
</dt><dd>Represents the even rows of an HTML table.
</dd><dt><tt>tr:nth-child(even)</tt>
</dt><dd>Represents the even rows of an HTML table.
</dd><dt><tt>span:nth-child(0n+1)</tt>
</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><tt>span:nth-child(1)</tt>
</dt><dd>Equivalent to the above.
</dd></dl>
<h4 name="Usage_example">Usage example</h4>
<pre>span:nth-child(2n+1)
{
    background-color: lime;
}
</pre>
<p>...where...
</p>
<pre>  &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 ...
<br>
</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