Sub topic 11
Handling Wide Tables on Small Screens
This chapter assumes you can write a table with table, tr, th, and td. It also assumes you know how scope works and can write basic CSS rules. Here you will keep a wide table usable on a narrow screen without breaking its structure.
Why wide tables cause trouble
A table sizes its columns to fit their content. If a table has eight columns of numbers and dates, its minimum width can be larger than a phone screen. The browser does not wrap the table to fit. Instead, the table pushes past the edge of its container, and the whole page gains a horizontal scrollbar. Readers then have to pan the entire page to see one table.
You have three options:
- Make the table narrower.
- Let only the table scroll sideways.
- Keep the most important column visible while the rest scrolls.
Let the table scroll inside a wrapper
The most reliable fix is to put the table in a container that scrolls on its own. The page stays put, and only the wrapper moves.
<style>
.table-scroll {
overflow-x: auto;
}
</style>
<div class="table-scroll" tabindex="0" role="region" aria-label="Expenses table">
<table>
<tr>
<th scope="col">Item</th>
<th scope="col">Cost</th>
</tr>
<tr>
<td>Rent</td>
<td>900</td>
</tr>
</table>
</div>Each part of the wrapper has a job:
overflow-x: autoadds a horizontal scrollbar only when the table is wider than the wrapper. If the table fits, nothing changes.tabindex="0"puts the wrapper in the keyboard tab order. Without it, a keyboard user may have no way to scroll the region, because some browsers do not focus scrollable containers by default.role="region"andaria-labelgive the wrapper a name. A screen reader announces it as a labeled region, so users know what they have focused.
Use the label to name the table's content, as you would a caption. If the table already has a caption, keep the two consistent.
Do not change the table's display type
It can be tempting to set display: block on the table itself and add overflow-x: auto there. Some browsers then stop treating the element as a table. Screen readers can lose the row and column structure, which undoes the work you did with th and scope. Put the scrolling on a wrapper div and leave the table's display alone.
The same warning applies to turning rows into stacked cards by setting display: block on tr and td. This pattern is common in tutorials. If you use it, test it with a screen reader, and do not assume the table semantics survive.
Keep the row header visible
When a table scrolls sideways, the reader can lose track of which row they are reading. Fix this by making the row header cells stick to the left edge of the scroll container.
.table-scroll th[scope="row"] {
position: sticky;
left: 0;
background: #fff;
}position: sticky with left: 0 holds each row header in place once the scroll reaches it. The background is required. Without it, the cells scrolling underneath show through the header text.
Make the table narrower first
Scrolling is a fallback. Before you add it, check whether the table can simply be smaller:
- Shorten column headings. "Qty" can replace "Quantity ordered".
- Reduce cell padding in a media query for small screens.
- Remove columns that readers rarely need. Put the details on a separate page.
- Split one table into two when it holds two unrelated sets of data.
@media (max-width: 600px) {
th,
td {
padding: 0.25rem 0.4rem;
font-size: 0.9rem;
}
}If you use white-space: nowrap to keep values like dates on one line, remember that it makes columns wider and scrolling more likely.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The whole page scrolls sideways | The table has no scrolling wrapper | Wrap it in a `div` with `overflow-x: auto` |
| Keyboard users cannot scroll the table | The wrapper is not focusable | Add `tabindex="0"` |
| Sticky header text is unreadable | The sticky cell has no background | Set a solid `background` |
| Screen reader stops announcing headers | `display: block` was set on table elements | Remove it and use a wrapper |
Exercise
A table with ten columns overflows a phone screen. You wrap it in a div with overflow-x: auto, but keyboard users report they cannot scroll it. What do you add to the div, and why should you also give it a role and a label?
Show answer
Add tabindex="0" so the div can receive keyboard focus and be scrolled with the arrow keys. Add role="region" and an aria-label so a screen reader announces the focused container by name and the user knows what content they are scrolling.