CSS :first-of-type Pseudo Class

The CSS :first-of-type pseudo-class selects an element that is the first element of its type in the list of children of its parent.

The :first-of-type pseudo-class is the same as the :nth-of-type.

Version

CSS3

Syntax

:first-of-type {
  css declarations;
}

Example

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

Practice Your Knowledge

The :first-of-type CSS pseudo-class selects the element if




Related articles