Zum Inhalt springen

CSS :nth-of-type()-Pseudoklasse

Die Pseudo-Klasse :nth-of-type() wählt Elemente desselben Typs anhand ihres Index aus.

Die :nth-of-type()-Pseudo-Klasse kann mit einer Zahl, einem Schlüsselwort oder einer Formel angegeben werden. Der :nth-of-type()-Selektor ähnelt :nth-child, aber es gibt einen Unterschied: Er ist spezifischer. :nth-of-type() zielt in einer Anordnung nur in Bezug auf gleichartige Geschwister auf einen bestimmten Typ eines Elements ab.

Version

Selectors Level 4

Selectors Level 3

Syntax

CSS :nth-of-type()-Syntax

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

Beispiel des :nth-of-type()-Selektors:

CSS :nth-of-type()-Codebeispiel

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

Beispiel von :nth-of-type, angegeben als "odd" und "even":

CSS :nth-of-type()-weiteres Codebeispiel

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

Practice

Was trifft der CSS-Selektor :nth-of-type()?

Finden Sie das nützlich?

Dual-run-Vorschau — vergleichen Sie mit den Symfony-Routen live.