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-of-type(n)

[CSS Selector] E:nth-of-type(n)

Using 'selector:nth-of-type', you can apply styles to the nth child element with the same element name. This is generally called a 'pseudo-class'. Does not work in IE8 and below.

The difference from ':nth-child' lies in whether other element types are counted. For example, 'p:nth-child(2)' is not applied if the 2nd child element is not a p element.

Sample Code

style.css
/* 2nd p element inside a div */
div p:nth-of-type(2) { color: red;}

/* Even-numbered p elements */
p:nth-of-type(even) { background-color: #f5f5f5;}

/* Odd-numbered li elements */
li:nth-of-type(odd) { color: #3498db;}

/* Every 3rd li element */
li:nth-of-type(3n) { font-weight: bold;}

/* Hide img elements from the 4th onward */
img:nth-of-type(n+4) { display: none;}

Browser Display Result

div p:nth-of-type(2) { color: red;} /* Makes the 2nd p element inside a div element red. */

<div>
	<p>This is a p element.</p>
	<div>This is a div element.</div>
	<p>This is a p element.</p>
	<div>This is a div element.</div>
</div>

To target all nth child elements of every type, write '*:nth-of-type(n)' or ':nth-of-type(n)'. In this case, elements are counted separately per element type among all child elements. For example, if both p and div elements are present as children, the nth p element and the nth div element will each receive the style.

div :nth-of-type(2) { color: red;} /* Makes the 2nd child element of each type inside a div red. */

<div>
	<div>This is a div element.</div>
	<p>This is a p element.</p>
	<div>This is a div element.</div>
	<p>This is a p element.</p>
</div>

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