I have a table which contains multiple tbody
elements — some created by the browser, and some that I have written in myself for programmatic reasons. I want to apply styling to only the first tr
within my table, which I would normally use table > tr
for. However, my styling cannot assume that a tbody
element does or does not exist, because I don't know that every user's browser is adding in tbody
elements automatically in every case.
As you can see, an unwanted border styling is applied to the first tr
within the second tbody
of this table. How can I get this to be applied to only the first tr
in my table without regards to the presence of a tbody
tag?
JSFiddle demo
CSS
.my-table {
border-collapse: collapse;
}
.my-table td,
.my-table th {
text-align: left;
padding: 6px 8px;
align-content: center;
box-sizing: border-box;
}
.my-table td[colspan="42"] {
text-align: center;
background-color: lightgray;
}
.my-table th {
padding-top: 12px;
padding-bottom: 12px;
background-color: #0e3e64;
color: white;
text-align: center;
}
.my-table tr:not(:first-child) {
border: 1px solid #efefef;
}
/*here's the styling I don't want applied to that third tr in my table*/
.my-table tr:first-child {
border: 1px solid #0e3e64;
}
HTML
<table class="my-table">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<tr>
<td colspan="42">Section 1</td>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
<tbody>
<tr>
<td colspan="42">Section 2</td>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
</tbody>
</table>