Sub topic 10
Making the table work on small screens
This chapter assumes you can write basic HTML and CSS, that you know the semantic table elements (caption, thead, th scope), and that you understand viewport-based responsive layout. The previous chapter built the table. Here you make it usable at 320 to 400 pixels wide without losing the structure that screen readers depend on.
Why comparison tables break on phones
A plan comparison table is wide by nature. It has one column for the feature labels and one for each plan, and each plan column needs room for a price, a button, and short cell text. With four plans, a readable table needs about 36 rem (576 px). A phone viewport is often 360 px.
You have three ways to handle that:
- Let the table scroll sideways inside its own container.
- Reshape the table into stacked cards, one per plan.
- Show only some columns at a time, chosen by the visitor.
Each option trades fidelity to the table against convenience on a small screen. The rest of the chapter covers how each works and where it fails.
Option 1: horizontal scroll inside a wrapper
This is the safest default because the markup stays a real table. The table keeps a minimum width, and a wrapper element scrolls instead of the page.
.table-wrap {
overflow-x: auto;
}
.plans {
border-collapse: separate;
border-spacing: 0;
width: 100%;
min-width: 36rem;
}min-width is what forces the overflow. Without it the browser squeezes the columns until the text wraps one word per line. On wide screens width: 100% still lets the table fill its container.
Keep the labels visible with sticky columns
When a visitor scrolls right to see the Business column, the row labels disappear, and they lose track of what each cell means. A sticky first column fixes this.
.plans tbody th[scope="row"] {
position: sticky;
left: 0;
background: #fff;
text-align: left;
}Three details cause most of the bugs here:
- Sticky cells need an opaque background. Without one, scrolling cells show through the label.
- Use
border-collapse: separate. Withcollapse, borders belong to the table rather than to the cell, so they do not travel with a sticky cell and visually vanish or smear. This is why the table usesseparatewithborder-spacing: 0. - The top-left header cell needs its own rule. It is both a header and the first column, and it must sit above both kinds of sticky cells.
.plans thead th:first-child {
position: sticky;
left: 0;
z-index: 2;
background: #fff;
}
.plans tbody th[scope="row"] {
z-index: 1;
}The sticky header gotcha
The sticky thead th rule from the earlier chapter works only if the page scrolls. Once you set overflow-x: auto on the wrapper, the wrapper becomes the scroll container that sticky positioning measures against. Per the CSS specification, setting one overflow axis to anything other than visible makes the other axis compute to auto as well. The wrapper itself never scrolls vertically, so top: 0 no longer sticks to the viewport. The header scrolls away with the page.
You have two fixes:
- Give the wrapper a bounded height so it scrolls in both directions, for example
max-height: 70vh; overflow: auto;. The header then sticks inside the wrapper. The cost is a nested scroll area, which is awkward on touch devices. - Drop the sticky header on small screens. A pricing table has maybe 10 to 25 rows, so the header often stays near the viewport anyway. Plan names can also be repeated in the cells (see the stacked layout below).
@media (max-width: 40rem) {
.plans thead th {
position: static;
}
}Test this on a real device. Desktop browser emulation rarely reproduces touch scrolling conflicts.
Make the scroll area keyboard accessible
A container that scrolls but has no focusable content cannot be scrolled by keyboard in some browsers. Browsers differ here: recent Chrome and Firefox make scrollable regions focusable by default, but Safari historically did not. Do not rely on that. Add tabindex="0", a role, and a name.
<div class="table-wrap" role="region" aria-labelledby="plans-caption" tabindex="0">
<table class="plans">
<caption id="plans-caption">Compare plans</caption>
<!-- thead and tbody as built in the previous chapter -->
</table>
</div>The role="region" with a name makes screen readers announce what the scrollable area contains. Add a visible focus style, because tabindex="0" produces a focus ring only if you do not remove it:
.table-wrap:focus-visible {
outline: 3px solid #1a56db;
outline-offset: 2px;
}Signal that the table scrolls
Visitors do not always notice hidden columns. Two cheap cues work:
- Size the columns so the next column is partly cut off at the edge. A half-visible column invites a swipe.
- Add an edge shadow that appears only while more content exists. This can be done in CSS with
background-attachment: localgradients, or with a scroll listener that toggles a class.
A short hint like "Swipe to compare plans" works too, but it adds text to maintain and translate.
Option 2: stack the table into cards
For three or four plans, a card per plan often reads better on a phone than a scrolling grid. Each card lists the plan name, price, button, and features in order.
There are two ways to get there, and they behave very differently:
| Approach | How it works | Accessibility | Cost |
|---|---|---|---|
| CSS reflow of the same table | `display: block` on `tr`, `td`, with `data-label` text injected by `::before` | Often loses table semantics in some screen readers. Setting `display` on table elements can remove their roles | Fragile. Tied to the markup |
| Separate card markup | A second `<ul>` or `<section>` list per plan, with the table hidden on small screens | Both views are valid. Hide the inactive one with `display: none` so it is not read twice | Two views to keep in sync |
If you choose separate markup, render both views from the same plan data, as you did for the billing toggle. Two hand-edited copies of the feature list will drift apart, and a price mismatch between views is a trust problem.
Avoid the CSS reflow trick unless you test it with a screen reader. It is popular, but when you override display on tr and td, some browser and screen reader pairs stop announcing the cell's row and column headers. The visitor then hears values with no labels.
Option 3: let the visitor pick columns
A third pattern shows the feature column plus two plans, with controls to choose which two. It keeps real table semantics and fits a phone without sideways scroll. The cost is script. This minimal version shows one selected plan next to the labels:
const select = document.querySelector("#plan-select");
const table = document.querySelector(".plans");
function showPlan(index) {
table.querySelectorAll("tr").forEach((row) => {
Array.from(row.children).forEach((cell, i) => {
// Column 0 holds the feature labels and always stays visible.
cell.hidden = i !== 0 && i !== index;
});
});
}
select.addEventListener("change", () => showPlan(Number(select.value)));
showPlan(Number(select.value));Here the <select> options carry values 1, 2, 3, matching column positions. Use this only on small screens: wrap the call in a matchMedia check, or reveal every cell again above your breakpoint. Without the table on the page for desktop users, the comparison is lost. Also note that the toggle from the billing chapter must keep working here. Because it updates text through data attributes, hidden cells update too.
Choosing between the options
As a rule of thumb, start with the scrolling wrapper because it works with the markup you already have. Move to cards when analytics show visitors on phones do not scroll to later plans (the measurement chapter covers how to check this).
Common errors
- Setting
overflow-x: autoon the table itself.overflowdoes not apply totableelements in the way it does to blocks. Put it on the wrapper. - Transparent sticky cells. Content shows through the label. Give them a background.
- Forgetting
min-width. The table compresses and never scrolls, so text wraps into unreadable slivers. - Hiding the cell text with
display: nonefor the symbol alternatives. Keep thevisually-hiddenpattern from the accessibility examples, becausedisplay: noneremoves the text from assistive technology too. - Touch targets under 44 px. The call-to-action buttons in a narrow column still need comfortable tap size.
Exercise
A table in a div with overflow-x: auto has thead th { position: sticky; top: 0; }, but on a phone the header scrolls away with the page. Why does this happen, and name two fixes.
Show answer
The wrapper with overflow set becomes the scroll container that the sticky header measures against. Because the wrapper itself does not scroll vertically, top: 0 never engages against the viewport. Fixes: give the wrapper a bounded height (such as max-height: 70vh; overflow: auto) so the header sticks inside it, or turn off sticky positioning on the header at small widths with a media query.
Exercise
Why does the comparison table use border-collapse: separate; border-spacing: 0 rather than collapse, and what must sticky cells also have?
Show answer
With collapse, borders belong to the table rather than the cells, so they do not move with sticky cells and appear to vanish or smear while scrolling. Sticky cells also need an opaque background so scrolling content does not show through them.