Emmanuel Akinfulubi

HTML and CSSHTML and CSS

HTML table with thead, tbody and tfoot

11 A table with a head, a body and a foot

fair

Build a data table with a caption, a header row, a body and a totals row in the right section. Tell assistive technology which cells are headers and what they head.

Stuck? Three hints, smallest first
  1. 1. <caption> is the table’s name and goes first, inside the table.
  2. 2. <tfoot> is for a summary of the body, such as a total.
  3. 3. scope="col" on a column header, scope="row" on a row header.

index.html

<table class="fares">
  <caption>Fares from Jibowu, one way</caption>
  <thead>
    <tr>
      <th scope="col">Route</th>
      <th scope="col">Seats</th>
      <th scope="col">Fare</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Abuja</th>
      <td>14</td>
      <td>&#8358;18,000</td>
    </tr>
    <tr>
      <th scope="row">Kano</th>
      <td>30</td>
      <td>&#8358;22,000</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th scope="row">Total</th>
      <td>44</td>
      <td>&#8358;40,000</td>
    </tr>
  </tfoot>
</table>

style.css

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

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

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

.fares thead th {
  background: #e9ecf6;
}

.fares tfoot th,
.fares tfoot td {
  border-top: 2px solid #17171a;
  border-bottom: 0;
  font-weight: 700;
}
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 11

test('the table is named by a caption', hasTag(html, 'caption'), true);
test('all three sections are present', ['thead', 'tbody', 'tfoot'].every((t) => hasTag(html, t)), true);
test('three column headers', (html.match(/scope="col"/g) || []).length, 3);
test('and a row header on every body and foot row', (html.match(/scope="row"/g) || []).length, 3);
test('the totals row lives in tfoot, not tbody', /<tfoot>[\s\S]*Total/.test(html), true);
test('borders are collapsed to one per edge', /border-collapse\s*:\s*collapse/.test(css), true);
test('the header row is distinguishable', /thead th\s*\{[^}]*background/.test(css), true);
test('the caption is placed above the table', /caption-side\s*:\s*top/.test(css), true);

Output


      

scope is what turns a grid of cells into data a screen reader can read out in context: landing on ₦22,000 it can announce “Kano, Fare, ₦22,000” instead of a bare number. <tfoot> is not decoration either — it marks the row as a summary of the body rather than another data row, which matters when the table is long enough to scroll. And <caption> is the accessible name of the table, so a reader listing tables on the page gets something better than “table”.

Newsletter

New writing, when it lands.

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