I have a unique scenario with my table setup - it contains multiple tbody
elements, some generated by the browser and others added programmatically. I am trying to style only the first tr
within my table, typically achieved using table > tr
. The challenge is that I cannot assume the presence of a tbody
element due to variations in how different browsers handle this.
In the current state, an undesired border styling is affecting the first tr
within the second tbody
section. Is there a way to target only the very first tr in the table regardless of whether a tbody
tag exists?
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;
}
/*unwanted styling applied to third tr in 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>