CSS :nth-last-of-type() Pseudo Class

The :nth-last-of-type() pseudo-class selects elements based on their index starting from the last element upwards.

The:nth-last-of-type() can be specified by a number, a keyword, or a formula.

Version

CSS3

Syntax

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

Example

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style> 
      p:nth-last-of-type(3) {
      background: #8ebf42;
      }
    </style>
  </head>
  <body>
    <h2>:nth-last-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>
  </body>
</html>

Another example where "odd" and "even" keywords are used:

Example

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style> 
      p:nth-last-of-type(odd) {
      background: #1c87c9;
      color:#eeeeee;
      }
      p:nth-last-child(even) {
      background: #666666;
      color:#eeeeee;
      }
    </style>
  </head>
  <body>
    <h2>:nth-last-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>
  </body>
</html>

Browser support

4.0+ 12.0+ 3.5+ 3.2+ 10.0+

Practice Your Knowledge

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




Related articles