Emmanuel Akinfulubi

HTML and CSSHTML and CSS

Use colspan and rowspan in an HTML table

12 Cells that span

hard

Merge cells across columns and down rows, and keep the grid rectangular. Prove the arithmetic rather than trusting your eye.

Stuck? Three hints, smallest first
  1. 1. colspan widens a cell; rowspan makes it reach down into later rows.
  2. 2. A row under a rowspan declares one fewer cell, because the cell above is occupying that slot.
  3. 3. Add the declared spans per row. Every row must reach the same total once the spans from above are counted.

index.html

<table class="bays">
  <caption>Bay assignments</caption>
  <thead>
    <tr>
      <th scope="col">Bay</th>
      <th scope="col">Morning</th>
      <th scope="col">Afternoon</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">A</th>
      <td colspan="2">Closed for resurfacing</td>
    </tr>
    <tr>
      <th scope="row">B</th>
      <td rowspan="2">Abuja</td>
      <td>Kano</td>
    </tr>
    <tr>
      <th scope="row">C</th>
      <td>Enugu</td>
    </tr>
  </tbody>
</table>

style.css

.bays {
  border-collapse: collapse;
  font-size: 0.9rem;
}

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

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

.bays thead th { background: #e9ecf6; }

/* A merged cell reads better centred over the columns it covers. */
.bays td[colspan] { text-align: center; color: #5c5c66; }
.bays td[rowspan] { vertical-align: middle; background: #f7f7f8; }
Rendered result. Shown, not machine-checked: there is no browser in the build.
7 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 12

// Add up the columns each row DECLARES. The row under the rowspan declares one
// fewer, because the cell above is still occupying that slot.
const rows = html.match(/<tr>[\s\S]*?<\/tr>/g) || [];
const declaredWidth = (row) =>
  (row.match(/<t[hd][^>]*>/g) || []).reduce((n, cell) => {
    const span = /colspan="(\d+)"/.exec(cell);
    return n + (span ? Number(span[1]) : 1);
  }, 0);
const widths = rows.map(declaredWidth);

test('four rows', rows.length, 4);
test('they declare 3, 3, 3 and 2 columns', widths, [3, 3, 3, 2]);
test('the short row plus the rowspan above it makes 3', widths[3] + 1, 3);
test('one cell spans two columns', /colspan="2"/.test(html), true);
test('one cell spans two rows', /rowspan="2"/.test(html), true);
test('spanned cells are styled by attribute, not an extra class', /td\[colspan\]/.test(css), true);
test('every row still has a row header', (html.match(/scope="row"/g) || []).length, 3);

Output


      

The second and third checks are the whole exercise. Rows under a rowspan genuinely declare fewer cells, so counting tags gives 2 for the last row and nothing is wrong — the missing slot is filled from above. Get this off by one and the browser silently grows a phantom column, which is the usual cause of a table with a ragged right edge. Styling with td[colspan] rather than a class is a small win: the attribute is already the truth, so there is nothing extra to keep in sync.

Newsletter

New writing, when it lands.

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