:nth-of-type

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

Revision Content

{{ CSSRef() }}

Summary

The :nth-of-type CSS pseudo-class matches an element that has an+b-1 siblings with the same element name before it in the document tree, for a given positive or zero value for n, and has a parent element. See {{ Cssxref(":nth-child") }} for a more thorough description of the syntax of its argument. This is a more flexible and useful pseudo selector if you want to ensure you're selecting the same type of tag no matter where it is inside the parent element, or what other different tags appear before it.

Syntax

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

Example

This example causes the text alignment of paragraphs to alternate between left and right.

CSS Content

p:nth-of-type(2n+1) { text-align: left; }
p:nth-of-type(2n) { text-align: right; }

HTML Content

<div>
    <p>First paragraph (left aligned)</p>
    <p>Second paragraph (right aligned)</p>
    <p>Third paragraph (left aligned)</p>
</div>
{{ EmbedLiveSample('Example') }}

Specifications

Specification Status Comment
{{ SpecName('CSS4 Selectors', '#nth-of-type-pseudo', ':nth-of-type') }} {{ Spec2('CSS4 Selectors') }} No change.
{{ SpecName('CSS3 Selectors', '#nth-of-type-pseudo', ':nth-of-type') }} {{ 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

See also

  • {{ Cssxref(":nth-child") }}

Revision Source

<p>{{ CSSRef() }}</p>
<h2 id="Summary">Summary</h2>
<p>The <code>:nth-of-type</code> CSS <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 with the same element name before it in the document tree, for a given positive or zero value for <code>n</code>, and has a parent element. See {{ Cssxref(":nth-child") }} for a more thorough description of the syntax of its argument. This is a more flexible and useful pseudo selector if you want to ensure you're selecting the same type of tag no matter where it is inside the parent element, or what other different tags appear before it.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="eval">
element:nth-of-type(<em>a</em>n + <em>b</em>) { <em>style properties</em> }
</pre>
<h2 id="Example" name="Example">Example</h2>
<p>This example causes the text alignment of paragraphs to alternate between left and right.</p>
<h3 id="CSS_Content">CSS Content</h3>
<pre class="brush: css">
p:nth-of-type(2n+1) { text-align: left; }
p:nth-of-type(2n) { text-align: right; }</pre>
<h3 id="HTML_Content">HTML Content</h3>
<pre class="brush: html">
&lt;div&gt;
&nbsp;&nbsp;&nbsp; &lt;p&gt;First paragraph (left aligned)&lt;/p&gt;
&nbsp;&nbsp;&nbsp; &lt;p&gt;Second paragraph (right aligned)&lt;/p&gt;
&nbsp;&nbsp;&nbsp; &lt;p&gt;Third paragraph (left aligned)&lt;/p&gt;
&lt;/div&gt;</pre>
<div>
 {{ EmbedLiveSample('Example') }}</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-of-type-pseudo', ':nth-of-type') }}</td>
   <td>{{ Spec2('CSS4 Selectors') }}</td>
   <td>No change.</td>
  </tr>
  <tr>
   <td>{{ SpecName('CSS3 Selectors', '#nth-of-type-pseudo', ':nth-of-type') }}</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="See_also" name="See_also">See also</h2>
<ul>
 <li>{{ Cssxref(":nth-child") }}</li>
</ul>
Revert to this revision