BeginnerLesson 8 of 307 min read

HTML tables: rows, headers, merged cells and accessibility

Build accessible data tables with captions, header groups, merged cells and mobile-friendly wrappers.

In this lesson you will

  • Build a table row by row
  • Group header, body and footer
  • Merge cells with colspan and rowspan
  • Make tables accessible and mobile friendly

About 12 minutes

When to use a table

A table shows data arranged in rows and columns: prices, timetables, scores, comparisons. Tables are for information that makes sense in a grid. In the early web they were also used for page layout, which is now considered a serious mistake. Layout is done with CSS. If your content would not look right as a spreadsheet, it is not a table.

The basic parts

<table>
  <tr>
    <th>Course</th>
    <th>Level</th>
    <th>Lessons</th>
  </tr>
  <tr>
    <td>HTML</td>
    <td>Beginner</td>
    <td>31</td>
  </tr>
  <tr>
    <td>CSS</td>
    <td>Beginner</td>
    <td>30</td>
  </tr>
</table>
Element Meaning
<table> The whole table
<tr> A table row
<th> A header cell (bold and centred by default)
<td> A normal data cell

You build a table row by row. Every row should have the same number of cells unless you merge some on purpose.

Group the parts: thead, tbody, tfoot and caption

<table>
  <caption>Monthly savings plan</caption>
  <thead>
    <tr><th scope="col">Month</th><th scope="col">Saved</th></tr>
  </thead>
  <tbody>
    <tr><td>January</td><td>4,000</td></tr>
    <tr><td>February</td><td>4,000</td></tr>
  </tbody>
  <tfoot>
    <tr><th scope="row">Total</th><td>8,000</td></tr>
  </tfoot>
</table>
  • caption gives the table a title that stays attached to it.
  • thead, tbody and tfoot group the header, body and footer rows. They let CSS style each part and let long printed tables repeat the header on every page.
  • scope="col" or scope="row" tells assistive technology whether a header cell labels a column or a row.

Merging cells

Two attributes stretch a cell. colspan makes it cover several columns and rowspan several rows.

<table>
  <tr><th colspan="2">Contact</th></tr>
  <tr><td>Email</td><td>hi@example.com</td></tr>
  <tr><td>Phone</td><td>0123 456 789</td></tr>
</table>
Common mistake

When you merge, the other cells that the merged cell covers must be left out of the row. If you keep them, the table grows an extra column and everything slides sideways. Count the cells in every row to check.

Making tables readable

By default a table has no borders and cells touch. Your CSS lesson will style it properly, but here is a starter you can use right away, plus a live editor to experiment with.

