a

Select the second last css element

I already know about the last child. But is there a way to select a div:

<div id="container"> <div>a</div> <div>b</div> <div>SELECT THIS</div> <!-- THIS --> <div>c</div> </div> 

NOTE: without jQuery, only with CSS

+85
html css css-selectors css3
Mar 24 2018-11-11T00:
source share
2 answers

In CSS3 you have:

 :nth-last-child(2) 

See: http://webdesign.about.com/od/styleproperties/p/nth-last-child.htm

nth-last-child Browser Support:

  • Chrome 2
  • Firefox 3.5
  • Opera 9.5, 10
  • Safari 3.1, 4
  • Internet Explorer 9
+155
Mar 24 '11 at 12:08
source share

Note: Sent this answer because the OP later in the comments indicated that it needs to select the last two items , not just the penultimate one.




Selector :nth-child CSS3 is actually more efficient than you ever imagined!

For example, this will select the last 2 elements of #container :

 #container :nth-last-child(-n+2) {} 

But this is only the beginning of a wonderful friendship.

+35
Mar 24 '11 at 12:08
source share



All Articles