Sub topic 10
Making the table work on small screens
This chapter assumes you know basic HTML and CSS, and that you have built a semantic comparison table with a <caption>, column headers, and row headers. Here you make that table usable on a phone.
Why comparison tables break on narrow screens
A plan table has one column per plan and one column of feature labels. With four columns, each cell needs roughly 8 to 10 rem to stay readable. On a 360 px phone, that does not fit. The browser then squeezes columns until text wraps on every word, or the page scrolls sideways as a whole.
You have three common options:
| Approach | How it works | Trade-off |
|---|---|---|
| Horizontal scroll | The table keeps its shape and scrolls inside a wrapper. | Keeps true table semantics. Users must discover the scroll. |
| Stacked cards | Each plan becomes its own block of features. | Easy to read. Hard to compare plans side by side. |
| Plan picker | The user chooses two plans to compare. | Needs JavaScript and more design work. |
This chapter builds the first option, because it keeps the real table and costs the least code.
Scroll inside a wrapper
Put the table in a container that scrolls on its own. Then only the table moves, and the rest of the page stays still.
.table-wrap {
overflow-x: auto;
}
.plans {
border-collapse: separate;
border-spacing: 0;
width: 100%;
min-width: 36rem;
}min-width: 36rem stops the columns from shrinking below a readable size. When the screen is narrower than that, the wrapper scrolls. On wide screens, width: 100% lets the table fill its space.
border-collapse: separate matters here. Sticky cells do not keep their borders in the collapsed model, so borders appear to detach as you scroll.
Keep the labels in view
If feature labels scroll away, the user sees a column of values with no meaning. Make the row headers sticky on the left edge.
.plans thead th {
position: sticky;
top: 0;
background: #fff;
}
.plans tbody th[scope="row"] {
position: sticky;
left: 0;
background: #fff;
text-align: left;
}
.plans th,
.plans td {
padding: 0.75rem 1rem;
border-bottom: 1px solid #ddd;
}Each sticky cell needs an opaque background. Without one, scrolling cells show through it and the text overlaps.
Common errors
- Sticky header does not stick. A sticky element sticks relative to its nearest scrolling ancestor. Because
.table-wraphasoverflow-x: auto, it also becomes the vertical scroll container, sotop: 0refers to the wrapper and not the page. The header sticks only if the wrapper itself has a bounded height. Test it, and drop the sticky header if the table is short. - Hidden scroll. On some devices, scrollbars appear only while scrolling. Users may not know more plans exist. Let the last column peek past the edge, or add a short hint such as "Scroll to see all plans."
- Keyboard users. A scrollable region can be unreachable by keyboard in some browsers. Give the wrapper
tabindex="0",role="region", and anaria-labelledbythat points to the caption or a nearby heading. - Removing table semantics. Setting
display: blockon table elements to build cards can make screen readers stop announcing a table. Prefer scrolling unless you add ARIA roles back.
Check your work
- Open the page at 360 px width.
- Confirm the table scrolls inside its wrapper and the page itself does not scroll sideways.
- Scroll right and confirm feature labels stay visible.
- Tab to the wrapper and scroll with the arrow keys.
Exercise
A teammate sets border-collapse: collapse on the plan table, and the borders under sticky row headers disappear while scrolling. What do you change, and why?
Show answer
Set border-collapse: separate with border-spacing: 0. In the collapsed model, borders belong to the table grid and do not travel with a sticky cell. In the separate model, each cell owns its borders, so they move with it.
Exercise
Why does a sticky cell need a solid background color?
Show answer
Cells that scroll underneath are drawn behind the sticky cell. A transparent background lets their text show through and overlap the label.