I thought I was reasonably versed in CSS, but this one has me stumped.
How do you target the topmost row of a table, if you don't know what the table structure is? My CSS file needs to be independent of however the HTML is structured.
So what is the selector I should use? If the table has a thead
, then it's easy.
.theTable > thead > tr:first-child {..}
and if it only has one or more tbody
elements, that's easy too.
.theTable > tbody:first-of-type > tr:first-child {..}
but if you don't know? How do you prevent ending up styling both the thead
and the tbody
?
For example, how do you make sure that in the following three tables, only the top row in each one gets the style?
/* bad CSS */
table tr:first-of-type {background:tan;}
<table>
<thead><tr><th>nr</th><th>name</th></thead>
<tbody><tr><td>1</td><td>one</td>
<tr><td>2</td><td>two</td></tbody>
</table>
<hr>
<table>
<tbody><tr><td>1</td><td>one</td>
<tr><td>2</td><td>two</td></tbody>
</table>
<hr>
<table>
<caption>names</caption>
<tbody><tr><td>1</td><td>one</td>
<tr><td>2</td><td>two</td></tbody>
</table>