:nth-last-child Redirect 1


The :nth-last-child CSS pseudo-class matches an element that has an+b-1 siblings after it in the document tree, for a given positive or zero value for n, and has a parent element. See :nth-child for a more thorough description of the syntax of its argument.


element:nth-last-child(an + b) { style properties }


Example selectors

Matches the last four rows of an HTML table.
Matches the even elements in their parent element, starting at the last element and working backward.


This CSS ...

table {
  border: 1px solid blue;
tr:nth-last-child(-n+3) { /* the last 3 siblings */
  background-color: lime;

... with this HTML ...

      <td>First line</td>
       <td>Second line</td>
       <td>Third line</td>
       <td>Fourth line</td>
       <td>Fifth line</td>

... looks like :


Specification Status Comment
Selectors Level 4 Working Draft No change.
Selectors Level 3 Recommendation Initial definition.

Browser compatibility

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

See also


Document Tags and Contributors

 Contributors to this page: Sheppy
 Last updated by: Sheppy,