CSS :nth-last-child

CSS :nth-last-child(an+b) pseudo class represent all sibling elements whose sibling position is obtained by putting n=0,1,2,3… and so on from last. Note that sibling can be of any type but should have same parent container element.

CSS :nth-last-child values

The values are same as :nth-child. Only different is position counting starts from last.

Example of :nth-last-child

Change background of initial 2 siblings counting from last

<style type="text/css" media="screen">
.box > :nth-last-child(-n+2) {
  background-color:lightgreen;
}
</style>
<div class="box">
  <p>p1</p>
  <p>p2</p>
  <p>p3</p>
  <p>p4</p>
  <p>p5</p>
  <div>div1</div>
</div>
refresh

Related

Specification

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments