-1

I have a table with tr's and on some tr's I have a class named "comment" and on the others class named "something" like that: example

<body>

Hello

<tr class="comment"><td>World</td></tr>

<tr class="something"><td>!!!</td></tr>

<tr class="something"><td>hello</td></tr>

Now I need that the last child of the class "comment" will be with another background. How do I do it? Nothing seems to work for me.

LilacEilim
  • 89
  • 1
  • 8

2 Answers2

0

You can do this with jQuery, I don't think there is a way to do it with CSS. In the example I am makinf the background red, but you can do whatever you need. Here's a fiddle: https://jsfiddle.net/arwa36hg/

$('.comment').last().find('td').css('background-color', 'red');
sticksu
  • 3,660
  • 3
  • 23
  • 41
0

Why it does not work with CSS?

The last-child selector is used to select the last child element of a parent. It cannot be used to select the last child element with a specific class under a given parent element.

as an example:

tr:last-child will work: http://jsbin.com/julogo/1/edit?html,css,output

but not as you want:

tr.comment:last-child: http://jsbin.com/julogo/2/edit?html,css,output

balexandre
  • 73,608
  • 45
  • 233
  • 342
  • Last child only works if the element is actual the last in the list, no matter what classes are on them. In this case the comment div is not the last element in there, there are 2 more elements after it, so last-child won't work. Just try it on a fiddle and you will see. – sticksu Oct 19 '15 at 07:51