CSS/Selectors/pseudo-classes/:nth-last-child

From W3C Wiki
< CSS‎ | Selectors

Pseudo-class :nth-last-child()

The :nth-last-child() pseudo-class represents an element that has an+b siblings after it in the document tree, for any positive integer or zero value of n, and has a parent element.

Syntax

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


Point, Note

  • The examples of an+b are as follows:
    • :nth-last-child(odd) /* represents all odd foo elements in their parent element, counting from the last one */
    • :nth-last-child(-n+2) /* represents the two last elements */


Example

[style.css]

 tr:nth-last-child(2n){
   background-color: yellow;
 }


[index.html]

<body>
  <table>
    <tr><td>This is first row.</td></tr>
    <tr><td>This is second row.</td></tr>
    <tr><td>This is third row.</td></tr>
    <tr><td>This is fourth row.</td></tr>
  </table>
</body>

CSS defines the :nth-last-child() pseudo-class selector in 6.6.5.3. :nth-last-child() pseudo-class.