← Back to HTML & CSS, page 2

html-css-0029 · Easy fresher

How do you structure an accessible HTML table?

Use tables for data in rows and columns, not for layout. A good table has a caption, thead, tbody and optional tfoot, th for header cells and td for data, and scope attributes so assistive technology links headers to cells. Wide tables can scroll inside a wrapper on small screens.

Full answer

Use a table for data that has rows and columns, such as marks, timetables or prices. Do not use it for page layout. Use CSS for layout.

A well-built table has:

  • caption: the title of the table, announced by screen readers.
  • thead, tbody and optionally tfoot: group header rows, body rows and summary rows.
  • th for header cells and td for data cells.
  • scope="col" on column headers, and scope="row" on row headers, so assistive tools know which cells each header describes.

A screen reader user on the cell with 64 hears something like "Physics, Marks, 64", because the row and column headers are linked. The table is announced with its caption, "Term 1 marks".

For very wide tables on small screens, wrap the table in a block with overflow-x: auto so the page itself does not scroll sideways. For complex tables with merged cells (colspan, rowspan), use id and headers attributes, or consider splitting into simpler tables.

html
<style>
  table { border-collapse: collapse; }
  th, td { border: 1px solid #999; padding: 6px 12px; text-align: left; }
  tfoot th, tfoot td { font-weight: bold; }
</style>
<table>
  <caption>Term 1 marks</caption>
  <thead>
    <tr><th scope="col">Subject</th><th scope="col">Marks</th><th scope="col">Out of</th></tr>
  </thead>
  <tbody>
    <tr><th scope="row">Maths</th><td>78</td><td>100</td></tr>
    <tr><th scope="row">Physics</th><td>64</td><td>100</td></tr>
  </tbody>
  <tfoot>
    <tr><th scope="row">Total</th><td>142</td><td>200</td></tr>
  </tfoot>
</table>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Tables are for tabular data; use CSS for layout.
  • caption names the table; thead, tbody and tfoot group rows.
  • th is for headers; scope="col" and scope="row" connect them to cells.
  • border-collapse: collapse gives single borders.
  • Wrap wide tables in a scrolling container instead of scrolling the page.

Common mistakes

  • using td with bold text instead of th for headers. It looks the same but carries no meaning.
  • building page layouts with tables. They do not adapt to screen sizes and make the reading order confusing.

Follow-ups

  • what is the difference between th scope="col" and scope="row"?
  • What do colspan and rowspan do?
  • How would you make a wide table usable on a phone?

Tips

  • Use border-collapse: collapse for a single clean border between cells. By default each cell draws its own.
  • Write a small marks table from memory including caption and scope.
  • Mention that complex tables with merged cells need headers and id attributes.