Thuta Learning
HTML
IntermediateWeb DevelopmentBeginner

Tables

HTMLLesson 1930 min

What you'll walk away with

  • Tell when a table is right (two-dimensional data) and when it is not (page layout)
  • Write header cells and data cells correctly with tr, th and td
  • Structure a table into parts with caption, thead, tbody and tfoot
  • Work out how many cells a row needs when colspan or rowspan is used

Explain it simply

Explain it simply

Picture a school mark sheet or a train timetable. The number "85" in a cell only means something once you know which student (row) and which subject (column) it belongs to. For data that gets its meaning from both a row and a column, HTML has the table element.

A table is built from rows (tr), and each row holds cells: th for a heading cell, td for a data cell. You never write columns separately; the first cells of every row line up to form the first column.

Problem → Why → Solution

The problem: a table drawn with divs

Pricing data is laid out with divs in a CSS grid so it looks like a table. It looks fine, but a screen reader user who hears "$19" has no idea which plan or which column it belongs to, and cannot move by row or column with table navigation keys. Copying the data into a spreadsheet produces one long line of text.

pricing.html
<div class="grid">
  <div class="head">Plan</div>  <div class="head">Price</div>
  <div>Free</div>              <div>$0</div>
  <div>Pro</div>               <div>$19</div>
</div>

What does table add?

A div is a box with no meaning. table, th and td put the relationship "this cell belongs to this row and this column header" into the browser's accessibility tree, so a screen reader can read a cell together with its header, such as "Price, $19".

Core concept: build from rows, give meaning with headers

A table has four basic parts: table is the whole grid, tr is a row, th is a header cell (bold and centred by default), and td is a data cell. Every row must add up to the same number of cells; if one does not, the columns shift and data ends up under the wrong header. Cells may hold more than plain text, such as a link, a button or an image, but never a whole page section.

ElementMeaning
captionThe table's title; must be the first child of table
theadThe group of rows holding the column headers
tbodyThe group of data rows (there can be several)
tfootSummary rows such as totals or averages

thead, tbody and tfoot group the rows into sections. When a long table is printed, browsers can repeat the thead on every page, and CSS can target each section separately. Even if you never write tbody, the HTML parser wraps the rows in one automatically, so the DOM contains table > tbody > tr, never table > tr.

The browser's default style has no borders. Instead of the old border="1" attribute, use CSS: border-collapse: collapse; merges the doubled cell borders into one line, and th, td { border: 1px solid; padding: ...; } gives every cell a line and some breathing room.

Tables are for data only. Using a table for page layout, such as putting a sidebar next to the content, is a technique from around the year 2000: screen readers present the layout as if it were a data grid, and it cannot reflow on a narrow phone screen. Use CSS Grid and Flexbox for layout.

Mental model: rows first, then cells
  1. Check the data is rows × columns
  2. Name the table with a caption
  3. Put th headers in a thead row
  4. Give every tbody row equal cells

The browser builds columns from the order of cells in each row, which is why the cell count has to be right.

General pattern

html
<table>
  <caption>What this table shows</caption>
  <thead>
    <tr><th>Header A</th><th>Header B</th></tr>
  </thead>
  <tbody>
    <tr><td>Value A1</td><td>Value B1</td></tr>
  </tbody>
  <tfoot>
    <tr><td>Summary A</td><td>Summary B</td></tr>
  </tfoot>
</table>

caption comes first, then thead, tbody and tfoot. Every row contains one cell per column.

Example: a subscription plan table

This table draws its borders with CSS rather than the outdated border="1" attribute, and it has a caption plus thead and tbody sections, so both its structure and its look are explicit. Notice which cells are th and which are td.

plans.html
<style>
  table { border-collapse: collapse; }
  th, td { border: 1px solid #999; padding: 0.4rem 0.8rem; text-align: left; }
</style>

<table>
  <caption>Subscription plans</caption>
  <thead>
    <tr>
      <th>Plan</th>
      <th>Price per month</th>
      <th>Projects</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Free</td>
      <td>$0</td>
      <td>3</td>
    </tr>
    <tr>
      <td>Pro</td>
      <td>$19</td>
      <td>Unlimited</td>
    </tr>
  </tbody>
</table>

Code breakdown

  1. CSS borderstable { border-collapse: collapse; }

    Removes the default 2px gap between neighbouring cell borders so they merge into a single line.

  2. caption<caption>Subscription plans</caption>

    The table's title. Screen readers announce it when the user enters the table; the next lesson looks at it in detail.

  3. Header row<th>Price per month</th>

    th marks a column header. "Price per month" states the unit, so the cells below do not need to repeat /month.

  4. Data row<td>Unlimited</td>

    Each tbody row has three td cells, one per header. "Unlimited" lands under the Projects column.

  5. Sections<tbody>

    Separates the data rows from the header row. The browser would add it anyway, but once there is a thead, writing tbody yourself is clearer.

You should see
A table with the caption "Subscription plans" above it, 3 columns and 3 rows:
Plan | Price per month | Projects   (bold)
Free | $0 | 3
Pro | $19 | Unlimited
Every cell has a single grey border and left-aligned text.

colspan, rowspan and tfoot

colspan="2" makes a cell two columns wide and rowspan="2" makes it two rows tall. Because the spanned cell occupies that space, its row (or the row below) needs fewer td elements. In the example below, "Monday" spans two rows, so the CSS row beneath it has only two cells; and the "Total" cell in the tfoot spans two columns, so that row has only two cells as well.

study-hours.html
<table>
  <caption>Study hours this week</caption>
  <thead>
    <tr><th>Day</th><th>Subject</th><th>Hours</th></tr>
  </thead>
  <tbody>
    <tr><td rowspan="2">Monday</td><td>HTML</td><td>2</td></tr>
    <tr><td>CSS</td><td>1</td></tr>
    <tr><td>Tuesday</td><td>JavaScript</td><td>3</td></tr>
  </tbody>
  <tfoot>
    <tr><th colspan="2">Total</th><td>6</td></tr>
  </tfoot>
</table>

Use merged cells sparingly

With many spans, screen readers struggle to work out which header belongs to which cell, and the table is harder to adapt for phones. Splitting one complex table into two simple ones is usually better.

In real projects

Where this shows up in real projects

An admin dashboard's order list, a bank statement, a pricing comparison, a sports league standings table, the parameter table in API documentation: all of these are jobs for table. Dashboards add JavaScript features such as sorting by clicking a column header or selecting rows, but underneath they keep table, thead and tbody, which keeps them accessible and makes copy-paste into a spreadsheet land in the right columns.

On a narrow phone screen a table with many columns tends to overflow. The most common fix is wrapping the table in <div style="overflow-x: auto"> so only the table scrolls sideways, not the whole page. You will also see that wrapper given tabindex="0" with role="region" and an aria-label so keyboard users can scroll it.

Common mistakes

Common mistakes

1. Writing the header row with td and making it bold

<td><b>Price</b></td> looks like a header but is still a data cell to the browser, so screen readers do not pair other cells with it. Use th for headers and style them with CSS.

2. Not removing a cell after adding colspan

In a 3-column table, adding <td colspan="2"> and then two more td cells makes that row 4 columns wide, and a strange extra column appears on the right. The spans in every row must add up to the column count.

3. Writing table > tr in CSS

Because the parser inserts tbody automatically, the selector table > tr matches nothing and the CSS seems not to work. Use tr or tbody tr instead.

Best practices

Best practices

  • Put units in the header (Price per month, Weight (kg)) so no cell has to repeat them.
  • Right-align numeric columns with CSS so decimal places line up and values are easy to compare.
  • Do not leave cells empty; write something meaningful such as "—" or "Not included".
  • Wrap wide tables in an overflow-x: auto container and test them at a 320px phone width.

Mini challenge

Mini challenge: a school-supplies receipt

Write an online shop's receipt as a table with three items, three columns and a total row.

  • A caption containing "Receipt", and Item, Qty, Amount headers inside thead
  • tbody rows for Notebook (2, $7.00), Pen (3, $4.50) and Backpack (1, $25.00)
  • A tfoot row with a "Total" cell spanning two columns and $36.50
  • Borders come from CSS; no border attribute
receipt.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Receipt</title>
  <!-- TODO: collapsed borders and cell padding -->
</head>
<body>
  <table>
    <!-- TODO: caption -->
    <!-- TODO: thead with three th -->
    <!-- TODO: tbody with three item rows -->
    <!-- TODO: tfoot: Total spanning two columns, then the sum -->
  </table>
</body>
</html>

Expected: Under the caption "Receipt — 12 May": the header Item | Qty | Amount, rows Notebook | 2 | $7.00, Pen | 3 | $4.50, Backpack | 1 | $25.00, and at the bottom a two-column-wide "Total" followed by $36.50.

Show hint

The tfoot cell with colspan="2" takes two columns, so the row needs only one more cell. $7.00 + $4.50 + $25.00 = $36.50.

Show solution
receipt.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Receipt</title>
  <style>
    table { border-collapse: collapse; }
    th, td { border: 1px solid #999; padding: 0.3rem 0.7rem; }
    td:last-child { text-align: right; }
  </style>
</head>
<body>
  <table>
    <caption>Receipt — 12 May</caption>
    <thead>
      <tr><th>Item</th><th>Qty</th><th>Amount</th></tr>
    </thead>
    <tbody>
      <tr><td>Notebook</td><td>2</td><td>$7.00</td></tr>
      <tr><td>Pen</td><td>3</td><td>$4.50</td></tr>
      <tr><td>Backpack</td><td>1</td><td>$25.00</td></tr>
    </tbody>
    <tfoot>
      <tr><th colspan="2">Total</th><td>$36.50</td></tr>
    </tfoot>
  </table>
</body>
</html>

Quick quiz 1 / 3

In a 3-column table, a row starts with <td colspan="2">Subtotal</td>. How many more td cells does that row need?

Quick quiz 2 / 3

After the browser parses <table><tr><td>A</td></tr></table>, what does the CSS table > tr { color: red; } do?

Quick quiz 3 / 3

Which content should be marked up as a table?

What to remember

  • Use table only for data that gets its meaning from both a row and a column; use CSS Grid/Flexbox for layout.
  • tr is a row, th a header cell, td a data cell, and every row must add up to the same number of cells, spans included.
  • caption, thead, tbody and tfoot name the table and split it into sections; the parser always adds a tbody.
  • Draw borders and spacing with CSS (border-collapse, border, padding), not the border attribute.

HTML Living Standard — Tables — WHATWG

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Tables | Thuta Learning