Full answer
A table with many columns does not fit on a small screen. There are three main approaches.
- Horizontal scroll: put the table in a wrapper with
overflow-x: auto, and give the wrappertabindex="0", arole="region"and a label so keyboard users can scroll it. It keeps the table intact and is the safest choice. Pin the first column withposition: stickyif users need to keep their place. - Hide less important columns on small screens. This hides data, so only use it for information that is not needed.
- 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: blockontable,tbody,trandtd, and hide thetheadvisually. - Copy each column name into a
data-labelattribute on the cells, and show it withtd::before { content: attr(data-label); }. - Setting
display: blockon table parts can remove the table semantics for some browsers and screen readers. Restore them withrole="table",role="row",role="cell"androle="columnheader", and keep the header row available to assistive tools by hiding it with a visually hidden style, notdisplay: none. - Text from
contentis not always read by every screen reader, so test it. - Keep a real
tablein 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.
<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.