:nth-last-of-type

The :nth-last-of-type(an+b) CSS pseudo-class matches an element that has an+b-1 siblings of the same type after it, where n is positive or zero. It is essentially the same as :nth-of-type, except it counts items backwards from the end, not the beginning.

/* Selects every fourth <div> inside a parent element */
/* Counting backwards from the last one */
div:nth-last-of-type(4n) {
  background-color: lime;
}

See :nth-child for a more thorough description of the syntax of its argument.

Syntax

:nth-last-of-type( <nth> )

where
<nth> = <an-plus-b> | even | odd

Example

HTML

<div>
  <span>This is a span.</span>
  <span>This is another span.</span>
  <em>This is emphasized.</em>
  <span>Wow, this span gets limed!!!</span>
  <strike>This is struck through.</strike>
  <span>Here is one last span.</span>
</div>

CSS

span:nth-last-of-type(2) {
  background-color: lime;
}

Result

Specifications

Specification Status Comment
Selectors Level 4
The definition of ':nth-last-of-type' in that specification.
Working Draft Matching elements are not required to have a parent.
Selectors Level 3
The definition of ':nth-last-of-type' in that specification.
Recommendation Initial definition.

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support 4.0 (Yes) 3.5 (1.9.1) 9.0 9.5 3.2
Feature Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 (Yes) 1.0 (1.9.1) 9.0 10.0 3.2

See also

Document Tags and Contributors

 Last updated by: chrisdavidmills,