Select second last element with css

dynamic picture dynamic · Mar 24, 2011 · Viewed 99.3k times · Source

I already know of :last-child. But is there a way to select the 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

Answer

Frosty Z picture Frosty Z · Mar 24, 2011

In CSS3 you have:

:nth-last-child(2)

See: https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-child

nth-last-child Browser Support:

  • Chrome 2
  • Firefox 3.5
  • Opera 9.5, 10
  • Safari 3.1, 4
  • Internet Explorer 9