Skip to content

CSS Selectors

Position nth-last-of-type()

About

The :nth-last-of-type() pseudo-class matches elements based on their position among siblings of the same type.

Last of each type

The selector :nth-last-of-type(1) matches the last div and the last p element, counting each type separately from the end.

Second to last paragraph

The selector p:nth-last-of-type(2) matches the second to last p element, ignoring other element types when counting.

  • Change the selector to match only the third to last paragraph

All except the last two paragraphs

The selector p:nth-last-of-type(n + 3) matches all paragraphs except the last two, counting only p elements from the end.

  • n = 0 → (n + 3) = 3 (3rd from end)
  • n = 1 → (n + 3) = 4 (4th from end)
  • n = 2 → (n + 3) = 5 (5th from end)
  • Change the selector to match all paragraphs except the last one