Lesson 25 of 51 · Module 4, lesson 4 of 6 · 3 min

Your first table

Build a table from rows and cells, give it a title, and know when not to use one.

What you'll learn

  • When to use a table, and when not to
  • How to build a table from rows and cells
  • How to give a table a title with <caption>

Why this matters

Some information only makes sense in rows and columns: a timetable, a price list, results. A real HTML table lets a screen reader tell the listener which row and column each value belongs to. A layout faked with spaces or images does not.

Tables are for data only. Never use a table to place things side by side on a page. That is a job for CSS.

Rows and cells

A table is built row by row:

  • <table> holds the whole table.
  • <tr> is a table row.
  • <th> is a header cell (a label). Browsers show it bold and centred.
  • <td> is a data cell (a value).
  • <caption> is the table's title. It goes first, right after <table>.
html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Study plan</title>
</head>
<body>
  <h1>My study plan</h1>
  <table>
    <caption>Study time this week</caption>
    <tr>
      <th>Day</th>
      <th>Topic</th>
      <th>Minutes</th>
    </tr>
    <tr>
      <td>Monday</td>
      <td>Headings</td>
      <td>30</td>
    </tr>
    <tr>
      <td>Wednesday</td>
      <td>Links</td>
      <td>45</td>
    </tr>
    <tr>
      <td>Friday</td>
      <td>Images</td>
      <td>40</td>
    </tr>
  </table>
</body>
</html>

What you'll see: a small grid with the caption above it, bold headers in the first row, and no lines between the cells. Borders are a job for CSS; without them, the table looks plain, and that is correct for now.

The page "My study plan" with the caption "Study time this week" above a table. The first row holds the bold headers Day, Topic and Minutes. Three rows follow: Monday, Headings, 30; Wednesday, Links, 45; Friday, Images, 40. There are no borders.

How to read the code

Read a table like a book: one row at a time, left to right. Every row should have the same number of cells. Here, each <tr> has three: Day, Topic, Minutes.

The total study time is 30 + 45 + 40 = 115 minutes. Check your numbers when you build tables — readers do.

Tables are not for layout

Old websites used tables to put a menu next to the content. Do not do this:

  • Screen readers read layout tables as data, cell by cell, which is confusing.
  • The page cannot adapt to a phone screen.

If the content is not rows and columns of data, it is not a table.

Try it yourself

  1. Select the example and choose Try it.
  2. Add a row for Sunday.
  3. Add a fourth column, "Done?", with Yes or No in each row. Remember to add a header cell to the first row too.

Common mistakes

  • Rows with different numbers of cells. The columns no longer line up. Fix: count the cells in each <tr>.
  • Putting the caption somewhere else. It must be the first thing inside <table>. Fix: move it right after <table>.
  • Using a table for layout. Fix: use it only for data; use CSS for layout.
  • Text directly inside <tr>, outside a cell. The browser moves it out of the table. Fix: every piece of content is in a <th> or <td>.

Check yourself

  1. Which element is a row? Which is a data cell?
  2. Where does <caption> go?
  3. Should you use a table to put a menu beside your content?

Answers

  1. <tr> is a row; <td> is a data cell.
  2. First, right after the opening <table> tag.
  3. No. Tables are for data only.

Next: the header cells in this table work, but a screen reader has to guess which cells they describe. The next lesson removes the guesswork.

Chat with us on WhatsApp