Table Responsive Examples
Responsive table layouts that adapt to narrow viewports using container queries and card-based layouts.
Responsive Card Layout
When the table container is narrower than 500px, each row transforms into a card with labeled fields. Uses data-responsive="card" on the table; each cell's label comes from data-label or, when that is missing, from its column header.
Resize your browser window to see the table transform into cards at narrow widths.
Card Layout Preview
This container is constrained to 400px width to demonstrate the card layout without resizing.
Container width: 400px
Accessories in stock (labels derived from the headers)
| Product |
Price |
Stock |
| Wireless Keyboard |
$79.99 |
42 units |
| USB-C Hub |
$49.99 |
128 units |
| Monitor Stand |
$119.99 |
15 units |
| Webcam HD |
$89.99 |
67 units |
How It Works
HTML Structure
Add data-responsive="card" to the table. Card labels come from the column headers; add data-label to a <td> only to override one:
<data-table>
<table data-responsive="card">
<caption>Team directory</caption>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alice Chen</td>
<td data-label="Contact">[email protected]</td>
</tr>
</tbody>
</table>
</data-table>
Container Query Behavior
The data-table element uses container-type: inline-size to enable container queries (VB sections and articles do too, so a plain table works the same way). When the container is narrower than 500px:
- The header row is hidden visually but stays in the accessibility tree
- Each body and footer row becomes a bordered card
- Each cell displays as a grid with its label at the inline start
- A small script restores explicit table roles (changing the display of rows and cells drops them in some browsers) and fills missing
data-label values from the column headers
Benefits
- Progressive Enhancement
- The card layout is pure CSS and works without JavaScript; the script only adds roles and missing labels.
- Container-Based
- Responds to the container width, not viewport. Works in sidebars, modals, and constrained layouts.
- Accessible
- Column headers stay available to screen readers and the table, row and cell roles are restored explicitly, so table navigation still works in card mode. Without JavaScript, browsers that drop table roles on re-displayed rows (notably Safari) will read the cards as plain text.