Responsive Table with HTML & CSS: 4 Methods

A responsive table is an HTML table that stays readable on a small screen. A plain table doesn’t. It either squeezes its columns until every word wraps onto its own line, or pushes the page wider than the screen.

There are four reliable ways to fix that with CSS, and each one suits a different kind of table:

  1. Scroll wrapper: the table keeps its layout and scrolls sideways inside its own box.
  2. Stacked cards: on small screens, each row becomes a card with its own labels.
  3. Sticky header and first column: the labels stay in view while the data scrolls.
  4. Container query: the table switches layout based on the space it actually has, not the screen width.

I first wrote this post around a single trick, the card layout with data-label. I rewrote it to cover all four, each with a live demo you can resize, the code, and the accessibility catch that comes with it. This blog uses the first method on every table in its posts, through a small WordPress filter I’ll show near the end.

Which Responsive Table Method Should You Use?

Here’s the short version. The rest of the post covers each method in detail.

Method Best for Main catch
Scroll wrapper Data tables with many columns, where people compare values down a column Readers need a hint that there’s more to the side
Stacked cards Short tables people read one row at a time: orders, plans, contacts Hard to compare values across rows, and every cell needs a data-label
Sticky header and first column Long or wide tables, where people lose track of the column or the row Needs solid cell backgrounds, and breaks if a parent has its own overflow
Container query Tables in sidebars, cards and reusable components Needs a wrapper element to act as the container

None of the four needs JavaScript. All the demos below use the same sample data: a Core Web Vitals report with up to nine columns, which is too wide for a phone screen.

Method 1: Scroll the Table Horizontally

The simplest fix is also the most robust. Put the table inside a wrapper with overflow-x: auto. When the table is wider than its container, the wrapper gets a horizontal scrollbar and the rest of the page stays put.

The table itself doesn’t change, so the columns stay aligned and readers can still compare values down a column. That’s why this is the right default for real data tables. Bootstrap’s .table-responsive class is the same idea: a wrapper with overflow-x: auto.

Here’s the HTML:

<div class="table-wrap" role="region" aria-labelledby="cwv-caption" tabindex="0">
  <table>
    <caption id="cwv-caption">Core Web Vitals by page</caption>
    <thead>
      <tr>
        <th scope="col">Page</th>
        <th scope="col">LCP</th>
        <th scope="col">INP</th>
        <th scope="col">CLS</th>
        <th scope="col">Page weight</th>
        <th scope="col">Requests</th>
        <th scope="col">Status</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>/checkout/</td>
        <td>4.6 s</td>
        <td>520 ms</td>
        <td>0.31</td>
        <td>3.4 MB</td>
        <td>92</td>
        <td>Poor</td>
      </tr>
      <!-- more rows -->
    </tbody>
  </table>
</div>

Three attributes on the wrapper matter more than they look:

  • tabindex="0" lets keyboard users focus the box and scroll it with the arrow keys.
  • role="region" makes the box a landmark that screen reader users can jump to.
  • aria-labelledby names that region with the table’s caption, so it isn’t announced as an unnamed region.

And the CSS:

.table-wrap {
  overflow-x: auto;
}

.table-wrap table {
  width: 100%;
  border-collapse: collapse;
}

.table-wrap th,
.table-wrap td {
  padding: 12px 16px;
  text-align: start;
  white-space: nowrap;
}

.table-wrap:focus-visible {
  outline: 2px solid #ffb42f;
  outline-offset: 2px;
}

white-space: nowrap stops the browser from squeezing the columns. Without it, a narrow table rarely overflows at all. It just gets very tall and hard to read.

Add a Scroll Hint

A scrollbar alone is a weak hint. On phones, and on macOS with default settings, it stays hidden until you start scrolling. The demo below uses Lea Verou’s scrolling shadows technique: a soft glow appears on whichever edge still has content behind it.