table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ccc; padding: 8px 12px; text-align: left; }
th { background: #f3f0ff; }

Build a table with a caption and a header row, then three rows of data about your favourite films, books or songs. A style block is included to give it borders.

<style>
 table{border-collapse:collapse}
 th,td{border:1px solid #bbb;padding:6px 10px}
 th{background:#f1ecff}
</style>

Start with <table>, then <caption>, then a <tr> of <th> cells, then rows of <td>.

<style>
 table{border-collapse:collapse}
 th,td{border:1px solid #bbb;padding:6px 10px}
 th{background:#f1ecff}
</style>
<table>
  <caption>My favourite books</caption>
  <tr><th>Title</th><th>Author</th></tr>
  <tr><td>Wings of Fire</td><td>A. P. J. Abdul Kalam</td></tr>
  <tr><td>The Alchemist</td><td>Paulo Coelho</td></tr>
</table>

Tables on small screens

Wide tables are the number one cause of sideways scrolling on phones. The easiest fix is to wrap the table in a container that scrolls on its own, so only the table moves and not the whole page.

<div style="overflow-x:auto">
  <table>...</table>
</div>
Good to know

Design wide tables with the most important column first. A visitor who only sees the first two columns on a phone should still get the key information.

Accessibility checklist

  • Use th for headers, never a bold td.
  • Add caption so people know what the table is about.
  • Keep tables simple. Complex tables with several merged headers are hard for screen readers.
  • Do not use empty cells just to create space.

Practice exercises

  1. Build a weekly study timetable with days across the top and time slots down the side.
  2. Add a caption, thead, tbody and scope attributes.
  3. Merge two cells with colspan for a break time.
  4. Copy the starter CSS and try changing the header colour.

Worked example: a price comparison table

This table combines everything: a caption, grouped sections, scoped headers and a merged footer note. Study how each part earns its place.

<table>
  <caption>Website hosting plans compared</caption>
  <thead>
    <tr>
      <th scope="col">Plan</th>
      <th scope="col">Websites</th>
      <th scope="col">Storage</th>
      <th scope="col">Price per month</th>
    </tr>
  </thead>
  <tbody>
    <tr><th scope="row">Starter</th><td>1</td><td>10 GB</td><td>$3</td></tr>
    <tr><th scope="row">Standard</th><td>10</td><td>50 GB</td><td>$6</td></tr>
    <tr><th scope="row">Pro</th><td>100</td><td>200 GB</td><td>$12</td></tr>
  </tbody>
  <tfoot>
    <tr><td colspan="4">Prices are examples for practice.</td></tr>
  </tfoot>
</table>

A screen reader user moving through the Standard row hears: “Plan Standard, Websites 10, Storage 50 GB”. Because every row begins with a th scope="row", the table is understandable cell by cell. This is what the scope attribute buys you, and it costs only a few characters.

Columns that behave: colgroup

If you want to style a whole column without adding a class to every cell, use colgroup and col before the first row. They are rarely needed in beginner work, but they are worth knowing. For example <col span="2" style="background:#F7F4FF"> tints two columns at once.

Choosing between a table and a list

Ask yourself: does each piece of information need two labels, a row and a column? A timetable does, since each cell depends on both the day and the hour. A set of features does not, because each is just a single line. When in doubt, try writing the content as a spreadsheet. If it fits naturally, use a table. If you find yourself inventing empty columns, use a list.

Quick reference

You want to Use
Give the table a title caption
Mark a column heading th scope="col"
Mark a row heading th scope="row"
Join cells sideways colspan
Join cells downward rowspan
Stop sideways scroll on phones Wrap in a div with overflow-x: auto

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

Which element creates a header cell?

  1. td
  2. th
  3. tr
  4. thead only

th marks a header cell. thead groups header rows.

What should tables be used for?

  1. Page layout
  2. Tabular data in rows and columns
  3. Placing images side by side
  4. Making menus

Tables are for data. Layout belongs to CSS.

Which attribute merges a cell across two columns?

  1. rowspan="2"
  2. colspan="2"
  3. merge="2"
  4. span="2"

colspan stretches a cell horizontally across columns.

What does caption do?

  1. Adds a footnote
  2. Gives the table a title tied to it
  3. Sets a border
  4. Hides the table

It describes the table and is read by assistive tools.

Why do wide tables break phone layouts?

  1. Phones cannot show tables
  2. They are wider than the screen and force sideways scrolling
  3. Tables are blocked on mobile
  4. They need images

Wrapping the table in a scrollable container keeps the page itself steady.

Summary

  • Tables are for data, not for layout.
  • th for headers, td for data, caption for the title.
  • thead, tbody and tfoot group rows; scope helps assistive tools.
  • Wrap wide tables in a scrolling container.

Next lesson

Div, span, classes and ids: the hooks for CSS and JavaScript.

Questions

Why not use tables for layout?

They confuse screen readers, do not adapt to phones, and are hard to change. CSS grid and flexbox do the job better.

Do I need thead and tbody?

The page works without them, but they give structure, enable styling and improve accessibility, so professionals include them.

How do I add borders?

With CSS. Use border-collapse: collapse on the table and a border on th and td, as in the starter above.

Can a table cell contain images or lists?

Yes. Cells can hold any content, including links, images and lists.

Progress is saved in this browser.