Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 122 ยท Tables

Table Headers, Sections and Captions

Give your tables real structure with th and scope, a caption that names the table, and thead, tbody and tfoot row groups for headers, data and totals.

In the previous lesson you built tables from rows and cells. That is enough to draw a grid, but a good table also explains itself: it has a title, its headers say what they label, and its rows are grouped into a head, a body and sometimes a foot of totals.

This lesson covers the elements and attributes that add that structure: scope on <th>, <caption>, and the row groups <thead>, <tbody> and <tfoot>. None of them changes much on screen, but together they make tables far easier to understand, style and navigate.

Header cells and the scope attribute

A <th> says "I am a header". The scope attribute adds which cells I am a header for:

Value The header labelsโ€ฆ
col the rest of its column
row the rest of its row
colgroup a group of columns (see Column Groups)
rowgroup a group of rows, such as one <tbody>

Here is a weekly class schedule with headers in both directions:

HTML
<tr>
  <th scope="col">Time</th>
  <th scope="col">Monday</th>
  <th scope="col">Tuesday</th>
</tr>
<tr>
  <th scope="row">09:00</th>
  <td>Biology</td>
  <td>History</td>
</tr>

When a screen-reader user lands on History, they hear something like "Tuesday, 09:00, History". Without that context, a lone word in a grid means very little.

In a simple table, browsers can usually guess the scope from the cell's position. Writing it out removes the guesswork, costs a few characters and makes your intent obvious to anyone who reads the code later. It is a good habit for every header cell.

Naming the table with <caption>

A <caption> is the table's title. It must be the first child of <table>, and browsers show it centered above the table by default.

Caption and scope

Why not just put an <h2> above the table? A heading helps people skim the page, and you can certainly have one. But the caption is part of the table itself: screen readers announce it when the user enters the table, and it travels with the table if the table is moved. A good caption is short and specific: "Year 9 timetable, week A" beats "Timetable".

Move it with CSS, not HTML. If your design needs the caption below the table, keep <caption> as the first child and set caption-side: bottom; in CSS.

Row groups: <thead>, <tbody> and <tfoot>

Large tables have distinct parts. The rows of column headers at the top are different from the data, and a row of totals at the bottom is different again. HTML gives each part its own element:

  • <thead>: the header rows, usually one row of column headers.
  • <tbody>: the main data. A table can have several <tbody> elements to split the data into groups.
  • <tfoot>: summary rows such as totals or averages.

The order inside <table> is fixed: <caption> first, then any column groups, then <thead>, then the <tbody> elements, and finally <tfoot>.

A table with head, body and foot

Notice how the CSS styles whole sections at once: thead gets the dark background and tfoot gets the highlight. You didn't need a single class. That alone makes row groups worth using.

What row groups give you

  • Easy styling. Selectors like thead th or tbody tr:nth-child(even) target exactly the rows you mean. You will use these in Styling Tables with CSS.
  • Better printing. When a long table spans several printed pages, browsers can repeat the <thead> (and <tfoot>) on each page.
  • Scrolling bodies and sticky headers. With the head separated from the body, it's simple to keep the header row visible while the data scrolls.
  • Clearer code. Anyone reading the HTML sees at a glance where the headers, data and totals are.

Several <tbody> groups

When the data falls into natural chunks, use one <tbody> per chunk and give each a header row with scope="rowgroup". A conference program split by day is a good fit:

HTML
<table>
  <caption>Conference program</caption>
  <thead>
    <tr>
      <th scope="col">Time</th>
      <th scope="col">Talk</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="rowgroup" colspan="2">Friday</th>
    </tr>
    <tr>
      <th scope="row">10:00</th>
      <td>Designing for slow networks</td>
    </tr>
  </tbody>
  <tbody>
    <tr>
      <th scope="rowgroup" colspan="2">Saturday</th>
    </tr>
    <tr>
      <th scope="row">10:00</th>
      <td>Forms that forgive mistakes</td>
    </tr>
  </tbody>
</table>

The "Friday" and "Saturday" cells label every row in their group. The colspan="2" lets them stretch across both columns; you will learn all about it in Spanning Rows and Columns.

Only one <thead> and one <tfoot>. A table may contain any number of <tbody> elements, but at most one head and one foot. And remember: if you leave out <tbody> entirely, the browser adds one for you.

Common mistakes

  • Putting the caption in the wrong place. <caption> must come before everything else inside <table>, not after the rows or outside the table.
  • Using <th> just to get bold text. If a cell doesn't label anything, use <td> and make it bold with CSS.
  • Using <thead> for visual styling only. Put header rows in <thead>, not the first data row just because you want it to look different.
  • Mixing loose rows and <tbody>. Once you write any <tbody>, put all data rows inside row groups.
  • Empty top-left corner cells marked as <th>. When the corner cell has no text, a <td> is the cleaner choice; an empty header labels nothing.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where must the <caption> element go?

    <caption> has to be the very first thing inside <table>. Use the CSS caption-side property if you want it displayed below the table.
  2. 2. A <th> labels the cells to its right in the same row. Which scope value describes that?

    scope="row" means the header applies to the rest of its row. col would mean the cells below it.
  3. 3. How many <tbody> elements can a table contain?

    You may split the data into as many <tbody> groups as make sense. Only <thead> and <tfoot> are limited to one each.
  4. 4. Which row group is the right place for a row of totals?

    <tfoot> holds summary rows such as totals. It comes after the <tbody> elements in the code.

Summary

  • scope="col" and scope="row" tell browsers and assistive technology which cells a <th> labels; colgroup and rowgroup cover groups.
  • <caption> names the table and must be the first child of <table>; reposition it with caption-side.
  • <thead>, <tbody> and <tfoot> divide rows into header, data and summary groups.
  • Order inside a table: caption, column groups, <thead>, <tbody> elements, <tfoot>.
  • Row groups make styling, printing and sticky headers much easier.

Next lesson: Spanning Rows and Columns.