2

Is there a way to select the nth-child or last-child of a specific type of class among the same elements?

ie

if I use .small:last-child or ul .small:last-child that property will only affect the very last element of the second list. Is there a selector that I can use to select the last instance of small in each list?

<ul>
    <li class="small">List Item</li>
    <li class="small">List Item</li>
    <li class="medium">List Item</li>
    <li class="large">List Item</li>
</ul>
<ul>
    <li class="medium">List Item</li>
    <li class="large">List Item</li>
    <li class="small">List Item</li>
    <li class="small">List Item</li>
</ul>

Please see fiddle CSS Line 72 for reference

http://jsfiddle.net/j7wq12v2/

Adjit
  • 10,134
  • 12
  • 53
  • 98
  • http://stackoverflow.com/questions/7298057/css-last-child-selector-select-last-element-of-specific-class-not-last-child-i it is not supported – Vitorino fernandes Jul 27 '15 at 16:48

2 Answers2

3

:last-child only works when the element in question is the last child of the container, not the last of a specific type of element. We could have used last-of-type

Unfortunately finding the last .class is not possible with last-of-type.

We need to use javascript in order to achieve the result.

Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
2

Edit: actually, it may not be possible to use the last-of-type psuedo selector on classes, check this answer to a similar question.

Community
  • 1
  • 1
sowasred2012
  • 695
  • 1
  • 7
  • 22