Skip to content
Kuza Ndoto Academy

Tables

BeginnerChapter 3 · Page 15 of 254 min readUpdated 15 Nov 2026

Tables present data in rows and columns: timetables, price lists, exam results, league standings. HTML tables have a clear structure of rows, header cells and data cells, and when you mark them up properly, screen readers can announce which row and column each value belongs to.

A basic table

<table>
  <tr>
    <th>Route</th>
    <th>Peak</th>
    <th>Off-peak</th>
  </tr>
  <tr>
    <td>Rongai – CBD</td>
    <td>150</td>
    <td>100</td>
  </tr>
</table>
  • <table> wraps the whole table.
  • <tr> (table row) holds one row of cells.
  • <th> (table header) is a heading cell. Browsers make it bold and centred.
  • <td> (table data) is an ordinary data cell.

There is no element for columns in the basic structure: columns appear because each row has the same number of cells, lined up in order.

Full structure: caption, thead, tbody, tfoot

Real tables should also have a caption and row groups:

<table>
  <caption>Example matatu fares (KES)</caption>
  <thead>
    <tr>
      <th scope="col">Route</th>
      <th scope="col">Peak</th>
      <th scope="col">Off-peak</th>
    </tr>
  </thead>
  <tbody>
    <tr><th scope="row">Rongai – CBD</th><td>150</td><td>100</td></tr>
    <tr><th scope="row">Thika – CBD</th><td>200</td><td>120</td></tr>
    <tr><th scope="row">Kikuyu – CBD</th><td>100</td><td>70</td></tr>
  </tbody>
</table>
Example matatu fares (KES) Route Peak Off-peak Rongai – CBD 150 100 Thika – CBD 200 120 Kikuyu – CBD 100 70 <caption> <thead>: one row of <th> <tbody>: three <tr> first cell of each row is a row header (th scope="row") Gold outline: one <td> data cell. Each horizontal band is a <tr>.
Figure 1. The caption names the table, <thead> holds the column headings and <tbody> holds the data rows.
  • <caption> must be the first child of <table>. It's the table's title, shown above it by default, and screen readers announce it when users reach the table.
  • <thead>, <tbody> and <tfoot> group the header rows, body rows and footer rows (such as totals). They help CSS and accessibility, and when printing a long table some browsers repeat the header on every page.
  • scope="col" says a header applies to the cells below it; scope="row" says it applies to the cells to its right. In simple tables browsers can often work this out, but stating it removes the guesswork.
Note

If you leave out <tbody>, the browser adds one to the DOM automatically. That's why CSS like table > tr often doesn't match anything: the rows really live inside an invisible tbody.

Spanning rows and columns

A cell can stretch across several columns with colspan, or several rows with rowspan.

<table>
  <caption>Saturday coding club</caption>
  <tr><th scope="col">Time</th><th scope="col">Room A</th><th scope="col">Room B</th></tr>
  <tr><th scope="row">9:00</th><td>HTML basics</td><td rowspan="2">Python project lab</td></tr>
  <tr><th scope="row">10:00</th><td>CSS basics</td></tr>
  <tr><th scope="row">11:00</th><td colspan="2">Lunch break for everyone</td></tr>
</table>

"Python project lab" fills Room B for both 9:00 and 10:00, which is why the 10:00 row has only one <td>. "Lunch break for everyone" stretches across both rooms. When you use spans, count carefully: every row must still add up to the same number of columns.

Tables are for data, not layout

Years ago, developers used tables to arrange whole page layouts into columns. Don't. Layout tables confuse screen readers (which announce rows and columns that mean nothing), don't adapt to phone screens and are hard to maintain. Use Flexbox or Grid for layout and keep tables for real tabular data.

Common mistake

Using old presentational attributes such as border="1", bgcolor, align or cellpadding. They are obsolete in modern HTML. Use CSS (border, background, text-align, padding) instead, as in the example below.

Try it

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>League table</title>
    <style>
      table { border-collapse: collapse; }
      th, td { border: 1px solid #999; padding: 6px 10px; }
      thead th { background: #1f3a5f; color: white; }
      tfoot td { font-style: italic; }
    </style>
  </head>
  <body>
    <table>
      <caption>Inter-school football league</caption>
      <thead>
        <tr><th scope="col">Team</th><th scope="col">Played</th><th scope="col">Points</th></tr>
      </thead>
      <tbody>
        <tr><th scope="row">Kisumu Boys</th><td>5</td><td>13</td></tr>
        <tr><th scope="row">Jinja College</th><td>5</td><td>10</td></tr>
        <tr><th scope="row">Arusha Academy</th><td>5</td><td>7</td></tr>
      </tbody>
      <tfoot>
        <tr><td colspan="3">3 points for a win, 1 for a draw</td></tr>
      </tfoot>
    </table>
  </body>
</html>

The preview shows a bordered table with its caption centred above, white headings on a dark blue band, three team rows whose names are bold (they are row headers) and an italic footer note spanning all three columns. Try adding a fourth team, or remove the CSS to see the browser's plain default table.

Key points

  • A table is built from <tr> rows containing <th> header cells and <td> data cells.
  • Add a <caption> and group rows with <thead>, <tbody> and <tfoot>.
  • scope="col" and scope="row" tell assistive technology what each header labels.
  • colspan and rowspan merge cells; use tables only for data, and style them with CSS.
Up next · New chapter: FormsHTML FormsHow forms collect and send data.
→

Tip: use the ← and → keys to move between pages.