.table-wrap {
  overflow-x: auto;
  background:
    /* Covers: scroll with the content and hide the glow at each end */
    linear-gradient(to right, #12161a 30%, transparent) left / 48px 100% no-repeat local,
    linear-gradient(to left, #12161a 30%, transparent) right / 48px 100% no-repeat local,
    /* Glows: stay pinned to the edges of the box */
    radial-gradient(farthest-side at 0 50%, rgba(255, 180, 47, 0.3), transparent) left / 16px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, rgba(255, 180, 47, 0.3), transparent) right / 16px 100% no-repeat scroll;
  background-color: #12161a;
}

The trick is background-attachment. The two local layers scroll with the content and cover the glow once you reach an end. The two scroll layers stay pinned to the edges of the box. For it to work, the table cells need transparent backgrounds.

Demo 1: Scroll wrapper with a scroll hint
Frame: -
Core Web Vitals by page (sample data)
Page LCP INP CLS Page weight Requests Status
/ 1.9 s 140 ms 0.04 1.2 MB 38 Good
/pricing/ 2.3 s 180 ms 0.08 980 KB 31 Good
/blog/ 2.8 s 160 ms 0.12 1.6 MB 54 Needs improvement
/blog/css-grid-guide/ 3.4 s 240 ms 0.06 2.1 MB 67 Needs improvement
/contact/ 1.6 s 90 ms 0.01 640 KB 22 Good
/checkout/ 4.6 s 520 ms 0.31 3.4 MB 92 Poor

Pick 375px and scroll the table, or tab into it and use the arrow keys. Watch the glow on the edge that still has content behind it.

If you’d rather keep the scrollbar visible, you can style the scrollbar with CSS to match your design.

Method 2: Stack Table Rows into Cards on Mobile

This is the method the original version of this post covered. Below a breakpoint, the table stops being a grid. Each row becomes a card, each cell becomes a line inside it, and each line prints its column name next to the value.

The column names come from a data-label attribute on every cell:

<table class="table-cards">
  <caption>Core Web Vitals by page</caption>
  <thead>
    <tr>
      <th scope="col">Page</th>
      <th scope="col">LCP</th>
      <th scope="col">INP</th>
      <th scope="col">CLS</th>
      <th scope="col">Page weight</th>
      <th scope="col">Status</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td data-label="Page">/blog/</td>
      <td data-label="LCP">2.8 s</td>
      <td data-label="INP">160 ms</td>
      <td data-label="CLS">0.12</td>
      <td data-label="Page weight">1.6 MB</td>
      <td data-label="Status">Needs improvement</td>
    </tr>
    <!-- more rows -->
  </tbody>
</table>

Then a media query switches the table, its rows and its cells to block layout, and prints each label with a pseudo-element:

@media (max-width: 600px) {
  /* Hide the header row visually, but keep it for screen readers */
  .table-cards thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  .table-cards,
  .table-cards tbody,
  .table-cards tr {
    display: block;
  }

  .table-cards tr {
    margin-bottom: 14px;
    border: 1px solid #2a2f36;
    border-radius: 6px;
  }

  .table-cards td {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    padding: 10px 16px;
    text-align: end;
    border-bottom: 1px dashed #2a2f36;
  }

  .table-cards td:last-child {
    border-bottom: 0;
  }

  /* Print the column name next to each value */
  .table-cards td::before {
    content: attr(data-label);
    font-weight: 600;
    text-align: start;
    text-transform: uppercase;
  }
}

The key line is content: attr(data-label). The attr() function reads an attribute from the element and returns it as text, so one CSS rule gives every cell its own label. If you haven’t worked with them before, here’s more on data attributes in JavaScript and CSS.

Two details are easy to get wrong:

  • Scope it to a class. The old version of this post styled the bare table selector, which turns every table on the page into cards. Use a class such as .table-cards.
  • Don’t hide the header with display: none. That hides it from screen readers too. The clip pattern above hides it visually only.
Demo 2: Stacked cards with data-label
Frame: -
Core Web Vitals by page (sample data)
Page LCP INP CLS Page weight Status
/ 1.9 s 140 ms 0.04 1.2 MB Good
/blog/ 2.8 s 160 ms 0.12 1.6 MB Needs improvement
/blog/css-grid-guide/ 3.4 s 240 ms 0.06 2.1 MB Needs improvement
/checkout/ 4.6 s 520 ms 0.31 3.4 MB Poor

The demo switches to cards at 600px with a container query instead of the media query, so the buttons can preview it on a desktop screen. Method 4 explains the difference.

The trade-off: cards are great for reading one row at a time and poor for comparing values across rows. For a report like the one in the demo, the scroll wrapper usually works better on mobile.

Method 3: Sticky Table Header and First Column

Long tables have a different problem. Scroll down and the column names disappear. Scroll sideways and you lose track of which row you’re on. position: sticky fixes both, without JavaScript.

Give the wrapper a max-height and overflow: auto so it becomes the scroll container. Then stick the header cells to the top and the first cell of each row to the start edge, which is the left side in English:

.table-sticky {
  max-height: 360px;
  overflow: auto;
}

.table-sticky table {
  border-collapse: separate;
  border-spacing: 0;
}

/* Column headers stick to the top */
.table-sticky thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #161b22;
}

