Responsive Table to Cards
A data table that reflows into stacked cards when its container gets narrow, using data-label headings.
AI prompt
Clean UI mockup showing the same order list twice: on the left a wide table with columns Order, Customer, Status and Total, on the right a narrow phone-width version where each row has become a stacked card with small grey labels Customer, Status and Total beside the values and the order number in bold on top, thin dividers, light theme, flat modern style. Negative: gradients, shadows, clutter, text errors, device frames. Reduced motion: static design.About this pattern
Definition
Wide tables are unreadable on a phone. Instead of scrolling sideways, each row can turn into a small card where every cell shows its column name next to its value. A container query makes the switch depend on the space the table actually has, not on the window width.
Purpose & UX principles
- Keep real table markup; only change display with CSS so semantics stay for large screens.
- Hide the header row visually and print each cell's column name with a data-label attribute in a ::before.
- Switch at the width where columns start to squeeze, usually 480 to 600 px.
- Put the most important cell first in the card and make it bold.
Interaction states
- A width slider changes the container; below about 340px the rows become cards.
Motion specification
- The layout switches instantly.
- Reduced motion: no animation is involved.
Responsive behaviour
- When the table becomes cards, keep table roles with role attributes or accept the list semantics.
- Very wide data tables with many columns may still need horizontal scroll.
Code
<div class="bx"><div class="fw" id="fw"><table class="rt"><thead><tr><th>Order</th><th>Customer</th><th>Status</th><th>Total</th></tr></thead><tbody><tr><td data-label="Order">#1042</td><td data-label="Customer">Ada Lopez</td><td data-label="Status">Shipped</td><td data-label="Total">$128.00</td></tr><tr><td data-label="Order">#1043</td><td data-label="Customer">Ben Okoye</td><td data-label="Status">Packing</td><td data-label="Total">$54.90</td></tr></tbody></table></div><label class="rw">Width <b id="wv">400px</b><input id="ws" type="range" min="220" max="400" value="400"></label><p class="out" id="out"></p></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.fw{container-type:inline-size;box-sizing:border-box;margin:0 auto;max-width:100%;border:1px dashed #c9c9c4;border-radius:10px;padding:6px;background:var(--surface)}.rt{width:100%;border-collapse:collapse;font-size:12px}.rt th{text-align:left;font-weight:700;padding:6px 6px;border-bottom:2px solid var(--line)}.rt td{padding:7px 6px;border-bottom:1px solid var(--line)}@container (max-width:340px){.rt thead{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}.rt,.rt tbody,.rt tr,.rt td{display:block}.rt tr{border:1px solid var(--line);border-radius:8px;margin-bottom:6px;padding:4px 8px;background:#fff}.rt td{display:flex;justify-content:space-between;gap:8px;border:0;padding:2px 0}.rt td::before{content:attr(data-label);color:var(--muted);font-size:11px}.rt td:first-child{font-weight:700}}.out{font-size:12px;color:var(--muted);min-height:16px;margin:0}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const ws=document.getElementById('ws'),fw=document.getElementById('fw');
function up(){const w=+ws.value;fw.style.width=w+'px';document.getElementById('wv').textContent=w+'px';document.getElementById('out').textContent=w<=340?'Narrow: every row is a card with labels.':'Wide: a normal table with column headers.';}
ws.addEventListener('input',up);up();One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Responsive Table to Cards — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.fw{container-type:inline-size;box-sizing:border-box;margin:0 auto;max-width:100%;border:1px dashed #c9c9c4;border-radius:10px;padding:6px;background:var(--surface)}.rt{width:100%;border-collapse:collapse;font-size:12px}.rt th{text-align:left;font-weight:700;padding:6px 6px;border-bottom:2px solid var(--line)}.rt td{padding:7px 6px;border-bottom:1px solid var(--line)}@container (max-width:340px){.rt thead{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}.rt,.rt tbody,.rt tr,.rt td{display:block}.rt tr{border:1px solid var(--line);border-radius:8px;margin-bottom:6px;padding:4px 8px;background:#fff}.rt td{display:flex;justify-content:space-between;gap:8px;border:0;padding:2px 0}.rt td::before{content:attr(data-label);color:var(--muted);font-size:11px}.rt td:first-child{font-weight:700}}.out{font-size:12px;color:var(--muted);min-height:16px;margin:0}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="fw" id="fw"><table class="rt"><thead><tr><th>Order</th><th>Customer</th><th>Status</th><th>Total</th></tr></thead><tbody><tr><td data-label="Order">#1042</td><td data-label="Customer">Ada Lopez</td><td data-label="Status">Shipped</td><td data-label="Total">$128.00</td></tr><tr><td data-label="Order">#1043</td><td data-label="Customer">Ben Okoye</td><td data-label="Status">Packing</td><td data-label="Total">$54.90</td></tr></tbody></table></div><label class="rw">Width <b id="wv">400px</b><input id="ws" type="range" min="220" max="400" value="400"></label><p class="out" id="out"></p></div>
</div>
<script>
const ws=document.getElementById('ws'),fw=document.getElementById('fw');
function up(){const w=+ws.value;fw.style.width=w+'px';document.getElementById('wv').textContent=w+'px';document.getElementById('out').textContent=w<=340?'Narrow: every row is a card with labels.':'Wide: a normal table with column headers.';}
ws.addEventListener('input',up);up();
</script>
</body></html>