← Back to HTML & CSS, page 8

html-css-0145 · Hard senior

How do you make a wide data table readable on a phone, and what must you not break?

Wrap the table in a labelled scroll area, hide low-value columns, or turn rows into cards with display: block and data-label text. When changing display on table parts, restore table roles and keep the header available to assistive tools.

Full answer

A table with many columns does not fit on a small screen. There are three main approaches.

  1. Horizontal scroll: put the table in a wrapper with overflow-x: auto, and give the wrapper tabindex="0", a role="region" and a label so keyboard users can scroll it. It keeps the table intact and is the safest choice. Pin the first column with position: sticky if users need to keep their place.
  2. Hide less important columns on small screens. This hides data, so only use it for information that is not needed.
  3. Stack each row as a card. Each cell shows its column name before its value. This is easy to read, but it takes more work to keep accessible.

For the card version:

  • In a media query, set display: block on table, tbody, tr and td, and hide the thead visually.
  • Copy each column name into a data-label attribute on the cells, and show it with td::before { content: attr(data-label); }.
  • Setting display: block on table parts can remove the table semantics for some browsers and screen readers. Restore them with role="table", role="row", role="cell" and role="columnheader", and keep the header row available to assistive tools by hiding it with a visually hidden style, not display: none.
  • Text from content is not always read by every screen reader, so test it.
  • Keep a real table in the HTML. Do not change the HTML for small screens.

Choose scrolling when the data must be compared across columns. Choose cards when each row is a record, such as an order or a person.

On a wide screen it is a normal table. Under 500px each row becomes a small card with "Course" and "Price" before the values, and the header is moved off screen but still available to assistive tools.

Test with a screen reader in both layouts, because changing display values can change what is announced.

html
<style>
  table { border-collapse: collapse; width: 100%; font: 14px sans-serif; }
  th, td { padding: 6px; border: 1px solid #ccc; text-align: left; }
  @media (max-width: 500px) {
    thead { position: absolute; left: -9999px; }
    table, tbody, tr, td { display: block; }
    tr { margin-bottom: 8px; border: 1px solid #999; }
    td { border: 0; display: flex; gap: 8px; }
    td::before { content: attr(data-label); font-weight: bold; min-width: 70px; }
  }
</style>
<table id="t" role="table">
  <thead role="rowgroup"><tr role="row"><th role="columnheader">Course</th><th role="columnheader">Price</th></tr></thead>
  <tbody role="rowgroup">
    <tr role="row"><td role="cell" data-label="Course" id="c1">HTML</td><td role="cell" data-label="Price" id="p1">10</td></tr>
  </tbody>
</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

  • Horizontal scroll keeps the table intact.
  • Cards use display: block and data-label with attr().
  • display changes can remove table semantics.
  • Restore roles and keep the header available.
  • Choose by whether users compare columns.

Common mistakes

  • Hiding the thead with display: none, so screen readers lose the column names.
  • Changing display on table elements and not restoring the roles.
  • Using cards when users need to compare columns.

Follow-ups

  • When is horizontal scroll better than cards?
  • Why use a visually hidden header instead of display: none?

Tips

  • List the three options and when each fits.
  • Warn about losing table semantics when changing display.