Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in css to select second last child?

Need to ensure browser compatibility as well (should work in IE8)

I came across - :nth-last-child(2)
But it is not browser compatible as it is a css3 selector.

Also I came across a tip to get 2nd, 3rd, 4th child as - td:first-child + td + td
But no way of reversing like
td:last-child - td
to get second last child.

I don't want to use jquery.
Is applying class to the second last element is only option ?

like image 890
Ashwin Avatar asked Sep 04 '25 04:09

Ashwin


2 Answers

The CSS3 nth-last-child is probably the way to go, since support should become more and more common.

Failing this, you could simply add a CSS class to the element in the markup eg:

<tr>
  <td>...</td>
  <td class = "penultimate">...</td>
  <td>...</td>
</tr>
like image 101
Rich O'Kelly Avatar answered Sep 06 '25 06:09

Rich O'Kelly


Under the conditions specified, applying class (or id) to the second last element is only option.

like image 26
Jukka K. Korpela Avatar answered Sep 06 '25 06:09

Jukka K. Korpela