“CSS: Turn a Wide Table into Cards on Smartphones”
Wide tables with many columns, such as order histories or price lists, overflow on smartphone screens and make the whole page scroll sideways. To make a table responsive, switch tr and td to display: block only when the screen is narrow, so each row becomes a card stacked vertically. Before each cell, output the column name from the data-label HTML attribute using ::before.
I tested with Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I built an order table with 6 columns and 3 rows, set the viewport width to 800px and 360px, and checked element sizes and the accessibility tree (the structure of the page that is passed to assistive technologies such as screen readers) that I extracted with Playwright. I did not test Firefox, Safari, real smartphones, or actual screen reader output.
- 1. Without any changes, the table overflows the page
- 2. Turn each row into a card only when the screen is narrow
- 3. The caption must also be set to block, or the table name is stacked one character per line
- 4. Hide the header row without display: none
- 5. Add / "" to content to keep column names out of the cell name
Without any changes, the table overflows the page
The test uses a table like the following. A caption names the table, and the header cells have scope="col".
<table class="orders">
<caption>注文の一覧</caption>
<thead>
<tr>
<th scope="col">注文番号</th>
<th scope="col">注文日</th>
<th scope="col">商品</th>
<th scope="col">数量</th>
<th scope="col">金額</th>
<th scope="col">状態</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="注文番号">A-1001</td>
<td data-label="注文日">2026-11-01</td>
<td data-label="商品">ステンレスの保温マグ(350ml)</td>
<td data-label="数量">2</td>
<td data-label="金額">4,180円</td>
<td data-label="状態">発送済み</td>
</tr>
<!-- The other two rows follow the same format -->
</tbody>
</table>
On the test page, the following CSS is applied to the table. white-space: nowrap prevents the cell text from wrapping.
.orders { border-collapse: collapse; }
.orders th, .orders td { padding: 8px 12px; border-bottom: 1px solid #cfd8dc; text-align: left; white-space: nowrap; }
.orders thead th { background: #eefafd; }
At a 360px viewport width, the table stays 680.2px wide, and the page's width (scrollWidth) is 696px. The Quantity, Amount, and Status columns on the right are only visible if you scroll sideways.

Turn each row into a card only when the screen is narrow
The following CSS changes the table into cards on screens 600px wide or narrower.
@media (max-width: 600px) {
.orders thead {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
.orders,
.orders caption,
.orders tbody,
.orders tr,
.orders td {
display: block;
}
.orders caption {
margin-bottom: 8px;
font-weight: bold;
text-align: left;
}
.orders tr {
margin-bottom: 12px;
padding: 4px 12px;
border: 1px solid #cfd8dc;
border-radius: 8px;
}
.orders td {
display: grid;
grid-template-columns: 6em 1fr;
gap: 8px;
padding: 6px 0;
white-space: normal;
}
.orders tr td:last-child {
border-bottom: 0;
}
.orders td::before {
content: attr(data-label);
font-weight: bold;
}
}
When table, tbody, tr, and td are set to display: block, the table's row and column layout goes away, and the elements are stacked from top to bottom like normal blocks. Giving tr a border and rounded corners makes each row look like a single card.
td is also set to display: grid, which places the column name in a 6em-wide column on the left and the value on the right. The lines that the original CSS draws under each cell become dividers inside the card. The last cell in each card gets border-bottom: 0, so that the divider does not appear right above the card's own border, which would look like a double line.
The column names are output with content: attr(data-label) in td::before. As described on the MDN page for attr(), attr() without a type returns the attribute value as a plain string. In the test environment, the column name from ::before landed in the left grid column, and the cell text landed in the right column (see the image below).
The header row (thead) is hidden from view, but not with display: none. The reason is explained in a later section.
At a 360px viewport width, the page is 360px wide and no longer scrolls sideways. The first card is 328×255px. Long values such as product names wrap inside the right column because of white-space: normal. At an 800px viewport width, the media query does not match, so the original table was kept (680.2px wide).

The caption must also be set to block, or the table name is stacked one character per line
The CSS above includes caption among the elements set to display: block because the caption breaks without it. I removed only the caption rule and displayed the page at 360px. The caption kept display: table-caption, and it became a 16px-wide, 120px-tall box. The five characters of "Order list" were stacked one character per line.

With caption also set to display: block, the caption becomes 328px wide and 24px tall and appears in one line above the cards. If your table has no caption, you can omit this rule.
Hide the header row without display: none
The cards show column names with ::before, so the header row is not needed on screen. However, if you remove it with display: none, the header cells disappear from the accessibility tree. I compared the accessibility trees at a 360px viewport width in the test environment.
With the CSS above (shrinking thead to 1px and clipping it), the table, the rows, and the header cells remained after display: block was applied. The table kept the table role, the rows kept row, and the header cells kept columnheader.
- table "注文の一覧":
- caption: 注文の一覧
- rowgroup:
- row "注文番号 注文日 商品 数量 金額 状態":
- columnheader "注文番号"
- columnheader "注文日"
...
- rowgroup:
- row "注文番号 A-1001 注文日 2026-11-01 商品 ステンレスの保温マグ(350ml) 数量 2 金額 4,180円 状態 発送済み":
- cell "注文番号 A-1001"
- cell "注文日 2026-11-01"
...
When only thead was set to display: none, the header row disappeared entirely, and the data rows came directly under the rowgroup. The table, row, and cell roles remained, but there were no columnheader elements at all.
- table "注文の一覧":
- caption: 注文の一覧
- rowgroup:
- row "注文番号 A-1001 注文日 2026-11-01 商品 ステンレスの保温マグ(350ml) 数量 2 金額 4,180円 状態 発送済み":
- cell "注文番号 A-1001"
...
Add / "" to content to keep column names out of the cell name
The tree above shows that the cell name in the accessibility tree (the name passed to screen readers) is the ::before column name joined with the value, such as the Japanese label for the order number followed by "A-1001". Because the header cells (columnheader) remain, the column names appear twice in the tree.
In content, you can add alternative text after a slash (/). The MDN page for content explains that this alternative text is meant for audio output from screen readers and is exposed to the browser's accessibility tree. If you set the alternative text to an empty string, the column name appears only on screen.
.orders td::before {
content: attr(data-label) / "";
font-weight: bold;
}
In the test environment, the cell names became values only, such as "A-1001" and "2026-11-01", and the columnheader elements remained as they were. At a 360px viewport width, the screenshot was pixel-identical to the one taken before adding / "".
According to the MDN browser compatibility data (@mdn/browser-compat-data 8.1.8), alternative text for content is supported from Chrome 77, Edge 79, Firefox 128, and Safari 17.4 (iOS 17.4).
The same data shows the other features used in this article. attr() inside content is supported from Chrome 2, Edge 12, Firefox 1, and Safari 3.1. clip-path without a vendor prefix (-webkit-) is supported from Chrome 55, Edge 79, Firefox 3.5, and Safari 9.1 (iOS 9.3). Basic shapes such as inset() are available in Firefox from version 54.





