Skip to content

CSS Selectors

Position nth-last-child()

About

The :nth-last-child() pseudo-class matches elements based on their position among a group of siblings, counting from the end.

Second from the end

The selector :nth-last-child(2) matches the second to last element among its siblings.

Last three elements

The selector :nth-last-child(-n + 3) matches the last three elements, counting from the end.

  • n = 0 → (-n + 3) = (-0 + 3) = 3
  • n = 1 → (-n + 3) = (-1 + 3) = 2
  • n = 2 → (-n + 3) = (-2 + 3) = 1
  • n = 3 → (-n + 3) = (-3 + 3) = 0 (no match)
  • Change the selector to only match the last two elements

Every third element from the end

The selector :nth-last-child(3n) matches every third element, counting from the end.