I have an HTML table with rowspans in it:
table tr,
td {
border: 1px solid black;
}
tr:nth-child(1) {
background-color: red;
}
tr:nth-child(2) {
background-color: blue;
}
<table>
<tr>
<td rowspan=2>Section 1</td>
<td>Data 1a</td>
</tr>
<tr>
<td>Data 1b</td>
</tr>
<tr>
<td rowspan=3>Section 2</td>
<td>Data 2a</td>
</tr>
<tr>
<td>Data 2b</td>
</tr>
<tr>
<td>Data 2C</td>
</tr>
I am trying to find a simple CSS solution using nth-child
to apply styling, such as background colors, to a group of rows within a "Section" that is defined by row spans.
The current CSS only styles individual table rows based on their position, but I want to target and style multiple rows based on the number of rows spanned by a cell.
In this example, I would like to:
- Make Section 1, Data 1a, and Data 1b appear in red
- Style Section 2, Data 2a, Data 2b, and Data 2c with a blue color
If you have any suggestions for achieving this using simple HTML/CSS without relying on JavaScript, please let me know!