Accordion
A stack of sections where opening one closes the others, with keyboard arrow navigation.
AI prompt
Create a single-open accordion with three items ("Shipping", "Returns", "Warranty"). Each item has a button header (48px tall, 14px medium text, a plus icon on the right made from two CSS bars) and a panel with a short paragraph. Opening an item closes the others. Animate panels with grid-template-rows from 0fr to 1fr over 260ms cubic-bezier(.2,.7,.2,1), with the inner element overflow hidden and min-height 0. When open, the vertical bar of the plus scales to 0 over 200ms to form a minus. Headers carry aria-expanded and aria-controls; panels use role region and aria-labelledby. ArrowDown and ArrowUp move focus to the next and previous header, Home and End jump to the first and last. Hover tints the header; focus-visible shows a 2px ring. Dividers are 1px lines. Under prefers-reduced-motion remove all transitions.About this pattern
Definition
A vertical list of headings that each reveal a panel. In the single-open variant, opening one section closes the previous one so the list never grows beyond one open panel.
Purpose & UX principles
- Good for FAQs and settings where people read one answer at a time.
- Single-open keeps the page height stable and the user oriented.
- Each header is a button with
aria-expanded; Up and Down arrows move focus between headers.
Interaction states
- Closed, open, hover (tint), focus-visible, pressed.
- The plus icon becomes a minus when open.
Motion specification
- Height animation with grid-template-rows, 260ms cubic-bezier(.2,.7,.2,1).
- The plus icon's vertical bar scales to zero in 200ms.
- Reduced motion: panels open instantly and the icon swaps without animation.
Responsive behaviour
- Full width; headers are at least 48px tall for touch.
- Long panel text wraps; no horizontal scrolling inside panels.
Code
<div class="acc" id="acc">
<div class="item"><h3><button class="hd" id="a1" aria-expanded="true" aria-controls="p1">Shipping<i></i></button></h3><div class="pn open" id="p1" role="region" aria-labelledby="a1"><div class="in"><p>Orders ship within 2 business days. Tracking is emailed once the parcel leaves.</p></div></div></div>
<div class="item"><h3><button class="hd" id="a2" aria-expanded="false" aria-controls="p2">Returns<i></i></button></h3><div class="pn" id="p2" role="region" aria-labelledby="a2"><div class="in"><p>Return unused items within 30 days for a full refund to the original payment method.</p></div></div></div>
<div class="item"><h3><button class="hd" id="a3" aria-expanded="false" aria-controls="p3">Warranty<i></i></button></h3><div class="pn" id="p3" role="region" aria-labelledby="a3"><div class="in"><p>Every product includes a two year limited warranty against manufacturing defects.</p></div></div></div>
</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}
.acc{width:min(340px,100%);background:var(--surface);border:1px solid var(--line);border-radius:14px;overflow:hidden}
.item+.item{border-top:1px solid var(--line)}
h3{font-size:inherit;font-weight:inherit}
.hd{width:100%;min-height:48px;display:flex;align-items:center;justify-content:space-between;padding:0 16px;background:none;border:0;font:inherit;font-size:14px;font-weight:500;color:inherit;cursor:pointer;text-align:left;transition:background .16s}
.hd:hover{background:var(--surface-2)}
.hd:focus-visible{outline:2px solid var(--accent);outline-offset:-2px}
.hd i{position:relative;width:12px;height:12px;flex:none}
.hd i::before,.hd i::after{content:"";position:absolute;background:currentColor;border-radius:1px}
.hd i::before{left:0;right:0;top:5.25px;height:1.5px}
.hd i::after{top:0;bottom:0;left:5.25px;width:1.5px;transition:transform .2s}
.hd[aria-expanded=true] i::after{transform:scaleY(0)}
.pn{display:grid;grid-template-rows:0fr;transition:grid-template-rows .26s cubic-bezier(.2,.7,.2,1)}
.pn.open{grid-template-rows:1fr}
.in{overflow:hidden;min-height:0}
.in p{padding:0 16px 16px;font-size:13px;line-height:1.55;color:var(--muted)}
@media (prefers-reduced-motion:reduce){.pn,.hd i::after,.hd{transition:none}}const hs=[...document.querySelectorAll('.hd')];
function open(h){hs.forEach(x=>{const on=x===h&&x.getAttribute('aria-expanded')!=='true';x.setAttribute('aria-expanded',String(on));document.getElementById(x.getAttribute('aria-controls')).classList.toggle('open',on);});}
hs.forEach((h,i)=>{h.addEventListener('click',()=>open(h));h.addEventListener('keydown',e=>{const m={ArrowDown:(i+1)%hs.length,ArrowUp:(i-1+hs.length)%hs.length,Home:0,End:hs.length-1};if(e.key in m){e.preventDefault();hs[m[e.key]].focus();}});});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>Accordion — 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}}
.acc{width:min(340px,100%);background:var(--surface);border:1px solid var(--line);border-radius:14px;overflow:hidden}
.item+.item{border-top:1px solid var(--line)}
h3{font-size:inherit;font-weight:inherit}
.hd{width:100%;min-height:48px;display:flex;align-items:center;justify-content:space-between;padding:0 16px;background:none;border:0;font:inherit;font-size:14px;font-weight:500;color:inherit;cursor:pointer;text-align:left;transition:background .16s}
.hd:hover{background:var(--surface-2)}
.hd:focus-visible{outline:2px solid var(--accent);outline-offset:-2px}
.hd i{position:relative;width:12px;height:12px;flex:none}
.hd i::before,.hd i::after{content:"";position:absolute;background:currentColor;border-radius:1px}
.hd i::before{left:0;right:0;top:5.25px;height:1.5px}
.hd i::after{top:0;bottom:0;left:5.25px;width:1.5px;transition:transform .2s}
.hd[aria-expanded=true] i::after{transform:scaleY(0)}
.pn{display:grid;grid-template-rows:0fr;transition:grid-template-rows .26s cubic-bezier(.2,.7,.2,1)}
.pn.open{grid-template-rows:1fr}
.in{overflow:hidden;min-height:0}
.in p{padding:0 16px 16px;font-size:13px;line-height:1.55;color:var(--muted)}
@media (prefers-reduced-motion:reduce){.pn,.hd i::after,.hd{transition:none}}
</style></head>
<body><div class="stage">
<div class="acc" id="acc">
<div class="item"><h3><button class="hd" id="a1" aria-expanded="true" aria-controls="p1">Shipping<i></i></button></h3><div class="pn open" id="p1" role="region" aria-labelledby="a1"><div class="in"><p>Orders ship within 2 business days. Tracking is emailed once the parcel leaves.</p></div></div></div>
<div class="item"><h3><button class="hd" id="a2" aria-expanded="false" aria-controls="p2">Returns<i></i></button></h3><div class="pn" id="p2" role="region" aria-labelledby="a2"><div class="in"><p>Return unused items within 30 days for a full refund to the original payment method.</p></div></div></div>
<div class="item"><h3><button class="hd" id="a3" aria-expanded="false" aria-controls="p3">Warranty<i></i></button></h3><div class="pn" id="p3" role="region" aria-labelledby="a3"><div class="in"><p>Every product includes a two year limited warranty against manufacturing defects.</p></div></div></div>
</div>
</div>
<script>
const hs=[...document.querySelectorAll('.hd')];
function open(h){hs.forEach(x=>{const on=x===h&&x.getAttribute('aria-expanded')!=='true';x.setAttribute('aria-expanded',String(on));document.getElementById(x.getAttribute('aria-controls')).classList.toggle('open',on);});}
hs.forEach((h,i)=>{h.addEventListener('click',()=>open(h));h.addEventListener('keydown',e=>{const m={ArrowDown:(i+1)%hs.length,ArrowUp:(i-1+hs.length)%hs.length,Home:0,End:hs.length-1};if(e.key in m){e.preventDefault();hs[m[e.key]].focus();}});});
</script>
</body></html>