X

Headline

This is an important message requiring you to make a choice if you're based in the EU.

CSS :nth-of-type() Pseudo Class

The :nth-of-type() pseudo-class selects the elements of the same type based on their index.

The :nth-of-type() can be specified by a number, a keyword, or a formula. The :nth-of-type selector is similar to :nth-child but there is a difference: it is more specific. The :nth-of-type targets a certain type of an element in the arrangement only in relation to similar siblings.

Version

Selectors Level 4

Selectors Level 3

Syntax

:nth-of-type(number) {
  css declarations;
}

Example of the :nth-of-type() selector:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style> 
      p:nth-of-type(3) {
      background: #8ebf42;
      }
    </style>
  </head>
  <body>
    <h2>:nth-of-type() selector example</h2>
    <p>Paragraph 1</p>
    <p>Paragraph 2</p>
    <p>Paragraph 3</p>
  </body>
</html>

In the following example the :nth-of-type() is specified as "odd" and "even":

Example of :nth-of-type specified as "odd" and "even":

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style> 
      p:nth-of-type(odd) {
      background: #1c87c9;
      }
      p:nth-of-type(even) {
      background: #8ebf42;
      }
    </style>
  </head>
  <body>
    <h2>nth-of-type() selector example</h2>
    <p>Paragraph 1</p>
    <p>Paragraph 2</p>
    <p>Paragraph 3</p>
    <p>Paragraph 4</p>
    <p>Paragraph 5</p>
    <p>Paragraph 6</p>
    <p>Paragraph 7</p>
    <p>Paragraph 8</p>
    <p>Paragraph 9</p>
    <p>Paragraph 10</p>
  </body>
</html>

Browser support

4.0+ 12.0+ 3.5+ 3.2+ 10.0+

Practice Your Knowledge

The :nth-of-type() pseudo-class selects the elements



Do you find this helpful?

Related articles