CSS pseudo-classPseudo-classes

:nth-of-type()

Selects elements by position among siblings with the same tag, for example every other image or the third paragraph.

On this page

Example

#
CSS
.chat p:nth-of-type(odd) {
  justify-self: start;
  background: #27272a;
}

.chat p:nth-of-type(even) {
  justify-self: end;
  background: #7c3aed;
}
Preview

The timestamps in time elements sit between the messages, but they don’t disturb the alternating layout, since only the paragraphs are counted.

Definition and usage

#

The :nth-of-type() pseudo-class works like :nth-child(), but only counts siblings with the same tag. In an article where paragraphs alternate with images, p:nth-of-type(2) points to the second paragraph, even if three images come before it.

It takes the same arguments: a number, odd, even, and the An+B formula. The argument-free versions are :first-of-type and :last-of-type, and counting from the end is done with :nth-last-of-type().

Important: "type" means the tag name, not the class. The selector .card:nth-of-type(2) doesn’t select the second card. It selects the element with the class card that is the second element of its tag (e.g. the second div) within the parent. To count by class, use :nth-child(2 of .card).

Syntax

#
Syntax
:nth-of-type( <number> | odd | even | An+B )

Arguments

#
  • 2

    span 2

    The second span, even though it’s the third child of the parent. em elements aren’t counted.
  • odd

    em 1, em 3

    The odd-numbered em elements: the first and third. Each tag has its own count.
  • even

    span 2, span 4

    The even-numbered span elements, with no regard for the em elements standing between them.
  • n+3

    span 3, span 4

    From the third span element to the last.
  • :nth-child(2)

    nothing

    For comparison, span:nth-child(2) selects nothing, because the parent’s second child is an em.

Arguments in practice

#

2

The second span, even though it’s the third child of the parent. em elements aren’t counted.
span:nth-of-type(2) { background: #7c3aed; color: #fff; }

odd

The odd-numbered em elements: the first and third. Each tag has its own count.
em:nth-of-type(odd) { background: #7c3aed; color: #fff; }

even

The even-numbered span elements, with no regard for the em elements standing between them.
span:nth-of-type(even) { background: #7c3aed; color: #fff; }

n+3

From the third span element to the last.
span:nth-of-type(n+3) { background: #7c3aed; color: #fff; }

:nth-child(2)

For comparison, span:nth-child(2) selects nothing, because the parent’s second child is an em.
span:nth-child(2) { background: #7c3aed; color: #fff; }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Use :nth-of-type() when different tags are mixed within the parent, for example paragraphs, images, and headings.
  • Don’t combine it with a class hoping it will count classes. It only counts tags, so for classes use :nth-child(… of .class).

Related entries

#

Spotted a mistake or missing an example? Write to us.