One simple way to 'always target the last row, not matter where it is' is simply
table > *:last-child > tr:last-of-type {
background-color: #0f0;
}
<table>
<thead>
<tr>
<th>A</th>
</tr>
</thead>
<tbody>
<tr>
<td>a</td>
</tr>
<tr>
<td>b</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>A</th>
</tr>
</thead>
<tbody>
<tr>
<td>a</td>
</tr>
<tr>
<td>b</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>c</td>
</tr>
</tfoot>
</table>
This will do what you described, though there is a catch. The order of the <thead>
, <tbody>
and <tfoot>
elements is not enforced, so you may actually put a <tfoot>
before the <tbody>
with the same table as output, in which case literally the last <tr>
will get selected, which is not visually the last <tr>
.
That would require some additional trickery:
/* the optimistic true last <tr> */
table > :last-child > tr:last-of-type,
/* any last <tr> within a <tfoot> */
table tfoot > tr:last-of-type {
background-color: #0f0;
}
/* reset any style in the true last <tr> if its parent is
preceded by a `<tfoot>` */
table > tfoot ~ :last-child > tr:last-of-type {
background-color: initial;
}
working fiddle
EDIT:
I forgot to take empty <tfoot>
element into consideration, this demands the use of the :not
and :empty
pseudo classes (which are not supported by older browsers), which would look like:
/* the optimistic true last <tr> */
table > :last-child > tr:last-of-type,
/* any last <tr> within a <tfoot> */
table tfoot > tr:last-of-type {
background-color: #0f0;
}
/* reset any style in the true last <tr> if its parent is
preceded by a `<tfoot>` */
table > tfoot:not(:empty) ~ :last-child > tr:last-of-type {
background-color: initial;
}
updated fiddle