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.

Version

CSS3

Syntax

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

Example

<!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

<!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>



Related articles