Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Table Anatomy in HTML: Rows, Cells and Spans

· 5 min read · Home Tech

In this article
  1. Build the Skeleton: table, tr, th and td
  2. Wrap Headers in thead and Rows in tbody
  3. Use scope to Tie Headers to Their Cells
  4. Span Cells with colspan and rowspan
  5. Keep the Grid Rectangular and the Markup Semantic

An HTML table is built from a <table> wrapper containing rows (<tr>), and each row holds cells: <td> for data and <th> for headers. The part most people get wrong is the header structure: a <th> only announces itself as a header to a screen reader if it sits inside a <thead> or is tied to its column with scope. Getting rows and cells right is easy; getting the header-to-data relationship right is what separates a table that reads well from one that only looks right.

Build the Skeleton: table, tr, th and td

Every table starts with the same four elements, nested in a fixed order. The <table> element is the container. Inside it, <tr> creates a row. Inside each row, <th> marks a header cell and <td> marks a data cell. The number of cells in a row sets the number of columns for the whole table, so the first row you write effectively defines the grid.

A minimal two-column table looks like this:

<table>
  <tr>
    <th scope="col">City</th>
    <th scope="col">Population</th>
  </tr>
  <tr>
    <td>Austin</td>
    <td>About 1 million</td>
  </tr>
</table>

That is the whole anatomy. Everything else — sectioning, spanning, captions — is an addition to these four parts. If you can write that block from memory, you can build any table.

Wrap Headers in thead and Rows in tbody

The browser will render a table without <thead> and <tbody>, but adding them makes the structure explicit and lets you style the header row separately. The order is <caption> first, then <thead>, then <tbody>, then <tfoot> if you need a totals row. Browsers will reorder these correctly even if you write them out of sequence, but writing them in order keeps the source readable.

A full skeleton with all four sections:

<table>
  <caption>Monthly rainfall</caption>
  <thead>
    <tr><th scope="col">Month</th><th scope="col">Inches</th></tr>
  </thead>
  <tbody>
    <tr><td>January</td><td>3.2</td></tr>
    <tr><td>February</td><td>2.8</td></tr>
  </tbody>
  <tfoot>
    <tr><td>Total</td><td>6.0</td></tr>
  </tfoot>
</table>

The <caption> is not decoration. It gives the table a name that screen readers announce before the contents, and it is the correct place for a title like “Monthly rainfall” rather than a heading above the table.

Use scope to Tie Headers to Their Cells

The scope attribute tells assistive technology which cells a header governs. scope="col" means the header applies down its column. scope="row" means it applies across its row. A table with both row and column headers needs both, and the header cell for a row goes in the first cell of that row, as a <th> rather than a <td>.

Consider a table of test scores by student and subject. The top row holds subject names with scope="col". The first cell of each student row holds the student name with scope="row". When a screen reader lands on a score, it can announce both the subject and the student, which is the whole point of the structure.

If you omit scope, browsers and screen readers fall back to guessing from position. That guess is often right for simple tables and often wrong for anything with merged cells, so adding scope is cheap insurance.

Span Cells with colspan and rowspan

Two attributes let a cell cover more than one column or row. colspan="2" makes a cell stretch across two columns. rowspan="3" makes it stretch down three rows. The rule that trips people up: a spanned cell counts toward the total in every row or column it covers, so the remaining cells in those rows must be fewer to keep the grid rectangular.

Take a three-column table where the header row has one cell spanning all three columns:

<tr><th colspan="3">Quarterly sales</th></tr>
<tr><th scope="col">Q1</th><th scope="col">Q2</th><th scope="col">Q3</th></tr>

The first row has one cell that counts as three, so the grid stays three columns wide. If you then wrote three more <th> cells in that same row, the table would have six column slots and break. The arithmetic is simple but easy to lose track of: total slots per row must equal the column count.

For a rowspan example, a table listing a department once and three employees beneath it uses rowspan="3" on the department cell. The next two rows then contain only the employee cells, because the department cell already occupies the first column in all three rows.

Keep the Grid Rectangular and the Markup Semantic

A table is a grid, and every row must resolve to the same number of column slots. When a row is short, browsers insert empty cells to fill the gap, which usually looks like a mystery blank column. When a row is long, the table widens and earlier rows get padded. Checking the slot count row by row is the fastest way to find a broken table.

Semantics matter as much as shape. Use <th> for anything that labels a row or column, and <td> for the data itself. Do not use a table for page layout; that was a common practice years ago and it makes the page harder to navigate with a screen reader. If the content is a list of items, use a list. If it is a grid of data with row and column relationships, a table is correct.

Two more details worth knowing. First, <colgroup> and <col> let you apply styling to whole columns without repeating it on every cell, though support for some column properties is uneven. Second, the headers attribute on a <td> can point to the id of one or more header cells, which is useful for complex tables where scope alone is not enough; it is more verbose and usually unnecessary for simple layouts.

A quick rule of thumb: if a table has one header row and no merged cells, scope="col" on each header is enough. If it has both row and column headers, add scope="row" to the first cell of each row. If it has merged cells, check the slot count in every row before you style anything.

Finally, keep the caption short and descriptive. “Quarterly sales by region, 2024” tells a reader what the numbers mean before they start reading them. A caption that repeats the page heading adds nothing. The caption is part of the table's anatomy, not an optional extra, and it is the first thing a screen reader encounters when it enters the table.