← Back to all topics

HTML REFERENCE · 07

Tables

Tables describe data whose values relate across rows and columns. Use them for schedules, comparisons, and measurements. Use CSS layout rather than tables to position unrelated page regions.

Tables at a glance

Element or attribute What it means When to use it
table A container for tabular data. Use when row and column relationships are meaningful. It does not automatically provide visible borders.
caption The table’s visible title or description. Place it first inside table to explain what the data represents.
tr A table row. Use inside thead, tbody, or tfoot. Put the row’s th and td cells inside it.
th A header cell naming a row, column, or group. Use for labels that explain data cells. It is meaningful even if CSS makes it look like an ordinary cell.
td An ordinary data cell. Use for values, such as a duration or quantity, rather than the labels that identify those values.
thead / tbody / tfoot Groups of header rows, main data rows, and footer rows. Use to express the table’s organization. Tfoot can hold totals or summaries; these elements do not replace tr.
scope="col" / scope="row" Associates a header with its column or row. Put scope on th to make simple header relationships explicit, especially when a table has both row and column headers.
colspan The number of columns one cell spans. Use on th or td for a value or heading that covers several columns. Count the occupied columns when building the row.
rowspan The number of rows one cell spans. Use on th or td for a shared value across rows. Later rows omit the cell in the occupied position.

Connecting row labels and column labels

The caption names the data. The top headers describe columns, and each row header identifies a route. Scope makes those relationships explicit. The shared guide stylesheet adds the borders and colors you see.

Code

<table>
    <caption>Walking routes</caption>
    <thead>
      <tr>
        <th scope="col">Route</th>
        <th scope="col">Distance</th>
        <th scope="col">Terrain</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">Ridge Loop</th>
        <td>4 km</td>
        <td>Hilly</td>
      </tr>
      <tr>
        <th scope="row">Valley Path</th>
        <td>2 km</td>
        <td>Level</td>
      </tr>
    </tbody>
  </table>

Output

Walking routes
Route Distance Terrain
Ridge Loop 4 km Hilly
Valley Path 2 km Level

Separating body data from a summary

Thead holds header rows, tbody holds ordinary data, and tfoot holds the total. Each group still contains tr elements. The order is caption, thead, tbody, then tfoot.

Code

<table>
  <caption>Walks recorded this week</caption>
  <thead>
    <tr>
      <th scope="col">Day</th>
      <th scope="col">Walks</th>
    </tr>
  </thead>
  <tbody>
    <tr><th scope="row">Monday</th><td>2</td></tr>
    <tr><th scope="row">Tuesday</th><td>1</td></tr>
  </tbody>
  <tfoot>
    <tr><th scope="row">Total</th><td>3</td></tr>
  </tfoot>
</table>

Output

Walks recorded this week
Day Walks
Monday2
Tuesday1
Total3

Spanning columns

The closing notice applies across both columns, so its cell has colspan="2". There is no second cell in that row: the single cell already occupies both positions.

Code

<table>
  <caption>Trail desk opening times</caption>
  <thead>
    <tr><th scope="col">Day</th><th scope="col">Hours</th></tr>
  </thead>
  <tbody>
    <tr><th scope="row">Saturday</th><td>09:00–12:00</td></tr>
    <tr><td colspan="2">Closed on public holidays.</td></tr>
  </tbody>
</table>

Output

Trail desk opening times
DayHours
Saturday09:00–12:00
Closed on public holidays.

Spanning rows

The shared “Open” value covers the two route rows. The second row contains no status cell because the first row’s rowspan="2" already occupies that position.

Code

<table>
  <caption>Routes with a shared status</caption>
  <thead>
    <tr><th scope="col">Route</th><th scope="col">Status</th></tr>
  </thead>
  <tbody>
    <tr><th scope="row">Ridge Loop</th><td rowspan="2">Open</td></tr>
    <tr><th scope="row">Valley Path</th></tr>
  </tbody>
</table>

Output

Routes with a shared status
RouteStatus
Ridge LoopOpen
Valley Path

Keep tables simple when possible. Large networks of merged header cells require more careful accessibility associations. Splitting a complicated table into smaller ones can be clearer.

Further reading: W3Schools: Tables. The explanations and examples here are written for this guide.

Study questions

Answer in your own words before opening each explanation.

When should I use a table?

When data has meaningful relationships across rows and columns, such as a schedule. Do not use tables to arrange a page’s navigation and content.

How do th and td differ?

Th is a header that labels related data. Td contains an ordinary data value. Their meaning is independent of visual boldness or color.

Where does caption belong?

As the first element inside table. It supplies a visible title or description of that table.

What does tr contain?

Header cells and data cells: th and td. The cells belong inside a row, rather than directly inside tbody.

What does scope="row" mean?

It marks a th as the header for its row. Scope="col" marks a column header.

Does tbody create a data cell?

No. It groups rows. Each row still needs tr and the appropriate th or td cells.

What changes when colspan="2" is used?

That cell occupies two column positions. Do not add another cell for the second occupied position.

Why does the row after a rowspan omit a cell?

The spanning cell already occupies that position in the later row. Adding another cell there would shift the intended grid.

Do table tags create the guide’s border colors?

No. The shared stylesheet supplies those visual styles. The HTML expresses the data relationships.