/* Row headers stick to the start edge (left in LTR, right in RTL) */
.table-sticky tbody th {
  position: sticky;
  inset-inline-start: 0;
  z-index: 1;
  background: #12161a;
}

/* The corner cell sticks both ways and stays on top */
.table-sticky thead th:first-child {
  inset-inline-start: 0;
  z-index: 3;
}

/* On phones, keep the pinned column narrow */
@media (max-width: 600px) {
  .table-sticky tbody th {
    min-width: 120px;
    max-width: 120px;
    white-space: normal;
    overflow-wrap: anywhere;
  }
}

The first cell of each row is a row header, so the HTML looks like this:

<div class="table-sticky" role="region" aria-labelledby="cwv-caption" tabindex="0">
  <table>
    <caption id="cwv-caption">Core Web Vitals by page</caption>
    <thead>
      <tr>
        <th scope="col">Page</th>
        <th scope="col">LCP</th>
        <!-- more column headers -->
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">/checkout/</th>
        <td>4.6 s</td>
        <!-- more cells -->
      </tr>
    </tbody>
  </table>
</div>

Three things make or break this method:

  • The scroll container decides where cells stick. A sticky element sticks inside its nearest scrolling ancestor. If another element between the table and the wrapper has its own overflow, the cells stick to that one instead, or seem not to stick at all. More on that in fixing position: sticky when it doesn’t work.
  • Sticky cells need a solid background. Otherwise the rows scroll visibly underneath them.
  • Use border-collapse: separate. With collapsed borders, the borders don’t travel with the sticky cells. In my test in Chrome 156, the sticky header lost its borders and the rows showed through above it.
Demo 3: Sticky header and first column
Core Web Vitals by page (sample data)
Page LCP INP CLS TTFB FCP Page weight Requests Status
/ 1.9 s 140 ms 0.04 0.42 s 1.1 s 1.2 MB 38 Good
/pricing/ 2.3 s 180 ms 0.08 0.51 s 1.3 s 980 KB 31 Good
/blog/ 2.8 s 160 ms 0.12 0.63 s 1.6 s 1.6 MB 54 Needs improvement
/blog/css-grid-guide/ 3.4 s 240 ms 0.06 0.71 s 1.9 s 2.1 MB 67 Needs improvement
/contact/ 1.6 s 90 ms 0.01 0.38 s 0.9 s 640 KB 22 Good
/checkout/ 4.6 s 520 ms 0.31 1.20 s 2.8 s 3.4 MB 92 Poor
/about/ 1.8 s 110 ms 0.02 0.40 s 1.0 s 720 KB 26 Good
/services/ 2.4 s 190 ms 0.09 0.55 s 1.4 s 1.1 MB 35 Good
/case-studies/ 3.1 s 210 ms 0.14 0.66 s 1.8 s 2.4 MB 71 Needs improvement
/shop/ 3.9 s 380 ms 0.18 0.92 s 2.2 s 2.9 MB 84 Needs improvement
/cart/ 2.2 s 260 ms 0.05 0.88 s 1.5 s 1.3 MB 40 Needs improvement
/account/ 2.0 s 150 ms 0.03 0.74 s 1.2 s 890 KB 29 Good

