Online admissions open for the SEO, web development and networking & cybersecurity courses. Enquire for the next batch.

HTML Tables: Structure, Accessibility and Responsive Design

5 min read Updated By the SkillNewton team

What is an HTML table?

An HTML table presents tabular data, information that has a meaningful relationship across rows and columns, using the <table> element with rows (<tr>), header cells (<th>) and data cells (<td>). Correct markup with a caption and scoped headers lets screen readers announce which row and column each value belongs to.

Table elements and terminology

ElementRole
<table>The container for the whole table
<caption>The table's title; must be the first child of the table
<thead>, <tbody>, <tfoot>Group header rows, body rows and summary rows
<tr>A table row
<th>A header cell that labels a row or column
<td>A data cell
<colgroup> / <col>Apply widths or backgrounds to whole columns

Cells can span more than one column or row with colspan and rowspan. Every row should still add up to the same number of columns, or the table will look broken and be hard to navigate.

A well-structured table with code

<table>
  <caption>Monthly website visits by channel, 2026</caption>
  <thead>
    <tr>
      <th scope="col">Month</th>
      <th scope="col">Organic search</th>
      <th scope="col">Social</th>
      <th scope="col">Email</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">January</th>
      <td>4,210</td><td>1,050</td><td>620</td>
    </tr>
    <tr>
      <th scope="row">February</th>
      <td>4,580</td><td>980</td><td>710</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th scope="row">Total</th>
      <td>8,790</td><td>2,030</td><td>1,330</td>
    </tr>
  </tfoot>
</table>

The numbers here are sample data for practice. Notice that the first cell of each body row is a <th scope="row">: the month labels its row in the same way the top row labels each column.

The grouping elements are more than tidy markup. Browsers repeat <thead> and <tfoot> rows on every printed page when a long table breaks across pages, CSS can target body rows alone with tbody tr, and JavaScript can sort or filter the body without touching the headers. A table may also contain several <tbody> elements to separate groups of rows, such as one per quarter.

How screen readers read tables: caption and scope

A sighted reader glances up to the column header and across to the row header. A screen reader user moves cell by cell, and the software announces the relevant headers as they go, for example "February, Social, 980". That only works when headers are marked as <th> rather than bold <td> cells.

  • scope="col" says the header labels the cells below it.
  • scope="row" says the header labels the cells to its right.
  • <caption> names the table, so users can decide whether to explore it or skip it. Style or reposition it with CSS rather than removing it.

Complex tables with headers and id

When a table has two levels of headers, such as a heading that spans several columns above sub-headings, give each <th> an id and list the relevant ids in each data cell's headers attribute. This works, but it is tedious to maintain; splitting one complex table into two simpler ones is often clearer for everyone.

<th id="q1" colspan="2" scope="colgroup">Quarter 1</th>
...
<th id="q1-jan" scope="col">Jan</th>
...
<td headers="pune q1 q1-jan">310</td>

Making tables work on small screens

A wide table cannot shrink below the width of its content, so on a phone it pushes the whole page sideways. The most robust fix is to let the table scroll inside its own container while the rest of the page stays put.

<div class="table-wrap" role="region" aria-labelledby="visits-caption" tabindex="0">
  <table>
    <caption id="visits-caption">Monthly website visits by channel, 2026</caption>
    ...
  </table>
</div>

.table-wrap { overflow-x: auto; }
.table-wrap:focus-visible { outline: 2px solid currentColor; }
th, td { padding: 0.5rem 0.75rem; text-align: left; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }

The tabindex="0" lets keyboard users focus the wrapper and scroll it with the arrow keys, and the region role with a label tells screen reader users what the scrollable area contains.

Another pattern stacks each row as a card on narrow screens by changing the table elements to display: block. Use it with care: in some browsers this removes the table semantics, so you may need to add ARIA table roles back. For many data tables, horizontal scrolling or showing fewer columns on small screens is simpler and more reliable. See the media queries guide for switching styles at a breakpoint.

When not to use tables

Before CSS layout matured, developers built whole page layouts from nested tables. Doing so today causes real problems: screen readers announce rows and columns that mean nothing, the source order is hard to rearrange for mobile, and the markup is heavy and difficult to edit.

  • Use CSS grid or flexbox for page layouts and card grids.
  • Use a list for a simple set of items, and a description list (<dl>) for name and value pairs such as product specifications.
  • Use a table when a visitor would naturally compare values across rows and columns: timetables, price comparisons, results, schedules.

HTML emails are the main exception: many email clients still have limited CSS support, so layout tables remain common there. Add role="presentation" to such tables so screen readers ignore their structure.

Building an accessible table step by step

  1. Confirm the data is genuinely two-dimensional.
  2. Write a <caption> that says what the table shows and, where relevant, the period or units.
  3. Put column headers in a <thead> row of <th scope="col"> cells.
  4. Mark the first cell of each body row as <th scope="row"> if it labels the row.
  5. Put totals or summaries in <tfoot>.
  6. Wrap the table in a focusable scrolling container and style it with padding, borders or zebra stripes for readability.
  7. Test with a screen reader's table navigation commands to hear each cell with its headers.

Practical exercise: a class timetable

Build a weekly timetable for a training batch, starting from the page template in our HTML guide.

  1. Use days of the week as column headers and time slots as row headers, with a caption naming the week.
  2. Make one session span two time slots with rowspan, and check every row still has the right number of cells.
  3. Add a footer row showing total hours per day.
  4. Style it with zebra stripes using tbody tr:nth-child(even) and make it scroll horizontally below 600px.
  5. Navigate it with a screen reader, such as NVDA on Windows or VoiceOver on macOS, and confirm each cell is announced with its day and time.

You are done when the timetable reads correctly cell by cell, scrolls without widening the page on a phone, and can be focused and scrolled with the keyboard. Generating tables like this from database rows is part of SkillNewton's PHP foundation course.

Common mistakes

  • Styling the first row in bold with <td> instead of using <th> header cells.
  • Leaving out the <caption>, so users cannot tell what the table contains before exploring it.
  • Using tables to lay out page sections, forms or card grids.
  • Letting a wide table push the whole page sideways on phones instead of scrolling within a container.
  • Mismatched colspan and rowspan values that leave rows with too many or too few cells.

Frequently asked questions

Is tbody required in an HTML table?

You can leave it out and the browser adds a <tbody> to the DOM automatically. Writing it yourself makes the structure clear and gives you a reliable hook for CSS and JavaScript.

Can I hide the caption visually?

Yes, if a visible heading already introduces the table, you can hide the caption visually with a screen-reader-only CSS class. Avoid display: none, which hides it from screen readers as well.

How do I make a sortable table?

Put a <button> inside each sortable <th>, reorder the rows with JavaScript when it is clicked, and set aria-sort="ascending" or "descending" on the active header. The DOM guide covers the element methods you need.

Want to Learn This Properly?

The PHP Web Development: Foundation & Practical Implementation (Online) turns guides like this into practised skills, with projects on real websites.

WhatsApp Call Us Now

Free counselling

Send an Enquiry

Tell us which course you are interested in. We reply by email or phone with the next batch and how to enrol.

Protected by reCAPTCHA. Google Privacy and Terms apply.

We reply by email or phone within working hours. No spam.