Tables — HTML

You write a table row by row: <table> wraps everything, <tr> is one row, <th> is a heading cell, and <td> is a data cell. The heading row goes in <thead>, and…

On the wall of a food stall there is usually a price list: a Menu column, a Price column, and one row for each dish. That price list is a table: data arranged in rows and columns. A class timetable is a table too.

In HTML, a table is built from the inside out. Cells (the boxes inside a table) are grouped into a row, and the rows are grouped inside a <table>. You never write the columns themselves. They come from the order of the cells in each row.

A table is written row by row

<table>
  <thead>
    <tr>
      <th scope="col">Menu</th>
      <th scope="col">Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Fried Rice</td>
      <td>$3</td>
    </tr>
    <tr>
      <td>Sweet Iced Tea</td>
      <td>$1</td>
    </tr>
  </tbody>
</table>

Start with the four main tags. <thead>, <tbody> and scope come after this:

Every <th> and <td> must be inside a <tr>. Every row should have the same number of cells, so the columns stay aligned. The result of the code above: a table with two columns, Menu and Price, and two menu rows below them.

Separate headings from content: <thead> and <tbody>

The code above also uses two wrappers:

Inside both, there are still <tr> rows with their cells. So the wrapping order, from the outside to the inside, is: <table>, then <thead> or <tbody>, then <tr>, then <th> or <td>.

With or without <thead> and <tbody>, the table looks the same. But browsers, screen readers (programs that read pages aloud for blind users) and search engines can now recognise which part is the heading and which is the content. When a long table is printed over several pages, the browser can repeat the <thead> row at the top of each page.

scope: a column heading or a row heading

With scope, a screen reader can read the heading together with each cell, for example "Price, $3". The title of the whole table goes in <caption>, right after <table>.

Here is an order list with a <caption> and a total row:

<table>
  <caption>Table 3 Order</caption>
  <tr><th scope="col">Menu</th><th scope="col">Qty</th><th scope="col">Price</th></tr>
  <tr><td>Fried Rice</td><td>2</td><td>$6</td></tr>
  <tr><th scope="row" colspan="2">Total</th><td>$6</td></tr>
</table>

The title Table 3 Order shows above the table. colspan="2" makes the Total cell two columns wide, Menu and Qty. That is why the total row has only two cells, not three. For a cell that is several rows tall, use rowspan.

⚠️ Columns with gaps, fake headings, and tables used for layout

💡 Wide tables on a phone screen. Wrap the table in <div style="overflow-x: auto">. The style attribute holds CSS rules for that tag. With this rule, the table can be scrolled sideways inside that <div>, while the page itself stays in place.

TL;DR: You write a table row by row: <table> wraps everything, <tr> is one row, <th> is a heading cell, and <td> is a data cell. The heading row goes in <thead>, and the data rows go in <tbody>. scope="col" marks a column heading, and scope="row" marks a row heading. In the exercises, type cell text exactly as asked, including capital letters.

⚡ Row after row

Write:

<table>
  <tr>
    <th>Menu</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>Fried Rice</td>
    <td>$3</td>
  </tr>
</table>
Guess first: which tag makes the columns?

None. Columns come from the order of the cells (the boxes inside a table) in each row: the first cell forms the first column, the second cell forms the second column.

Meaning: <tr> is a row, <th> is a heading cell (bold, centred), <td> is a data cell. Every row should have the same number of cells, so the columns stay aligned.

⚡ Separate headings from content

Write:

<table>
  <thead>
    <tr>
      <th scope="col">Menu</th>
      <th scope="col">Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Fried Rice</td>
      <td>$3</td>
    </tr>
  </tbody>
</table>

Meaning: <thead> wraps the heading row, and <tbody> wraps the data rows. The table looks the same as without them, but browsers and screen readers can now recognise the heading row. When a long table is printed, the browser can repeat the <thead> row at the top of each page.

⚡ A column heading or a row heading

Meaning: scope="col" marks a column heading, and scope="row" marks a row heading. With scope, a screen reader reads the heading with each cell: "Price, $3".

In the exercises, type cell text exactly as asked, including capital letters: table and Table count as different.

What you will learn