I currently have a "vertical table" (i.e. the headings are on the right). The html looks like this:
<table>
<tbody>
<tr><th>1</th><td>1</td></tr> <!-- style this -->
<tr><th>1</th><td>1</td></tr> <!-- style this -->
<tr><th>1</th><td>1</td></tr> <!-- style this -->
<tr><th>2</th><td>2</td></tr>
<tr><th>2</th><td>2</td></tr>
<tr><th>2</th><td>2</td></tr>
<tr><th>3</th><td>3</td></tr> <!-- style this -->
<tr><th>3</th><td>3</td></tr> <!-- style this -->
<tr><th>3</th><td>3</td></tr> <!-- style this -->
</tbody>
<tbody>
<tr><th>4</th><td>4</td></tr> <!-- style this -->
<tr><th>4</th><td>4</td></tr> <!-- style this -->
<tr><th>4</th><td>4</td></tr> <!-- style this -->
<tr><th>5</th><td>5</td></tr>
<tr><th>5</th><td>5</td></tr>
<tr><th>5</th><td>5</td></tr>
</tbody>
</table>
I am trying to style each "data chunk" - the three rows with 1
, three rows with 2
, etc. in an even or odd basis - within each tbody
.
My first idea was to use tr:nth-child(odd)
; however, that does this:
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 4px;
}
tr:nth-child(odd) {
background-color: steelblue;
}
<table>
<tbody>
<tr><th>1</th><td>1</td></tr> <!-- style this -->
<tr><th>1</th><td>1</td></tr> <!-- style this -->
<tr><th>1</th><td>1</td></tr> <!-- style this -->
<tr><th>2</th><td>2</td></tr>
<tr><th>2</th><td>2</td></tr>
<tr><th>2</th><td>2</td></tr>
<tr><th>3</th><td>3</td></tr> <!-- style this -->
<tr><th>3</th><td>3</td></tr> <!-- style this -->
<tr><th>3</th><td>3</td></tr> <!-- style this -->
</tbody>
<tbody>
<tr><th>4</th><td>4</td></tr> <!-- style this -->
<tr><th>4</th><td>4</td></tr> <!-- style this -->
<tr><th>4</th><td>4</td></tr> <!-- style this -->
<tr><th>5</th><td>5</td></tr>
<tr><th>5</th><td>5</td></tr>
<tr><th>5</th><td>5</td></tr>
</tbody>
</table>
Which does not work.
I then tried styling it by tbody
(tbody:nth-child(odd)
) but that obviously did not work.
Another one I tried was :nth-child(-n+3)
which created this result:
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 4px;
}
tr:nth-child(-n+3) {
background-color: steelblue;
}
<table>
<tbody>
<tr><th>1</th><td>1</td></tr> <!-- style this -->
<tr><th>1</th><td>1</td></tr> <!-- style this -->
<tr><th>1</th><td>1</td></tr> <!-- style this -->
<tr><th>2</th><td>2</td></tr>
<tr><th>2</th><td>2</td></tr>
<tr><th>2</th><td>2</td></tr>
<tr><th>3</th><td>3</td></tr> <!-- style this -->
<tr><th>3</th><td>3</td></tr> <!-- style this -->
<tr><th>3</th><td>3</td></tr> <!-- style this -->
</tbody>
<tbody>
<tr><th>4</th><td>4</td></tr> <!-- style this -->
<tr><th>4</th><td>4</td></tr> <!-- style this -->
<tr><th>4</th><td>4</td></tr> <!-- style this -->
<tr><th>5</th><td>5</td></tr>
<tr><th>5</th><td>5</td></tr>
<tr><th>5</th><td>5</td></tr>
</tbody>
</table>
It's close, but only gets the first "data-chunk" (as I am calling it).
I am looking for a pure-css way to style multiple alternate table rows, something which would look like this:
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 4px;
}
.style-me {
background-color: steelblue;
}
tbody:before{
content: 'new tbody';
}
<table>
<tbody>
<tr class="style-me"><th>1</th><td>1</td></tr> <!-- style this -->
<tr class="style-me"><th>1</th><td>1</td></tr> <!-- style this -->
<tr class="style-me"><th>1</th><td>1</td></tr> <!-- style this -->
<tr><th>2</th><td>2</td></tr>
<tr><th>2</th><td>2</td></tr>
<tr><th>2</th><td>2</td></tr>
<tr class="style-me"><th>3</th><td>3</td></tr> <!-- style this -->
<tr class="style-me"><th>3</th><td>3</td></tr> <!-- style this -->
<tr class="style-me"><th>3</th><td>3</td></tr> <!-- style this -->
</tbody>
<tbody>
<tr class="style-me"><th>4</th><td>4</td></tr> <!-- style this -->
<tr class="style-me"><th>4</th><td>4</td></tr> <!-- style this -->
<tr class="style-me"><th>4</th><td>4</td></tr> <!-- style this -->
<tr><th>5</th><td>5</td></tr>
<tr><th>5</th><td>5</td></tr>
<tr><th>5</th><td>5</td></tr>
</tbody>
</table>
However, instead of using classes it uses a css selector/pseudo class/element (maybe :nth-of-type
?) to style the odd groupings of rows as the amount of grouping of rows is unknown (it's a dynamic table)
This comment in the answers which best describes what I am looking for.