Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I color odd-numbered table rows starting after each row with a particular class?

I have sections in my table, so there are section-header rows and regular-rows. How can I color one in two rows, so that the next row after the section-header is always colored, like in the following snippet. I’m trying using a combination of + and :nth-child, but I can’t make it work.

table {
border-collapse: collapse;
}

.want {
  background-color: aquamarine;
}
<table>
  <tr class="table-header"><th>Blah</th><th>Blablablah</th></tr>
  <tr class="section-header"><th colspan="2">Section A</th></tr>
  <tr class="regular-row want"><td>A1</td><td>Blah</td></tr>
  <tr class="regular-row"><td>A2</td><td>Blah</td></tr>
  <tr class="regular-row want"><td>A3</td><td>Blah</td></tr>
  <tr class="regular-row"><td>A4</td><td>Blah</td></tr>
  <tr class="regular-row want"><td>A5</td><td>Blah</td></tr>
  <tr class="section-header"><th colspan="2">Section B</th></tr>
  <tr class="regular-row want"><td>B1</td><td>Blah</td></tr>
  <tr class="regular-row"><td>B2</td><td>Blah</td></tr>
  <tr class="regular-row want"><td>B3</td><td>Blah</td></tr>
  <tr class="regular-row"><td>B4</td><td>Blah</td></tr>
  <tr class="section-header"><th colspan="2">Section C</th></tr>
  <tr class="regular-row want"><td>C1</td><td>Blah</td></tr>
  <tr class="regular-row"><td>C1</td><td>Blah</td></tr>
  <tr class="regular-row want"><td>C1</td><td>Blah</td></tr>
  <tr class="section-header"><th colspan="2">Section D</th></tr>
  <tr class="regular-row want"><td>C1</td><td>Blah</td></tr>
  <tr class="regular-row"><td>C1</td><td>Blah</td></tr>
</table>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I suggest either nested tables or a series of tbody elements. Either would make this simple.

table {
border-collapse: collapse;
}

tbody > tr:nth-child(even) {
  background-color: aquamarine;
}
<table>
  <thead>
    <tr class="table-header"><th>Blah</th><th>Blablablah</th></tr>
  </thead>
  <tbody>
    <tr class="section-header"><th colspan="2">Section A</th></tr>
    <tr class="regular-row want"><td>A1</td><td>Blah</td></tr>
    <tr class="regular-row"><td>A2</td><td>Blah</td></tr>
    <tr class="regular-row want"><td>A3</td><td>Blah</td></tr>
    <tr class="regular-row"><td>A4</td><td>Blah</td></tr>
    <tr class="regular-row want"><td>A5</td><td>Blah</td></tr>
  </tbody>
  <tbody>
    <tr class="section-header"><th colspan="2">Section B</th></tr>
    <tr class="regular-row want"><td>B1</td><td>Blah</td></tr>
    <tr class="regular-row"><td>B2</td><td>Blah</td></tr>
    <tr class="regular-row want"><td>B3</td><td>Blah</td></tr>
    <tr class="regular-row"><td>B4</td><td>Blah</td></tr>
  </tbody>
  <tbody>
    <tr class="section-header"><th colspan="2">Section C</th></tr>
    <tr class="regular-row want"><td>C1</td><td>Blah</td></tr>
    <tr class="regular-row"><td>C1</td><td>Blah</td></tr>
    <tr class="regular-row want"><td>C1</td><td>Blah</td></tr>
  </tbody>
  <tbody>
    <tr class="section-header"><th colspan="2">Section D</th></tr>
    <tr class="regular-row want"><td>C1</td><td>Blah</td></tr>
    <tr class="regular-row"><td>C1</td><td>Blah</td></tr>
  </tbody>
</table>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading