Expandable Card
A card that expands in place to reveal more content, with a smooth height animation.
AI prompt
Create an expandable card 320px wide. The header is a full-width button containing a 14px semibold title ("Order #4821"), a 12px secondary line ("Arriving Thursday") and a chevron icon on the right. Below it is a detail area with three rows of order information. Hidden by default. Animate the open and close with CSS grid: wrap the details in a container with display grid and grid-template-rows 0fr (collapsed) to 1fr (expanded), transition 280ms cubic-bezier(.2,.7,.2,1), with the inner element overflow hidden and min-height 0. Rotate the chevron 180deg in 220ms. The button has aria-expanded and aria-controls pointing at the region, and the region has role region and aria-labelledby. Hover tints the header with a light surface colour. Show a 2px focus-visible ring. Support Enter and Space natively through the button. Under prefers-reduced-motion remove all transitions.About this pattern
Definition
A card with a summary row and a hidden detail area. Activating the header grows the card to show the details without leaving the page, and activating it again collapses it.
Purpose & UX principles
- Keeps lists scannable while letting people open only what they care about.
- Uses a real button with
aria-expandedandaria-controls, so the pattern works with keyboard and assistive technology. - Animating the height gives continuity: content below moves smoothly instead of jumping.
Interaction states
- Collapsed, expanded, hover (header tint), focus-visible ring, pressed.
- The chevron rotates 180 degrees when expanded.
Motion specification
- Height uses the grid-template-rows 0fr to 1fr technique, 280ms cubic-bezier(.2,.7,.2,1).
- Chevron rotation 220ms; content fades in 180ms after the height starts.
- Reduced motion: the card opens instantly with no height animation or rotation.
Responsive behaviour
- Full width of the container; the toggle button spans the card header for a large touch target.
- Long content scrolls with the page, not inside the card.
Code
<article class="card">
<button class="head" id="h" aria-expanded="false" aria-controls="d">
<span><b>Order #4821</b><small>Arriving Thursday</small></span>
<svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true"><path d="M6 9l6 6 6-6" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
<div class="panel" id="d" role="region" aria-labelledby="h"><div class="in">
<div class="row"><span>Items</span><b>3</b></div>
<div class="row"><span>Shipping</span><b>Free</b></div>
<div class="row"><span>Total</span><b>$84.00</b></div>
</div></div>
</article>: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}
.card{width:min(320px,100%);background:var(--surface);border:1px solid var(--line);border-radius:14px;overflow:hidden}
.head{width:100%;display:flex;align-items:center;justify-content:space-between;gap:12px;padding:14px 16px;text-align:left;background:none;border:0;font:inherit;color:inherit;cursor:pointer;transition:background .16s}
.head:hover{background:var(--surface-2)}
.head:focus-visible{outline:2px solid var(--accent);outline-offset:-2px}
.head b{display:block;font-size:14px;font-weight:600}
.head small{font-size:12px;color:var(--muted)}
.head svg{flex:none;transition:transform .22s cubic-bezier(.2,.7,.2,1)}
.head[aria-expanded=true] svg{transform:rotate(180deg)}
.panel{display:grid;grid-template-rows:0fr;transition:grid-template-rows .28s cubic-bezier(.2,.7,.2,1)}
.panel.open{grid-template-rows:1fr}
.in{overflow:hidden;min-height:0;opacity:0;transition:opacity .18s}
.panel.open .in{opacity:1;transition-delay:.06s}
.row{display:flex;justify-content:space-between;padding:10px 16px;border-top:1px solid var(--line);font-size:13px}
.row span{color:var(--muted)}
@media (prefers-reduced-motion:reduce){.head,.head svg,.panel,.in{transition:none}}const h=document.getElementById('h'),p=document.getElementById('d');
h.addEventListener('click',()=>{const o=h.getAttribute('aria-expanded')==='true';h.setAttribute('aria-expanded',String(!o));p.classList.toggle('open',!o);});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>Expandable Card — 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}}
.card{width:min(320px,100%);background:var(--surface);border:1px solid var(--line);border-radius:14px;overflow:hidden}
.head{width:100%;display:flex;align-items:center;justify-content:space-between;gap:12px;padding:14px 16px;text-align:left;background:none;border:0;font:inherit;color:inherit;cursor:pointer;transition:background .16s}
.head:hover{background:var(--surface-2)}
.head:focus-visible{outline:2px solid var(--accent);outline-offset:-2px}
.head b{display:block;font-size:14px;font-weight:600}
.head small{font-size:12px;color:var(--muted)}
.head svg{flex:none;transition:transform .22s cubic-bezier(.2,.7,.2,1)}
.head[aria-expanded=true] svg{transform:rotate(180deg)}
.panel{display:grid;grid-template-rows:0fr;transition:grid-template-rows .28s cubic-bezier(.2,.7,.2,1)}
.panel.open{grid-template-rows:1fr}
.in{overflow:hidden;min-height:0;opacity:0;transition:opacity .18s}
.panel.open .in{opacity:1;transition-delay:.06s}
.row{display:flex;justify-content:space-between;padding:10px 16px;border-top:1px solid var(--line);font-size:13px}
.row span{color:var(--muted)}
@media (prefers-reduced-motion:reduce){.head,.head svg,.panel,.in{transition:none}}
</style></head>
<body><div class="stage">
<article class="card">
<button class="head" id="h" aria-expanded="false" aria-controls="d">
<span><b>Order #4821</b><small>Arriving Thursday</small></span>
<svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true"><path d="M6 9l6 6 6-6" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
<div class="panel" id="d" role="region" aria-labelledby="h"><div class="in">
<div class="row"><span>Items</span><b>3</b></div>
<div class="row"><span>Shipping</span><b>Free</b></div>
<div class="row"><span>Total</span><b>$84.00</b></div>
</div></div>
</article>
</div>
<script>
const h=document.getElementById('h'),p=document.getElementById('d');
h.addEventListener('click',()=>{const o=h.getAttribute('aria-expanded')==='true';h.setAttribute('aria-expanded',String(!o));p.classList.toggle('open',!o);});
</script>
</body></html>