I have an unordered list. It sometimes contains 4, 5, 6, or 7 items. I would like to know if there is a CSS selector to select the last two items. I realize that :last-child
will get me the last item. Is there a "second to last child" selector? or "number of children -2" selector?
HTML:
<ul>
<li>First Item</li>
<li>Second Item</li>
<li>Second from Last Item</li>
<li>Last Item</li>
<ul>
CSS:
ul li:last-child, ul li:last-child-1 {
// Do something
}
I realize :last-child-1
is not a selector but im hoping there is something similar out there. Any help would be greatly apprecialted.
ul li:nth-last-child(-n + 2) {
/* .. */
}
Go for :nth-last-child
http://jsfiddle.net/pQmXM/1/
ul li:nth-last-child(1),
ul li:nth-last-child(2)
{
color: red;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With