CSS - :nth-last-child
The :nth-last-child(an+b)
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.
Examples
Example selectors
tr:nth-last-child(-n+4)
- Matches the last four rows of an HTML table.
span:nth-last-child(even)
- Matches the even elements in their parent element, starting at the last element and working backward.
Usage
This CSS ...
table { border: 1px solid blue; } tr:nth-last-child(-n+3) { /* the last 3 siblings */ background-color: lime; }
... with this HTML ...
<table> <tbody> <tr> <td>First line</td> </tr> <tr> <td>Second line</td> </tr> <tr> <td>Third line</td> </tr> <tr> <td>Fourth line</td> </tr> <tr> <td>Fifth line</td> </tr> </tbody> </table>
... looks like :
Syntax
:nth-last-child( <a href="css/:nth-last-child#an-plus-b"><an-plus-b></a> <a title="Brackets" href="css/value_definition_syntax#brackets">[</a> of <selector><a title="Hash mark" href="css/value_definition_syntax#hash_mark_(.23)">#</a> <a title="Brackets" href="css/value_definition_syntax#brackets">]</a><a title="Question mark" href="css/value_definition_syntax#question_mark_(.3f)">?</a> ) <a title="Curly braces" href="css/value_definition_syntax#curly_braces_(.7b_.7d)">{</a> <var>style properties</var> <a title="Curly braces" href="css/value_definition_syntax#curly_braces_(.7b_.7d)">}</a><p>where <br><code><an-plus-b> = <var>A</var>n<a title="Plus" href="css/value_definition_syntax#plus_(.2b)">+</a><var>B</var> <a title="Single bar" href="css/value_definition_syntax#single_bar">|</a> even <a title="Single bar" href="css/value_definition_syntax#single_bar">|</a> odd</code></p>
Description
The :nth-last-child(an+b)
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.
In effect, it functions the same as :nth-child
except it selects items counting backwards from the end of element series, not the start.
See :nth-child
for a more thorough description of the syntax of its argument.
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
Specifications
Specification | Status | Comment |
---|---|---|
Selectors Level 4 The definition of ':nth-last-child' in that specification. |
Working Draft | No change |
Selectors Level 3 The definition of ':nth-last-child' in that specification. |
Recommendation | Initial definition |
License
© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/css/:nth-last-child