Skip to content
Codewilds

Lesson 9 of 29 · Page Structure & Forms

HTML Tables: table, tr, th, td, thead and caption

medium20 XP2 min read

Some information is naturally a grid: timetables, price lists, sports results, a game's high-score board. HTML tables show this kind of tabular data in rows and columns — and when they're built properly, a screen reader can tell its listener exactly which column every value belongs to.

Rows and cells

A table is built row by row. <table> wraps everything, each <tr> (table row) is one row, and each <td> (table data) is one cell in that row:

<table>
  <tr>
    <td>Ada</td>
    <td>Mage</td>
  </tr>
  <tr>
    <td>Linus</td>
    <td>Knight</td>
  </tr>
</table>

There's no element for a column: the first cell of every row forms the first column, the second cell the second column, and so on. That's why every row needs the same number of cells.

Header cells

A <th> (table header) is a cell that labels a column or a row. Browsers show it in bold and centred — and, more importantly, screen readers announce the header together with each value: "Class: Mage" instead of just "Mage".

Caption, thead and tbody

Real tables get three more elements:

  • <caption> is the title of the table. It must be the first thing inside <table>.
  • <thead> groups the header row (or rows).
  • <tbody> groups the data rows. There's also <tfoot>, for a row of totals at the end.
<table>
  <caption>High scores</caption>
  <thead>
    <tr>
      <th>Player</th>
      <th>Score</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Ada</td>
      <td>1200</td>
    </tr>
  </tbody>
</table>

These groups make the structure clear to assistive technology, let the browser repeat the header row on every printed page, and give you handy hooks for styling. If you leave out <tbody>, the browser quietly adds one for you.

Out of the box, a table has no borders or stripes. You'll style tables with CSS later in the course.

Tables are for data, not layout

Long ago, developers used tables to arrange whole pages into columns. Don't: screen readers will read your layout out as if it were data, and the page falls apart on small screens. Use tables only for real tabular data — page layout is a job for CSS, with Flexbox and Grid, which you'll meet later in this course.

Common mistakes

  • Cells without a row. Every <td> and <th> must sit inside a <tr>.
  • Rows of different lengths. A row with two cells next to a row with three makes ragged, confusing columns.
  • Plain <td> cells as headers. Bold text in a <td> only looks like a header; use <th> so it means one.
  • The caption in the wrong place. <caption> comes right after the opening <table> tag, before any rows.

Assignment

Build the guild leaderboard inside <table>:

  1. A <caption> with the text Guild leaderboard.
  2. A <thead> with one row of three <th> cells: Player, Class, Level.
  3. A <tbody> with three rows of <td> cells, in this order:
Player Class Level
Grace Ranger 15
Ada Mage 12
Linus Knight 9

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Keep learning

Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.

Lesson last updated on .

Write code ↓
HTML & CSS+20 XP

Lesson 9 is part of the full course. The first 7 lessons are free.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.