Lesson 6 / 875%

Lesson 6

Tables

18 min

table, tr, td, th: organize data.

When should you use a table?

A table is used to present data that is read in two directions: by row and by column. A timetable, a price comparison, school results or a set of statistics are good examples. The test is simple: if you can ask yourself "what is the value of this row for that column?", a table is the right fit. If your information is just a series of items, a list is enough.

The basic structure

The <table> tag wraps the whole table, <tr> ("table row") defines a row, and <td> ("table data") defines a cell.

simple-table.html
<table>
  <tr>
    <td>Alice</td>
    <td>28</td>
  </tr>
  <tr>
    <td>Bob</td>
    <td>34</td>
  </tr>
</table>

There is one important point to understand: in HTML, you describe a table row by row, never column by column. There is no "column" tag that would contain its cells: the number of columns simply results from the number of cells in each row. In the example above, each <tr> contains two<td> elements, so the table has two columns. All rows must have the same number of cells (counting merged cells), otherwise the table becomes irregular.

An invisible border?

If you test this code in your browser, you will see that the table has no border: the values are simply lined up. This is normal: HTML describes the structure, not the appearance. Borders, colors and spacing are set with CSS (see below).

Structuring with thead / tbody / tfoot

For a well-structured table, you separate the header, the body and, optionally, a footer. This makes the code more readable and helps assistive technologies understand the table.

full-table.html
<table>
  <thead>
    <tr>
      <th>Product</th>
      <th>Price</th>
      <th>Quantity</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Keyboard</td>
      <td>$49</td>
      <td>2</td>
    </tr>
    <tr>
      <td>Mouse</td>
      <td>$19</td>
      <td>1</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td colspan="2">Total</td>
      <td>$117</td>
    </tr>
  </tfoot>
</table>
  • <thead> groups the header row.
  • <tbody> groups the data rows.
  • <tfoot> groups a summary row (total, average...).
  • <th> ("table header") replaces <td> for header cells: displayed in bold and centered by default.

Even if you write <tfoot> before or after the<tbody> in the code, it is always displayed at the bottom of the table. Another advantage of this organization: on a very long table printed over several pages, the browser can repeat the header on each page, and CSS can precisely target each section.

Header cells and the scope attribute

A <th> cell is not just a bold cell: it indicates that its content describes the neighboring cells. So that screen readers know whether the header applies to a column or a row, you add the scope attribute. When the user moves into a cell, the screen reader then announces its header (for example "Price: $49").

scope.html
<table>
  <thead>
    <tr>
      <th scope="col">Student</th>
      <th scope="col">Math</th>
      <th scope="col">English</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Hery</th>
      <td>15</td>
      <td>12</td>
    </tr>
    <tr>
      <th scope="row">Lalaina</th>
      <td>13</td>
      <td>17</td>
    </tr>
  </tbody>
</table>
  • scope="col" — the header applies to the whole column below it.
  • scope="row" — the header applies to the whole row to its right, like the student's name here.

Merging cells

Two attributes let you merge cells together: colspan(horizontal merge) and rowspan (vertical merge).

merging.html
<table>
  <tr>
    <th>Day</th>
    <th>Morning</th>
    <th>Afternoon</th>
  </tr>
  <tr>
    <td>Monday</td>
    <td rowspan="2">HTML class</td>
    <td>Exercises</td>
  </tr>
  <tr>
    <td>Tuesday</td>
    <td>Project</td>
  </tr>
  <tr>
    <td colspan="3">Wednesday: day off</td>
  </tr>
</table>

The attribute's value indicates the number of slots occupied. Here, "HTML class" takes up two rows vertically: that is why the following row contains only two cells ("Tuesday" and "Project"), the morning slot already being taken by the cell above. Likewise, the last row contains only one cell, which spans all three columns.

A method for merging successfully

  • First draw your table on paper as a grid, then circle the cells to merge.
  • Write the merged cell in the first slot of the area, withcolspan or rowspan.
  • Remove the covered cells: they must no longer appear in the code.
  • Check that every row adds up to the same number of columns.

Captioning a table

The <caption> tag adds a descriptive title to your table, useful for accessibility.

caption.html
<table>
  <caption>Sales breakdown by month</caption>
  <thead>
    <tr>
      <th>Month</th>
      <th>Sales</th>
    </tr>
  </thead>
  <tbody>
    <tr><td>January</td><td>120</td></tr>
    <tr><td>February</td><td>98</td></tr>
  </tbody>
</table>

The <caption> must be the first child of the <table> tag, right after its opening. Screen readers announce it before the content, which lets the user decide whether to go through the table or skip to the next one.

Adding some style with CSS

Without styling, a table is hard to read. Here is a preview of what CSS can do with just a few lines (you will learn the details in the CSS course):

styled-table.css
table {
  border-collapse: collapse; /* merges adjacent borders */
  width: 100%;
}

th,
td {
  border: 1px solid #cbd5e1;
  padding: 0.5rem 0.75rem;
  text-align: left;
}

thead th {
  background-color: #ede9fe;
}

tbody tr:nth-child(even) {
  background-color: #f8fafc; /* alternating rows */
}

Keep two points in mind. First, styling is never set in the HTML tags: the old attributes like border, cellpadding or bgcolor are obsolete. Second, having properly structured your table with thead and tbody lets you style the header and the data separately.

Tables on phones

A table with many columns quickly exceeds the width of a phone screen and forces the entire page to scroll horizontally. The classic solution is to wrap the table in a container that scrolls on its own, without touching the table's structure:

scrollable-table.html
<div style="overflow-x: auto;">
  <table>
    <!-- A table with many columns -->
  </table>
</div>

What NOT to do

Tables should only be used to display tabular data (prices, statistics, schedules...), never to lay out an entire page with columns and menus — this was a common practice in the 2000s, now completely replaced by CSS (Flexbox, Grid).

Common beginner mistakes

  • Forgetting to close a </tr> or a </td>: the table displays crookedly with no error message.
  • Writing rows with a different number of cells, for example after a badly calculated merge.
  • Using a bold <td> instead of <th> for the header: the table loses its meaning for screen readers.
  • Placing text or another tag directly inside <table> or <tr>, outside of a cell.
  • Using a table to align buttons or position layout elements.
  • Forgetting the <caption> on a table whose subject isn't obvious.

Key takeaways

  • A table is described row by row: table contains tr elements, which contain td and th elements
  • A complete table is organized with thead, tbody and tfoot
  • th with scope andcaption make the table understandable for screen readers
  • colspan and rowspan merge cells; every row must remain consistent
  • Appearance (borders, colors, spacing) is CSS's job, and a site's layout should never rely on tables

Exercise

Create a table showing your weekly schedule: a "Day" column and a "Main activity" column, with a clear header.

Going further

Add a caption to your table with <caption>, then use colspan to merge two cells on a "weekend: rest" row.

Challenge: a report card

Build a report card table with three subjects and four students. Use <thead>, <tbody> and <tfoot> to display the average for each subject, <th> cells with scope="col" and scope="row", and a <caption>. Then add a cell merged over two rows to group two students in the same class. Finish by creating a CSS file that adds borders and alternating rows.