How can I select all children of an element except the last child?

RyanScottLewis picture RyanScottLewis · Apr 4, 2010 · Viewed 192.8k times · Source

How would I select all but the last child using CSS3 selectors?

For example, to get only the last child would be div:nth-last-child(1).

Answer

Nick Craver picture Nick Craver · Apr 4, 2010

You can use the negation pseudo-class :not() against the :last-child pseudo-class. Being introduced CSS Selectors Level 3, it doesn't work in IE8 or below:

:not(:last-child) { /* styles */ }