Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

CSS Dictionary

  1. Home
  2. CSS Dictionary
  3. [CSS Selector] E:nth-child(n)

[CSS Selector] E:nth-child(n)

'selector:nth-child(n)' applies to the nth child element. Does not work in IE8 and below.

The difference from ':nth-of-type' lies in whether other element types are counted. For example, 'p:nth-of-type(2)' targets the 2nd p element regardless of other elements in between, whereas 'p:nth-child(2)' counts all sibling elements — so if the 2nd child is not a p element, the style is not applied.

Sample Code

div p:nth-child(1) { color: red;} /* Makes the 1st p element inside a div element red. */

Browser Display Result

div p:nth-child(2) { color: red;} /* Makes the 2nd p element inside a div element red. */
div.hoge p:nth-child(2) { color: red;} /* Not applied because the 2nd child inside the 'hoge' element is not a p element. */

<div>
	<p>This is the 1st p element.</p>
	<p>This is the 2nd p element.</p>
</div>
<div class="hoge">
	<p>This is the 1st p element.</p>
	<div>This is the 1st div element.</div>
	<p>This is the 2nd p element.</p>
</div>

Note that when combining id names, class names, or attributes with ':only-child', the browser first checks whether the element is the only child, and then checks whether it matches the specified element name, id name, class name, or attributes. For example, 'p.hoge:only-child' applies only when the sole child element is a p element AND it has the class name 'hoge'.

Using 'n' in nth Arguments

When 'n' is used as the argument value for ':nth-child', ':nth-last-child', ':nth-of-type', and ':nth-last-of-type', 'n' represents a sequence of natural numbers including 0. This is the same notation as '2n' for even numbers and '2n+1' for odd numbers in mathematics. Note that elements are counted starting from '1', but 'n' starts from '0'.

The following are examples. When written as '2n':

  • 'n = 0' => '2 * 0' -> Matches the 0th element
  • 'n = 1' => '2 * 1' -> Matches the 2nd element
  • 'n = 2' => '2 * 2' -> Matches the 4th element
  • 'n = 3' => '2 * 3' -> Matches the 6th element
  • 'n = 4' => '2 * 4' -> Matches the 8th element
  • And so on...

When written as '2n+1':

  • 'n = 0' => '2 * 0 + 1' -> Matches the 1st element
  • 'n = 1' => '2 * 1 + 1' -> Matches the 3rd element
  • 'n = 2' => '2 * 2 + 1' -> Matches the 5th element
  • 'n = 3' => '2 * 3 + 1' -> Matches the 7th element
  • 'n = 4' => '2 * 4 + 1' -> Matches the 9th element
  • And so on...

When written as '6n-3':

  • 'n = 0' => '6 * 0 - 3' -> Matches the -3rd element
  • 'n = 1' => '6 * 1 - 3' -> Matches the 3rd element
  • 'n = 2' => '6 * 2 - 3' -> Matches the 9th element
  • 'n = 3' => '6 * 3 - 3' -> Matches the 15th element
  • 'n = 4' => '6 * 4 - 3' -> Matches the 21st element
  • And so on...

When written as '-2n+3':

  • 'n = 0' => '-2 * 0 + 3' -> Matches the 3rd element
  • 'n = 1' => '-2 * 1 + 3' -> Matches the 1st element
  • 'n = 2' => '-2 * 2 + 3' -> Matches the -1st element
  • 'n = 3' => '-2 * 3 + 3' -> Matches the -3rd element
  • 'n = 4' => '-2 * 4 + 3' -> Matches the -5th element
  • And so on...

When written as 'n+3':

  • 'n = 0' => '1 * 0 + 3' -> Matches the 3rd element
  • 'n = 1' => '1 * 1 + 3' -> Matches the 4th element
  • 'n = 2' => '1 * 2 + 3' -> Matches the 5th element
  • 'n = 3' => '1 * 3 + 3' -> Matches the 6th element
  • 'n = 4' => '1 * 4 + 3' -> Matches the 7th element
  • And so on...

Since elements are counted starting from '1', values of '0' or below are ignored.

Browser Compatibility

Chrome Chrome
1+
Firefox Firefox
3.5+
2 and earlier ×
Safari Safari
3.1+
2 and earlier ×
Edge Edge
12+
Supported in all versions
IE IE
11
10
9
8 ×
7 ×
6 ×
Opera Opera
9.5+
8 and earlier ×
iOS Safari iOS Safari
2+
1 and earlier ×
Android Android Browser
4.4+
3 and earlier ×
Chrome Android Chrome Android
Latest
Same support as desktop
Firefox Android Firefox Android
Latest
Same support as desktop

※ Version data is based on MDN.

If you find any errors or copyright issues, please .