Sub topic 11
Handling Wide Tables on Small Screens
This chapter assumes you can write a complete table with table, tr, th, and td. It also assumes you know scope, caption, and basic CSS selectors and media queries. Here you will learn why wide tables break small layouts, how to contain them, and when to change the layout instead.
Why wide tables overflow
A table sizes its columns from the content in them. The browser tries to shrink columns by wrapping text. A column cannot get narrower than its longest unbreakable piece, such as a long word, a number, an email address, or a cell with an image. If the sum of those minimum widths is larger than the viewport, the table is wider than the page.
The result depends on the parent. Usually the table pushes past the container and the whole page gains a horizontal scrollbar. On a phone, the browser may zoom the page out, which makes all text tiny. Both outcomes are bad. The goal is to decide where the extra width goes.
There are four common strategies:
- Let the table scroll sideways inside its own container.
- Keep the table, but pin the most important header cells in place while the rest scrolls.
- Reflow each row into a stacked block on narrow screens.
- Show fewer columns, or shorten the content.
Strategy 1: a scroll wrapper
The simplest fix is to put the table inside a block element that scrolls on the horizontal axis. The page stays the width of the viewport and only the wrapper scrolls.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Expenses</title>
<style>
.table-scroll {
overflow-x: auto;
}
table {
border-collapse: collapse;
}
th,
td {
border: 1px solid #ccc;
padding: 0.5rem 0.75rem;
white-space: nowrap;
}
</style>
</head>
<body>
<div class="table-scroll" tabindex="0" role="region" aria-label="Expenses table">
<table>
<caption>Monthly expenses</caption>
<tr>
<th scope="col">Item</th>
<th scope="col">Cost</th>
<th scope="col">Paid on</th>
<th scope="col">Payment method</th>
<th scope="col">Notes</th>
</tr>
<tr>
<th scope="row">Rent</th>
<td>900</td>
<td>1 May</td>
<td>Bank transfer</td>
<td>Due on the first of every month</td>
</tr>
</table>
</div>
</body>
</html>Three details in this example matter.
- The viewport meta tag. Without
width=device-width, a phone renders the page at a desktop width (often 980 px) and scales it down. Your media queries and your wrapper then never see a narrow screen. Every responsive technique in this chapter depends on this tag. overflow-x: auto. The scrollbar appears only when the content is wider than the wrapper. Do not useoverflow-x: scroll, which always shows a scrollbar track.white-space: nowrap. This is optional. It keeps each cell on one line, so the table keeps a predictable width and scrolls instead of squeezing text into tall, narrow columns. Use it only for short values. Applied to long text, it makes the table far wider than it needs to be.
Making the wrapper accessible
A scrolling region is a problem for keyboard users. A mouse or touch user can scroll it directly. A keyboard user can scroll only if the region can take focus. The attributes on the wrapper solve that:
tabindex="0"puts the wrapper in the tab order, so arrow keys scroll it once it has focus.role="region"witharia-labelgives the wrapper a name. Screen readers announce it as a landmark, and the label tells the user what is inside.
Give the focused wrapper a visible outline so sighted keyboard users can see where they are:
.table-scroll:focus-visible {
outline: 3px solid #1a5fb4;
outline-offset: 2px;
}The aria-label should name the table. If the table has a caption, keep the two consistent, for example "Expenses table" for the region and "Monthly expenses" for the caption.
Some current browsers already make scrollable regions focusable by default. Do not rely on that. The tabindex attribute is harmless there and necessary elsewhere.
Hint that more content exists
Overlay scrollbars on phones disappear until the user touches the screen. A reader may not realize that columns are hidden. Two small steps help:
- Let a column sit partly cut off at the right edge, so the clipping suggests more content.
- Add a short note above the table, such as "Scroll sideways to see all columns."
Strategy 2: sticky header cells
When a table scrolls sideways, the row labels in the first column scroll out of view. The reader then sees numbers without knowing what they describe. position: sticky keeps those header cells pinned.
.table-scroll {
overflow-x: auto;
}
.table-scroll tbody th[scope="row"] {
position: sticky;
left: 0;
background: #fff;
border-right: 2px solid #999;
}Notes on this rule:
- The sticky cell sticks relative to its nearest scrolling ancestor, which here is
.table-scroll. That is why the wrapper must be the element withoverflow-x. If an unrelated ancestor between the cell and the wrapper also setsoverflow, the cell will stick to that ancestor instead. - The
backgroundis required. A sticky cell is transparent by default, so the cells scrolling underneath it show through and the text overlaps. - With
border-collapse: collapse, borders belong to the table and do not travel with a sticky cell. The pinned cell can look as though it has lost its border while scrolling. A thickbox-shadowon the cell, orborder-collapse: separatewithborder-spacing: 0, avoids that. - Pin only what you need. If the sticky column takes 60% of a phone screen, little room is left for the data.
You can pin the header row the same way, using position: sticky; top: 0 on thead th. That only works when the wrapper also has a limited height and overflow-y: auto. In the horizontal-only wrapper above, the page itself scrolls vertically, so a sticky top would be relative to the wrong container.
Strategy 3: reflowing rows into stacked blocks
For tables where each row is a self-contained record, such as a list of orders, you can turn each row into a card on narrow screens. Each cell shows its column name next to its value.
The pattern needs one extra attribute in the HTML. Every td carries a data-label with the text of its column header:
<table class="reflow">
<caption>Orders</caption>
<thead>
<tr>
<th scope="col">Order</th>
<th scope="col">Customer</th>
<th scope="col">Total</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row" data-label="Order">1042</th>
<td data-label="Customer">Ana Silva</td>
<td data-label="Total">48.00</td>
</tr>
</tbody>
</table>The CSS shows the labels only on narrow screens:
@media (max-width: 40rem) {
.reflow,
.reflow tbody,
.reflow tr,
.reflow th,
.reflow td {
display: block;
}
.reflow thead {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
}
.reflow tr {
margin-bottom: 1rem;
border: 1px solid #ccc;
}
.reflow th[data-label],
.reflow td[data-label] {
display: flex;
justify-content: space-between;
gap: 1rem;
padding: 0.5rem 0.75rem;
}
.reflow [data-label]::before {
content: attr(data-label);
font-weight: bold;
}
}The header row is hidden visually but stays in the document, so its text is still available to assistive technology. Do not use display: none for it, because that removes it from the accessibility tree.
The cost of reflowing
Setting display: block on table elements replaces their table layout. In some browsers, this also removes the table semantics that screen readers use. The user may no longer hear "row 2, column 3" or the associated header when moving between cells. Support has improved, but it is not uniform across browser and screen reader pairs.
If you use this technique:
- Test with at least one real screen reader on the browsers you support.
- Restore the roles explicitly where needed:
role="table"on the table,role="rowgroup"ontheadandtbody,role="row"ontr,role="columnheader"on columnthcells,role="rowheader"on rowthcells, androle="cell"ontd. - Keep the
data-labeltext in sync with the real headers. Because it is duplicated, it is easy to let it drift when columns change. - Avoid it for tables where the point is comparison across rows. Once rows are cards, the reader cannot scan down a column.
Strategy 4: show less
Often the best fix is to ask why the table is wide. Options include:
- Merge related columns, for example "City, Country" in one cell.
- Hide low-priority columns on narrow screens by adding a class and using a media query. Hide them with
display: none, which also removes them from the accessibility tree. Make sure no information is lost for people who need it, for instance by linking to a detail page. - Shorten headers or use abbreviations with the
abbrelement. - Let long text wrap, using
overflow-wrap: anywhereon cells that hold URLs or identifiers. - Reduce
paddingandfont-sizeslightly on small screens.
Hiding columns changes the table's grid. If any cell uses colspan or rowspan, hiding columns will break the alignment. Check those tables carefully.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| The whole page scrolls sideways | No wrapper, or the wrapper has no `overflow-x` | Wrap the table and set `overflow-x: auto` |
| Media queries never trigger on a phone | Missing viewport meta tag | Add `width=device-width, initial-scale=1` |
| Sticky column text overlaps scrolled cells | Sticky cell has no background | Set an opaque `background` |
| Sticky column does not stick | Another ancestor has `overflow` set | Put `overflow-x: auto` on the closest wrapper, and remove it from other ancestors |
| Keyboard users cannot scroll the table | Wrapper is not focusable | Add `tabindex="0"`, a role, and an accessible name |
| Reflowed table is read as plain text | `display: block` removed table semantics | Add explicit ARIA roles and test with a screen reader |
| Fixed `width` on the table causes overflow | `width: 800px` or similar on `table` | Use `max-width: 100%` or `width: 100%` where possible |
One related trap is setting table-layout: fixed with width: 100%. This forces equal-width columns that never grow past the container, so long content spills out of cells or is clipped. It is useful only when you control the content length.
Choosing a strategy
Start with the scroll wrapper. It keeps the real table intact, keeps accessibility simple, and preserves comparison across rows. Add sticky header cells when the row labels matter. Reflow only for record-like data, and only when you can test the semantics afterwards. Remove columns whenever the design allows it, since no technique helps as much as less content.
Exercise
A table has eight columns of short numbers. Readers need to compare values down each column. A developer sets display: block on table, tr, and td below 40rem so each row becomes a card. Explain two problems with this choice and name a better approach.
Show answer
First, stacking rows into cards removes the column alignment, so readers can no longer scan down a column to compare values. Second, display: block can strip the table semantics in some browsers, so screen reader users may lose row and column announcements unless ARIA roles are restored and tested. A better approach is a wrapper with overflow-x: auto, tabindex="0", role="region", and an aria-label, plus a sticky row-header column so the labels stay visible.
Exercise
You add position: sticky; left: 0 to the row header cells, but while scrolling, the numbers from other columns show through the pinned cells. What is missing, and what else could stop the pinning from working at all?
Show answer
The sticky cells need an opaque background, because they are transparent by default. Pinning can fail if a different ancestor between the cell and the intended wrapper sets overflow, because the cell then sticks relative to that ancestor instead of the scrolling wrapper.
Exercise
Why must a scroll wrapper have tabindex="0", and what should you add alongside it?
Show answer
Keyboard users can scroll a region only if it can take focus, and tabindex="0" puts it in the tab order. Add role="region" with an aria-label that names the table, and a visible focus style such as an outline on :focus-visible.