← Tous les articles

Responsive HTML tables on mobile: 3 CSS solutions depending on the content (and the accessibility traps)

A table spilling off a phone screen is one of the most common layout bugs. Controlled horizontal scrolling, turning rows into cards, or priority columns: how to pick the right approach and build each one properly.

Responsive HTML tables on mobile: 3 CSS solutions depending on the content (and the accessibility traps)

Key points

  • There’s no single right answer: it depends on whether users compare rows or read one row at a time.
  • Horizontal scrolling suits comparison tables, as long as it’s visually signposted and usable with a keyboard.
  • Card layouts are ideal for lists of items, but display: block can strip table semantics, so you need to put them back.

On the sites we audit, a table overflowing on mobile is almost always on the list of problems: pricing grids, opening hours, technical specs, product comparisons. The symptom is familiar. Either the whole page starts scrolling sideways, or the table gets squeezed until every word wraps onto its own line. Ready-made fixes found online usually apply the same recipe to every table, but the right choice depends on how the table is used. Here are the three approaches we rely on, how to build each one cleanly, and how to decide which one fits a given table.

Start with how people use the table

Before writing any CSS, ask one question: do visitors compare rows with each other, or look at one row at a time?

  • A comparison table (pricing plans, product features) is read by crossing rows and columns. Turning it into cards breaks the comparison: users have to scroll and remember values from card to card. Keeping the grid and letting it scroll is the better option here.
  • A list of items laid out as a table (orders, registrations, branch opening hours, downloadable documents) is read row by row.
  • A wide table where some columns are secondary (internal IDs, last-modified dates) can simply drop those columns on small screens, with the details available elsewhere.

Solution 1: controlled horizontal scrolling

The simplest fix is to wrap the table in a container that scrolls horizontally. It still has to be done properly, because the bare-minimum version (an overflow-x: auto and nothing else) has two flaws: nothing tells users there are hidden columns, and keyboard users can’t scroll the container. Here’s the full markup:

<div class="table-scroll" role="region" aria-labelledby="plans-title" tabindex="0">
  <table>
    <caption id="plans-title">Plan comparison</caption>
    <thead>
      <tr><th scope="col">Plan</th><th scope="col">Users</th><th scope="col">Storage</th><th scope="col">Support</th><th scope="col">Monthly price</th></tr>
    </thead>
    <tbody>
      <tr><th scope="row">Starter</th><td>3</td><td>10 GB</td><td>Email</td><td>…</td></tr>
      <!-- … -->
    </tbody>
  </table>
</div>

tabindex="0" makes the container reachable by keyboard, so the arrow keys can scroll it. The region role, tied to the table’s caption, lets screen readers announce the area by name rather than as an anonymous focusable element. The CSS:

.table-scroll {
  overflow-x: auto;
  max-width: 100%;
  /* Shadows that hint at more columns to the right or left */
  background:
    linear-gradient(to right, #fff 30%, rgb(255 255 255 / 0)) left / 40px 100% no-repeat local,
    linear-gradient(to left, #fff 30%, rgb(255 255 255 / 0)) right / 40px 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, rgb(0 0 0 / .2), transparent) left / 14px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, rgb(0 0 0 / .2), transparent) right / 14px 100% no-repeat scroll;
}
.table-scroll:focus-visible {
  outline: 2px solid #1d4ed8;
  outline-offset: 2px;
}
.table-scroll table {
  border-collapse: collapse;
  min-width: 40rem; /* below this, scroll instead of squashing */
}
.table-scroll th,
.table-scroll td {
  padding: .6rem .8rem;
  text-align: left;
  white-space: nowrap;
}
/* Keep the first column visible while scrolling */
.table-scroll th[scope="row"] {
  position: sticky;
  left: 0;
  z-index: 1;
  background: #fff;
}

The four background gradients are an old trick that still works well: two shadows are fixed to the container’s edges, and two white “covers” scroll with the content. At the start, the left cover hides the left shadow; as soon as you scroll, the shadow appears. Users see there’s more content, with no JavaScript. If your background isn’t white, swap #fff for your background colour.

The table’s minimum width matters. Without it, the browser squashes the columns before it scrolls, which gives you the worst of both worlds. The sticky first column (position: sticky) keeps each row’s label in view while you scroll through the values; give it an opaque background, or the cells sliding underneath will show through.

Solution 2: turn each row into a card

For lists of items, we turn each row into a card below a certain width. Each cell then shows its column label in front of its value. The CSS relies on display: block and a data-label attribute on every cell:

@media (max-width: 40em) {
  /* Header stays available to screen readers but is no longer displayed */
  .table-cards thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
  .table-cards,
  .table-cards tbody,
  .table-cards tr,
  .table-cards td,
  .table-cards tbody th {
    display: block;
  }
  .table-cards tr {
    border: 1px solid #d4d4d8;
    border-radius: .5rem;
    padding: .5rem 1rem;
    margin-block-end: 1rem;
  }
  .table-cards td {
    display: grid;
    grid-template-columns: minmax(7rem, 40%) 1fr;
    gap: .75rem;
    padding: .35rem 0;
  }
  .table-cards td::before {
    content: attr(data-label);
    font-weight: 600;
  }
}

Accessibility specialists know the catch, but tutorials rarely mention it: when you change the display value of table elements, some browsers drop their semantics. The screen reader no longer announces a table, just a run of text, and ::before content isn’t read reliably everywhere. The fix is to restore the ARIA roles explicitly. Since the data-label attributes need filling in anyway, a small script handles both, which also saves writing labels by hand:

document.querySelectorAll('table.table-cards').forEach((table) => {
  const headers = [...table.querySelectorAll('thead th')].map((th) => th.textContent.trim());

  // Restore the table semantics that display: block can remove
  table.setAttribute('role', 'table');
  table.querySelectorAll('thead, tbody').forEach((g) => g.setAttribute('role', 'rowgroup'));

  table.querySelectorAll('tr').forEach((tr) => {
    tr.setAttribute('role', 'row');
    [...tr.children].forEach((cell, i) => {
      if (cell.closest('thead')) {
        cell.setAttribute('role', 'columnheader');
        return;
      }
      cell.setAttribute('role', cell.tagName === 'TH' ? 'rowheader' : 'cell');
      // Label taken from the header, unless already set in the HTML
      if (!cell.dataset.label && headers[i]) cell.dataset.label = headers[i];
    });
  });
});

This script assumes a simple table: one header row and no merged cells. With colspan, a cell’s index no longer lines up with its header, and you’ll need to set data-label in the HTML. If the table is generated server-side (PHP, a template), output the attributes directly: same result, no JavaScript dependency.

Solution 3: hide secondary columns

When a table has lots of columns but two or three carry what matters, the most readable option is to show only those on small screens. Add a class to the relevant cells, header included:

@media (max-width: 40em) {
  .table-priority .secondary { display: none; }
}

This is only acceptable if the hidden information remains reachable another way: a link to each row’s detail page, a button to show the full table, or a downloadable file. Hiding useful data with no alternative means mobile visitors miss part of the content, which is both an accessibility and an SEO problem.

The WordPress case: tables entered by the client

On a WordPress site, tables are usually created by the client’s team with the editor’s Table block, and nobody is going to add data-label attributes or a wrapper by hand. That’s where the solutions above need to become automatic.

The Table block already wraps the table in a figure with the wp-block-table class; depending on the theme, that wrapper may or may not scroll horizontally. We always add the solution 1 rules for .wp-block-table to the theme’s stylesheet, so every table scrolls by default. For tables that should become cards, the client’s team just adds the table-cards class in the block’s “Additional CSS class(es)” field, or picks a block style we register with register_block_style(), which is friendlier for non-developers. The solution 2 script takes care of the rest.

One often-missed detail: turn on the Table block’s “Header section” option. Without it, the first row is an ordinary row, there’s no thead, and neither screen readers nor our script know what the column labels are.

FAQ

Why does my table make the whole page scroll on mobile?

Because its minimum width is wider than the screen and no container limits the overflow. Wrap it in an element with overflow-x: auto and max-width: 100%, and check that no parent has a fixed width or a flex item that won’t shrink.

Do card-style tables hurt SEO?

No. The HTML is still a table; only the display changes. Search engines read the same content either way.

Tables, pricing grids or whole pages breaking on mobile? We fix responsive layout and accessibility issues, on WordPress and on custom sites and applications.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

Agence web : sites WordPress sur mesure, thèmes premium et logiciels SaaS.

Parler de votre projet →