Emmanuel Akinfulubi

HTML and CSSHTML and CSS

Zebra-striped table with collapsed borders in CSS

13 Collapsed borders and zebra stripes

fair

Turn the doubled borders of a default table into single lines, then stripe alternate body rows. Keep the stripe out of the header.

Stuck? Three hints, smallest first
  1. 1. border-collapse: collapse merges adjacent cell borders into one.
  2. 2. tbody tr:nth-child(even) selects every other row, counting within the body.
  3. 3. border-spacing only does anything while borders are separate, so it is the wrong tool here.

index.html

<table class="grid">
  <caption>Departures</caption>
  <thead>
    <tr>
      <th scope="col">Time</th>
      <th scope="col">Route</th>
      <th scope="col">Bay</th>
    </tr>
  </thead>
  <tbody>
    <tr><td>06:30</td><td>Abuja</td><td>B</td></tr>
    <tr><td>08:00</td><td>Kano</td><td>C</td></tr>
    <tr><td>11:15</td><td>Enugu</td><td>D</td></tr>
    <tr><td>14:45</td><td>Ibadan</td><td>A</td></tr>
  </tbody>
</table>

style.css

.grid {
  border-collapse: collapse;   /* one border per edge, not two side by side */
  width: 100%;
  max-width: 340px;
  font-size: 0.9rem;
}

.grid caption {
  text-align: left;
  padding-bottom: 8px;
  color: #5c5c66;
  font-weight: 600;
}

.grid th,
.grid td {
  padding: 7px 10px;
  border-bottom: 1px solid #c9cad1;
  text-align: left;
}

.grid thead th {
  background: #17171a;
  color: #ffffff;
}

/* Counting inside tbody, so the header is never striped. */
.grid tbody tr:nth-child(even) {
  background: #f2f3f5;
}

.grid tbody tr:hover {
  background: #e9ecf6;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 13

test('borders are collapsed', /border-collapse\s*:\s*collapse/.test(css), true);
test('alternate rows are striped', /nth-child\(even\)/.test(css), true);
test('the stripe is scoped to tbody, so the header is spared', /tbody tr:nth-child\(even\)/.test(css), true);
test('border-spacing is not used, since it needs separate borders', /border-spacing/.test(css), false);
test('the header has its own treatment', /thead th\s*\{[^}]*background\s*:\s*#17171a/.test(css), true);
test('four body rows, so two get a stripe', (html.match(/<tr><td/g) || []).length, 4);
test('white on near-black in the header, which clears AA comfortably', /color\s*:\s*#ffffff/.test(css), true);
test('a row highlight on hover as well', /tbody tr:hover/.test(css), true);

Output


      

Two traps. border-spacing is the property people reach for to tighten a table up, and it does nothing at all once border-collapse: collapse is set — the two models are mutually exclusive, which is why the fourth check refuses it. And tr:nth-child(even) without tbody in front counts the header row as child one, so the stripes land on the wrong rows and the header picks one up. Scoping the selector to tbody fixes both at once.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.