Scroll inside the table down and sideways. The column names stay on top, and the page names stay on the left.

One more thing to watch on phones: a pinned column with long values can take half the screen and leave no room for the data. That’s what the media query at the end of the CSS is for. It lets the first column wrap at 120px.

Using <th scope="row"> for the first column also helps screen readers announce which row a value belongs to.

The CSS uses inset-inline-start: 0 instead of left: 0, so the same rules pin the first column on the right in a right-to-left language like Hebrew or Arabic. The other methods use text-align: start and end for the same reason. With left and right, an RTL table pins the wrong column and aligns every cell to the wrong side.

Method 4: Responsive Table with Container Queries

The card layout in Method 2 reacts to the screen. That works until the same table appears in a narrow spot on a wide screen, like a sidebar, a card or a two-column layout. The screen is 1440px wide, the media query never fires, and the table overflows its column.

A container query fixes that by reacting to the width of the table’s wrapper instead:

<div class="table-container">
  <table>
    <!-- same table markup as Method 2, with data-label on every cell -->
  </table>
</div>
/* The wrapper is the container the query measures */
.table-container {
  container-type: inline-size;
}

@container (max-width: 420px) {
  .table-container thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  .table-container table,
  .table-container tbody,
  .table-container tr {
    display: block;
  }

  .table-container td {
    display: flex;
    justify-content: space-between;
    gap: 12px;
  }

  .table-container td::before {
    content: attr(data-label);
    font-weight: 600;
  }

  /* ...plus the card borders and spacing from Method 2 */
}

The container has to be a wrapper element, because an element can’t query its own size. container-type: inline-size measures the width only, which is all a table needs.

Demo 4: Same table, two containers
Main column-
Core Web Vitals by page (sample data)
Page LCP INP Status
/ 1.9 s 140 ms Good
/blog/ 2.8 s 160 ms Needs improvement
/checkout/ 4.6 s 520 ms Poor
Sidebar-
Core Web Vitals by page (sample data)
Page LCP INP Status
/ 1.9 s 140 ms Good
/blog/ 2.8 s 160 ms Needs improvement
/checkout/ 4.6 s 520 ms Poor

Same markup, same CSS, same screen. The sidebar copy is narrower than 420px, so it switches to cards while the main copy stays a table. On a phone both are narrow, so both show cards.

Container queries work in Chrome and Edge 106 and later, Safari 16 and later, and Firefox 110 and later, so they’re safe to use today. For the full picture, see the complete guide to CSS container queries.

Responsive Tables and Accessibility

For years, the card method came with a serious catch. Changing display on table elements made browsers drop the table’s semantics, so screen readers announced a pile of text instead of rows and columns.

That’s mostly history now. Adrian Roselli has tracked this bug since 2018. In his October 2023 update, he reports that tables with CSS display properties now work in Chromium, Gecko and WebKit, with Safari 17 the last to fix it. Chrome had stopped dropping the semantics back in version 80. display: contents is still the exception, so avoid it on table elements.

If you still support older Safari versions, add the ARIA roles back explicitly, the way his article does:

<table role="table">
  <thead role="rowgroup">
    <tr role="row">
      <th role="columnheader" scope="col">Page</th>
      <th role="columnheader" scope="col">LCP</th>
    </tr>
  </thead>
  <tbody role="rowgroup">
    <tr role="row">
      <td role="cell" data-label="Page">/blog/</td>
      <td role="cell" data-label="LCP">2.8 s</td>
    </tr>
  </tbody>
</table>

Whichever method you use, the same checklist applies:

  • Give the table a <caption>, and name its scroll wrapper with aria-labelledby.
  • Use <th scope="col"> for column headers and <th scope="row"> for row headers.
  • Make scroll containers focusable with tabindex="0".
  • Don’t rely on color alone. The status pills in the demos pair every color with a text label.

Responsive Tables in WordPress

If your tables live in WordPress, part of the work may already be done. The block editor‘s Table block wraps every table in a <figure class="wp-block-table">, and the block’s own stylesheet gives that figure overflow-x: auto. So block tables already scroll sideways, as long as your theme doesn’t remove the block library CSS.

Tables in classic editor content, Custom HTML blocks and older posts don’t get that wrapper. A small filter on the_content adds it to every table when the page renders, without touching the stored content:

/**
 * Wrap every table in post content in a scrolling container.
 */
function savvy_wrap_tables( $content ) {
    if ( ! is_singular() || false === stripos( $content, '<table' ) ) {
        return $content;
    }

    $open    = '<div class="table-wrap" role="region" aria-label="Scrollable table" tabindex="0"><table';
    $content = preg_replace( '#<table\b#i', $open, $content );

    return str_ireplace( '</table>', '</table></div>', $content );
}
add_filter( 'the_content', 'savvy_wrap_tables', 20 );

Add it to your child theme’s functions.php, then style .table-wrap with the CSS from Method 1. The filter runs at priority 20, after WordPress has finished formatting the content. If the_content filters are new to you, start with how WordPress hooks work.

This blog runs the same idea on every post, with one extra check so a nested table doesn’t get a second wrapper.

FAQs

Common questions about responsive tables:

How do I make a table responsive with CSS?
Wrap it in a div with overflow-x: auto, so it scrolls sideways when it is wider than the screen. That keeps the columns aligned and works for any table. If people read the table one row at a time, you can instead stack each row into a card below a breakpoint, using display: block and content: attr(data-label).
What does Bootstrap's table-responsive class do?
.table-responsive is a wrapper with overflow-x: auto, the same scroll wrapper as Method 1. The breakpoint versions, such as .table-responsive-md, only scroll below that breakpoint.
Can I make a table responsive without media queries?
Yes. The scroll wrapper needs no media query at all, since the scrollbar only appears when the table is wider than its container. To switch the layout, a container query changes the table into cards based on the width of its wrapper instead of the screen.
How do I keep the table header visible while scrolling?
Put the table in a wrapper with a max-height and overflow: auto, then give the header cells position: sticky, top: 0 and a solid background. Use border-collapse: separate, because collapsed borders do not move with sticky cells.
Does changing display on a table hurt accessibility?
Not in current browsers. Chromium, Firefox and Safari 17 and later keep table semantics when you change display on table elements. display: contents is the exception. For older Safari versions, add ARIA roles such as role="row" and role="cell" to the markup.
Should I use CSS Grid instead of a table?
Not for tabular data. A <table> tells browsers and screen readers how rows and columns relate, and a grid of div elements does not. Grid is the right tool when the content is a layout, like a set of cards. For that, see responsive layouts with CSS Grid and no media queries.
How do I make tables responsive in WordPress?
Tables from the block editor's Table block already sit inside a figure with overflow-x: auto. For classic content and older posts, add a the_content filter that wraps every <table> in a scrolling div, as shown in the WordPress section above.

Summary

A responsive table doesn’t have to be clever. For most data tables, a scroll wrapper with a visible hint is the right answer, and the core of it is one line of CSS. Switch to cards when people read one row at a time, add sticky headers when the table gets long, and use a container query when the same table shows up in containers of different widths.

Whichever you pick, keep the real <table> markup, add a caption and header scopes, and try it with the keyboard. If you build something with these, I’d like to see it in the comments.

Discussion
[  0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!