Accordion Components in Tailwind, HTML and CSS
Updated 2026-10-28. 5 accordions.
An accordion is a list of headings that each show or hide a panel. The simplest one uses the native details and summary elements and needs no script. For more control, use a button with aria-expanded and aria-controls, and animate height with a grid row going from 0fr to 1fr.
Start with the details and summary version if your content is a plain FAQ. It opens without JavaScript, keyboard and screen reader support come from the browser, and newer browsers slide it open smoothly. The single-open React version suits policies and long answers, where keeping one section open at a time stops the page from growing into a wall of text.
The plus and minus FAQ is the one to pick for a marketing page that wants a heading beside the questions. The nested settings accordion is a different job: it groups switches inside an app, so it shows a count of what is on and indents the rows under a guide line. The card version fits service menus where each row needs an icon, a price and a link.
Every piece comes twice. The React tab uses Tailwind classes and a little state; the HTML tab is plain markup, a style block with colour variables at the top and a few lines of script that set aria-expanded. Paste whichever matches your stack.
Compare them
| Variant | Built with | Size (gzipped) | Running cost | Browser support |
|---|---|---|---|---|
| Details Summary Accordion With Smooth Open | CSS only | 1.5 KB | No script at all; one height transition runs only while a panel is opening or closing. | All current browsers; smooth height in Chrome and Edge 131+, instant elsewhere |
| Card Accordion With Icons | CSS and a small script | 2.3 KB | Each card is plain markup; only the opening card runs a short grid transition. | All current browsers |
| FAQ Accordion With Plus Minus Icon | CSS and a small script | 2 KB | A Set in state and two CSS transitions per row; no layout measuring. | All current browsers |
| Settings Accordion With Nested Sections | CSS and a small script | 2 KB | Closed groups are not rendered at all, so a long settings page stays light. | All current browsers |
| Single-Open React Accordion With aria-controls | CSS and a small script | 1.8 KB | One state value; the grid row transition repaints only the panel that changes. | All current browsers |
Details Summary Accordion With Smooth Open
An order and care FAQ for a small pottery studio, built on the native details and summary elements so every question opens with no script. Where the browser supports interpolate-size and the ::details-content pseudo-element, the answer slides open to its natural height; older browsers open it instantly. On phones the rows keep a 56 pixel tap height and long questions wrap beside the chevron.
<section class="dsa">
<div class="dsa-inner">
<p class="dsa-kicker">Saltmarsh Pottery</p>
<h2 class="dsa-title">Orders and care</h2>
<div class="dsa-list">
<details open>
<summary>How long does a hand-thrown mug take to reach me?<svg viewBox="0 0 20 20" aria-hidden="true"><path d="M5 7.5l5 5 5-5"/></svg></summary>
<p>Every mug is thrown, dried for a week and fired twice, so allow three to four weeks from order to doorstep. We email a photo when yours comes out of the kiln.</p>
</details>
<details>
<summary>Are the glazes safe for food and the dishwasher?<svg viewBox="0 0 20 20" aria-hidden="true"><path d="M5 7.5l5 5 5-5"/></svg></summary>
<p>Yes. All of our glazes are lead free and fired to stoneware temperature. They go in the dishwasher and microwave, though hand washing keeps the matte finishes looking new for longer.</p>
</details>
<details>
<summary>Can I order a set in a colour that is not listed?<svg viewBox="0 0 20 20" aria-hidden="true"><path d="M5 7.5l5 5 5-5"/></svg></summary>
<p>Sets of six or more can be glazed in any of our test colours. Write to the studio with the shade you have in mind and we will send a sample tile first.</p>
</details>
<details>
<summary>What happens if a piece arrives broken?<svg viewBox="0 0 20 20" aria-hidden="true"><path d="M5 7.5l5 5 5-5"/></svg></summary>
<p>Send us a photo within seven days and we will make a replacement in the next firing. You do not need to return the broken piece.</p>
</details>
</div>
</div>
</section>
<style>
/* Change these: page tint, text, rule colour, accent, open speed. */
.dsa { --dsa-bg: #f5f5f4; --dsa-ink: #1c1917; --dsa-muted: #57534e; --dsa-rule: #d6d3d1; --dsa-accent: #b45309; --dsa-speed: .35s; }
.dsa { background: var(--dsa-bg); padding: 72px 20px; font-family: ui-sans-serif, system-ui, sans-serif; color: var(--dsa-ink); }
.dsa-inner { max-width: 42rem; margin: 0 auto; }
.dsa-kicker { margin: 0; font-size: 12px; font-weight: 600; letter-spacing: .2em; text-transform: uppercase; color: #92400e; }
.dsa-title { margin: 8px 0 0; font-size: clamp(24px, 3.4vw, 30px); letter-spacing: -.02em; }
.dsa-list { margin-top: 32px; border-top: 1px solid var(--dsa-rule); }
.dsa details { border-bottom: 1px solid var(--dsa-rule); interpolate-size: allow-keywords; }
/* Smooth open in browsers with ::details-content; others open instantly, which is fine. */
.dsa details::details-content { block-size: 0; overflow: hidden; transition: block-size var(--dsa-speed) cubic-bezier(.2,.7,.2,1), content-visibility var(--dsa-speed) allow-discrete; }
.dsa details[open]::details-content { block-size: auto; }
.dsa summary { list-style: none; display: flex; align-items: center; justify-content: space-between; gap: 16px; min-height: 56px; padding: 16px 0; cursor: pointer; font-size: 16px; font-weight: 500; }
.dsa summary::-webkit-details-marker { display: none; }
.dsa summary:focus-visible { outline: 2px solid var(--dsa-accent); outline-offset: 4px; border-radius: 4px; }
.dsa summary svg { width: 20px; height: 20px; flex-shrink: 0; fill: none; stroke: #78716c; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; transition: transform .3s cubic-bezier(.2,.7,.2,1); }
.dsa details[open] summary svg { transform: rotate(180deg); }
.dsa details p { margin: 0; padding: 0 32px 20px 0; font-size: 15px; line-height: 1.65; color: var(--dsa-muted); }
@media (prefers-reduced-motion: reduce) {
.dsa details::details-content, .dsa summary svg { transition: none; }
}
</style>
With reduced motion: Panels snap open and shut and the chevron flips without turning.
Running cost: No script at all; one height transition runs only while a panel is opening or closing.
- Change --dsa-speed from .35s to tune how fast a panel slides; anything above half a second starts to feel slow.
- Add the same name attribute (for example name="care") to every details element to make the list close one panel when another opens.
- Swap --dsa-accent for your brand colour; it only drives the focus outline, so keep it dark enough to see on the background.
- Remove the open attribute from the first details element if you would rather the list load fully closed.
Full page for Details Summary Accordion With Smooth Open
Card Accordion With Icons
A services list for a dental clinic laid out as separate white cards on a warm sand background, each with a tinted icon tile and a starting price. Opening a card lifts its shadow and reveals a short description with a booking link; the previous card closes. On phones the price moves from the header into the open panel so long names never squeeze.
<section class="cia">
<div class="cia-inner">
<h2>Treatments at Brightwater Dental</h2>
<p class="cia-sub">Tap a treatment to see what is included.</p>
<ul class="cia-list">
<li class="cia-card cia-teal" data-open>
<h3><button type="button" id="cia-b1" aria-expanded="true" aria-controls="cia-p1"><span class="cia-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3c-3 0-5 1.5-5 4.5 0 2 .8 3 1 5.5.3 3 .7 7 2 7s1.2-4 2-4 .7 4 2 4 1.7-4 2-7c.2-2.5 1-3.5 1-5.5C17 4.5 15 3 12 3z"/></svg></span><span class="cia-name">Check-up and clean</span><span class="cia-price">From $95</span><svg class="cia-chev" viewBox="0 0 20 20" aria-hidden="true"><path d="M5 7.5l5 5 5-5"/></svg></button></h3>
<div class="cia-panel" id="cia-p1" role="region" aria-labelledby="cia-b1"><div><div class="cia-body"><p>A full examination, scale and polish with our hygienist, plus X-rays once every two years. Plan for forty minutes in the chair.</p><a href="#">Book this</a></div></div></div>
</li>
<li class="cia-card cia-amber">
<h3><button type="button" id="cia-b2" aria-expanded="false" aria-controls="cia-p2"><span class="cia-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3v3M12 18v3M3 12h3M18 12h3M5.6 5.6l2.1 2.1M16.3 16.3l2.1 2.1M5.6 18.4l2.1-2.1M16.3 7.7l2.1-2.1M12 8a4 4 0 100 8 4 4 0 000-8z"/></svg></span><span class="cia-name">Whitening</span><span class="cia-price">From $340</span><svg class="cia-chev" viewBox="0 0 20 20" aria-hidden="true"><path d="M5 7.5l5 5 5-5"/></svg></button></h3>
<div class="cia-panel" id="cia-p2" role="region" aria-labelledby="cia-b2" inert><div><div class="cia-body"><p>Custom trays made from a scan of your teeth and two weeks of gel to use at home. Most people see four to six shades lighter.</p><a href="#">Book this</a></div></div></div>
</li>
<li class="cia-card cia-rose">
<h3><button type="button" id="cia-b3" aria-expanded="false" aria-controls="cia-p3"><span class="cia-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3a9 9 0 100 18 9 9 0 000-18zM12 7v5l3 2"/></svg></span><span class="cia-name">Emergency appointments</span><span class="cia-price">Same day</span><svg class="cia-chev" viewBox="0 0 20 20" aria-hidden="true"><path d="M5 7.5l5 5 5-5"/></svg></button></h3>
<div class="cia-panel" id="cia-p3" role="region" aria-labelledby="cia-b3" inert><div><div class="cia-body"><p>Call before 10am and we will see you the same day for pain, swelling or a broken tooth. Evening slots are kept free on weekdays.</p><a href="#">Book this</a></div></div></div>
</li>
<li class="cia-card cia-sky">
<h3><button type="button" id="cia-b4" aria-expanded="false" aria-controls="cia-p4"><span class="cia-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M8 10a2 2 0 100-4 2 2 0 000 4zM16 10a2 2 0 100-4 2 2 0 000 4zM7 15c1.3 1.6 3 2.5 5 2.5s3.7-.9 5-2.5"/></svg></span><span class="cia-name">Children's dentistry</span><span class="cia-price">Free under 12</span><svg class="cia-chev" viewBox="0 0 20 20" aria-hidden="true"><path d="M5 7.5l5 5 5-5"/></svg></button></h3>
<div class="cia-panel" id="cia-p4" role="region" aria-labelledby="cia-b4" inert><div><div class="cia-body"><p>Short, gentle visits with a dentist who sees children every day. Fissure sealants and fluoride varnish are included.</p><a href="#">Book this</a></div></div></div>
</li>
</ul>
</div>
</section>
<style>
/* Change these: page colour, card radius, button colour. */
.cia { --cia-bg: #f6f1ea; --cia-radius: 16px; --cia-ink: #1c1917; }
.cia { background: var(--cia-bg); padding: 72px 20px; font-family: ui-sans-serif, system-ui, sans-serif; color: var(--cia-ink); }
.cia-inner { max-width: 42rem; margin: 0 auto; }
.cia h2 { margin: 0; text-align: center; font-size: clamp(24px, 3.4vw, 30px); letter-spacing: -.02em; }
.cia-sub { margin: 8px 0 0; text-align: center; font-size: 15px; color: #57534e; }
.cia-list { list-style: none; margin: 40px 0 0; padding: 0; display: grid; gap: 12px; }
.cia-card { background: #fff; border-radius: var(--cia-radius); box-shadow: 0 1px 2px rgba(120,113,108,.2); transition: box-shadow .2s; }
.cia-card[data-open] { box-shadow: 0 6px 16px rgba(120,113,108,.18); }
.cia h3 { margin: 0; }
.cia button { display: flex; font-weight: 400 !important; width: 100%; min-height: 72px; align-items: center; gap: 16px; padding: 16px; border: 0; border-radius: var(--cia-radius); background: none; text-align: left; cursor: pointer; font: inherit; color: inherit; }
.cia button:focus-visible, .cia-body a:focus-visible { outline: 2px solid var(--cia-ink); outline-offset: 2px; }
.cia-icon { display: grid; place-items: center; width: 44px; height: 44px; flex-shrink: 0; border-radius: 12px; }
.cia-icon svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.cia-teal .cia-icon { background: #ccfbf1; color: #115e59; }
.cia-amber .cia-icon { background: #fef3c7; color: #92400e; }
.cia-rose .cia-icon { background: #ffe4e6; color: #9f1239; }
.cia-sky .cia-icon { background: #e0f2fe; color: #075985; }
.cia-name { flex: 1; font-size: 16px; font-weight: 600; }
.cia-price { font-size: 14px; color: #57534e; }
.cia-chev { width: 20px; height: 20px; flex-shrink: 0; fill: none; stroke: #78716c; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; transition: transform .3s; }
.cia-card[data-open] .cia-chev { transform: rotate(180deg); }
.cia-panel { display: grid; grid-template-rows: 0fr; transition: grid-template-rows .3s cubic-bezier(.2,.7,.2,1); }
.cia-card[data-open] .cia-panel { grid-template-rows: 1fr; }
.cia-panel > div { overflow: hidden; }
.cia-body { padding: 0 16px 20px 76px; }
.cia-body p { margin: 0; font-size: 15px; line-height: 1.65; color: #44403c; }
.cia-body a { display: inline-flex; align-items: center; height: 40px; margin-top: 16px; padding: 0 16px; border-radius: 999px; background: var(--cia-ink); color: #fff; font-size: 14px; font-weight: 500; text-decoration: none; }
@media (max-width: 520px) { .cia-body { padding-left: 16px; } .cia-price { display: none; } }
@media (prefers-reduced-motion: reduce) { .cia-panel, .cia-chev, .cia-card { transition: none; } }
</style>
<script>
(function () {
var root = document.querySelector(".cia");
if (!root) return;
var cards = root.querySelectorAll(".cia-card");
cards.forEach(function (card) {
card.querySelector("button").addEventListener("click", function () {
var was = card.hasAttribute("data-open");
cards.forEach(function (c) {
var on = c === card && !was;
c.toggleAttribute("data-open", on);
c.querySelector("button").setAttribute("aria-expanded", on);
c.querySelector(".cia-panel").toggleAttribute("inert", !on);
});
});
});
})();
</script>
With reduced motion: The chosen card opens at full height at once and the chevron flips straight over.
Running cost: Each card is plain markup; only the opening card runs a short grid transition.
- Change each tone string (bg-teal-100 text-teal-800 and so on) to recolour one icon tile without touching the rest.
- Replace an icon by editing its single path in the icon field; any 24 by 24 stroke path fits.
- Point the Book this link at your booking page, or delete it for a plain information list.
FAQ Accordion With Plus Minus Icon
A dark two-column FAQ for a coworking space: a short heading on the left, questions on the right. Each round icon is a plus whose vertical bar turns flat when its answer opens, so it reads as a minus, and the circle fills with sky blue. Several answers may be open at once. Below 768 pixels the heading sits above the list.
<section class="fpm">
<div class="fpm-inner">
<div>
<p class="fpm-kicker">Ropewalk Studios</p>
<h2 class="fpm-title">Questions before you join</h2>
<p class="fpm-lead">Something else on your mind? Ask at the front desk or reply to your welcome email.</p>
</div>
<ul class="fpm-list">
<li data-open>
<h3><button type="button" id="fpm-b1" aria-expanded="true" aria-controls="fpm-p1">Can I bring a guest for the day?<span class="fpm-icon"><svg viewBox="0 0 16 16" aria-hidden="true"><path d="M2 8h12"/><path class="fpm-v" d="M8 2v12"/></svg></span></button></h3>
<div class="fpm-panel" id="fpm-p1" role="region" aria-labelledby="fpm-b1"><div><p>Members on the Resident plan can sign in two guests a day at the front desk. Guests get Wi-Fi, coffee and a hot desk, but not the phone booths.</p></div></div>
</li>
<li>
<h3><button type="button" id="fpm-b2" aria-expanded="false" aria-controls="fpm-p2">Are the desks available at night?<span class="fpm-icon"><svg viewBox="0 0 16 16" aria-hidden="true"><path d="M2 8h12"/><path class="fpm-v" d="M8 2v12"/></svg></span></button></h3>
<div class="fpm-panel" id="fpm-p2" role="region" aria-labelledby="fpm-b2" inert><div><p>Fixed desks are open around the clock with your key fob. Hot desks and the kitchen close at 10pm so the cleaners can get in.</p></div></div>
</li>
<li>
<h3><button type="button" id="fpm-b3" aria-expanded="false" aria-controls="fpm-p3">How does billing work if I pause?<span class="fpm-icon"><svg viewBox="0 0 16 16" aria-hidden="true"><path d="M2 8h12"/><path class="fpm-v" d="M8 2v12"/></svg></span></button></h3>
<div class="fpm-panel" id="fpm-p3" role="region" aria-labelledby="fpm-b3" inert><div><p>You can pause once a year for up to two months. We hold your locker and stop charging from the first of the next month.</p></div></div>
</li>
<li>
<h3><button type="button" id="fpm-b4" aria-expanded="false" aria-controls="fpm-p4">Is there parking for bikes?<span class="fpm-icon"><svg viewBox="0 0 16 16" aria-hidden="true"><path d="M2 8h12"/><path class="fpm-v" d="M8 2v12"/></svg></span></button></h3>
<div class="fpm-panel" id="fpm-p4" role="region" aria-labelledby="fpm-b4" inert><div><p>There is a covered, locked bike room in the basement with forty stands, a pump and a shower next door.</p></div></div>
</li>
<li>
<h3><button type="button" id="fpm-b5" aria-expanded="false" aria-controls="fpm-p5">Can I use the address for my company?<span class="fpm-icon"><svg viewBox="0 0 16 16" aria-hidden="true"><path d="M2 8h12"/><path class="fpm-v" d="M8 2v12"/></svg></span></button></h3>
<div class="fpm-panel" id="fpm-p5" role="region" aria-labelledby="fpm-b5" inert><div><p>Yes. Any paid plan includes a registered business address and post handling, with a photo of each letter sent the day it arrives.</p></div></div>
</li>
</ul>
</div>
</section>
<style>
/* Change these: background, accent for the open icon, rule colour. */
.fpm { --fpm-bg: #020617; --fpm-accent: #7dd3fc; --fpm-rule: rgba(255,255,255,.1); }
.fpm { background: var(--fpm-bg); color: #fff; padding: 80px 20px; font-family: ui-sans-serif, system-ui, sans-serif; }
.fpm-inner { max-width: 64rem; margin: 0 auto; display: grid; gap: 40px; }
@media (min-width: 768px) { .fpm-inner { grid-template-columns: 1fr 1.6fr; gap: 64px; } }
.fpm-kicker { margin: 0; font-size: 14px; font-weight: 500; color: var(--fpm-accent); }
.fpm-title { margin: 8px 0 0; font-size: 30px; letter-spacing: -.02em; line-height: 1.2; }
.fpm-lead { margin: 16px 0 0; font-size: 15px; line-height: 1.65; color: #94a3b8; }
.fpm-list { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--fpm-rule); }
.fpm-list li { border-bottom: 1px solid var(--fpm-rule); }
.fpm h3 { margin: 0; }
.fpm button { display: flex; width: 100%; min-height: 60px; align-items: center; justify-content: space-between; gap: 24px; padding: 20px 0; border: 0; background: none; color: #fff; font: 500 17px ui-sans-serif, system-ui, sans-serif; text-align: left; cursor: pointer; }
.fpm button:focus-visible { outline: 2px solid var(--fpm-accent); outline-offset: 4px; border-radius: 4px; }
.fpm-icon { display: grid; place-items: center; width: 32px; height: 32px; flex-shrink: 0; border: 1px solid rgba(255,255,255,.25); border-radius: 50%; transition: background-color .2s, border-color .2s, color .2s; }
.fpm-icon svg { width: 14px; height: 14px; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.fpm-v { transform-box: fill-box; transform-origin: center; transition: transform .3s cubic-bezier(.2,.7,.2,1); }
.fpm li[data-open] .fpm-icon { background: var(--fpm-accent); border-color: var(--fpm-accent); color: var(--fpm-bg); }
/* The vertical bar turns flat, so plus becomes minus. */
.fpm li[data-open] .fpm-v { transform: rotate(90deg); }
.fpm-panel { display: grid; grid-template-rows: 0fr; opacity: 0; transition: grid-template-rows .3s ease, opacity .3s ease; }
.fpm li[data-open] .fpm-panel { grid-template-rows: 1fr; opacity: 1; }
.fpm-panel > div { overflow: hidden; }
.fpm-panel p { margin: 0; padding: 0 48px 24px 0; font-size: 15px; line-height: 1.65; color: #cbd5e1; }
@media (prefers-reduced-motion: reduce) { .fpm-panel, .fpm-v, .fpm-icon { transition: none; } }
</style>
<script>
(function () {
var root = document.querySelector(".fpm");
if (!root) return;
root.querySelectorAll(".fpm-list li").forEach(function (li) {
var btn = li.querySelector("button"), panel = li.querySelector(".fpm-panel");
btn.addEventListener("click", function () {
var on = !li.hasAttribute("data-open");
li.toggleAttribute("data-open", on);
btn.setAttribute("aria-expanded", on);
panel.toggleAttribute("inert", !on);
});
});
})();
</script>
With reduced motion: The plus swaps to a minus instantly and answers show without a fade.
Running cost: A Set in state and two CSS transitions per row; no layout measuring.
- Change the sky-300 classes (or --fpm-accent in the HTML version) to recolour the filled icon and the focus ring together.
- Make it single-open by replacing the Set with one index, as the returns policy piece above does.
- Move the icon to the left of each question by putting the span before the text and dropping justify-between.
Settings Accordion With Nested Sections
An app settings panel for a pilates instructor where each group (notifications, privacy, billing) opens to reveal its own rows of switches, indented under a guide line so the nesting is obvious. A badge on each header counts how many switches in that group are on, so you can read the state without opening it. Rows keep their label and hint stacked on narrow phones.
<section class="nsa">
<div class="nsa-card">
<div class="nsa-top">
<h2>Settings</h2>
<p>Juniper Pilates, instructor account</p>
</div>
<div class="nsa-group">
<h3><button type="button" class="nsa-head" id="nsa-notify" aria-expanded="true" aria-controls="nsa-notify-rows"><svg viewBox="0 0 20 20" aria-hidden="true"><path d="M7.5 5l5 5-5 5"/></svg><span class="nsa-text"><span class="nsa-gt">Notifications</span><span class="nsa-gs">Email, push and the weekly digest</span></span><span class="nsa-count">2 on</span></button></h3>
<ul class="nsa-rows" id="nsa-notify-rows" role="region" aria-labelledby="nsa-notify">
<li><span><span class="nsa-label" id="nsa-l-n1">New booking</span><span class="nsa-hint">Email me the moment a client books a slot</span></span><button type="button" role="switch" aria-checked="true" aria-labelledby="nsa-l-n1"><span></span></button></li>
<li><span><span class="nsa-label" id="nsa-l-n2">Cancellations</span><span class="nsa-hint">Push alert when a booking is cancelled</span></span><button type="button" role="switch" aria-checked="true" aria-labelledby="nsa-l-n2"><span></span></button></li>
<li><span><span class="nsa-label" id="nsa-l-n3">Weekly digest</span><span class="nsa-hint">A Monday summary of last week's sessions</span></span><button type="button" role="switch" aria-checked="false" aria-labelledby="nsa-l-n3"><span></span></button></li>
</ul>
</div>
<div class="nsa-group">
<h3><button type="button" class="nsa-head" id="nsa-privacy" aria-expanded="false" aria-controls="nsa-privacy-rows"><svg viewBox="0 0 20 20" aria-hidden="true"><path d="M7.5 5l5 5-5 5"/></svg><span class="nsa-text"><span class="nsa-gt">Privacy</span><span class="nsa-gs">Who can see your profile and calendar</span></span><span class="nsa-count">1 on</span></button></h3>
<ul class="nsa-rows" id="nsa-privacy-rows" role="region" aria-labelledby="nsa-privacy" hidden>
<li><span><span class="nsa-label" id="nsa-l-p1">Public profile</span><span class="nsa-hint">Show your page in the studio directory</span></span><button type="button" role="switch" aria-checked="true" aria-labelledby="nsa-l-p1"><span></span></button></li>
<li><span><span class="nsa-label" id="nsa-l-p2">Show full calendar</span><span class="nsa-hint">Let clients see busy times, not just free ones</span></span><button type="button" role="switch" aria-checked="false" aria-labelledby="nsa-l-p2"><span></span></button></li>
</ul>
</div>
<div class="nsa-group">
<h3><button type="button" class="nsa-head" id="nsa-billing" aria-expanded="false" aria-controls="nsa-billing-rows"><svg viewBox="0 0 20 20" aria-hidden="true"><path d="M7.5 5l5 5-5 5"/></svg><span class="nsa-text"><span class="nsa-gt">Billing</span><span class="nsa-gs">Invoices, receipts and payment reminders</span></span><span class="nsa-count">1 on</span></button></h3>
<ul class="nsa-rows" id="nsa-billing-rows" role="region" aria-labelledby="nsa-billing" hidden>
<li><span><span class="nsa-label" id="nsa-l-b1">Send receipts</span><span class="nsa-hint">Email clients a receipt after each payment</span></span><button type="button" role="switch" aria-checked="true" aria-labelledby="nsa-l-b1"><span></span></button></li>
<li><span><span class="nsa-label" id="nsa-l-b2">Overdue reminders</span><span class="nsa-hint">Nudge clients three days after an unpaid invoice</span></span><button type="button" role="switch" aria-checked="false" aria-labelledby="nsa-l-b2"><span></span></button></li>
</ul>
</div>
</div>
</section>
<style>
/* Change these: page tint, accent for switches and badges, guide line. */
.nsa { --nsa-bg: #f5f3ff; --nsa-accent: #7c3aed; --nsa-soft: #ede9fe; --nsa-line: #ddd6fe; }
.nsa { background: var(--nsa-bg); padding: 64px 20px; font-family: ui-sans-serif, system-ui, sans-serif; color: #0f172a; }
.nsa-card { max-width: 36rem; margin: 0 auto; padding: 8px; background: #fff; border: 1px solid var(--nsa-soft); border-radius: 16px; box-shadow: 0 1px 2px rgba(15,23,42,.06); }
.nsa-top { padding: 16px 16px 12px; }
.nsa-top h2 { margin: 0; font-size: 18px; }
.nsa-top p { margin: 2px 0 0; font-size: 14px; color: #475569; }
.nsa-group { border-top: 1px solid #f1f5f9; }
.nsa h3 { margin: 0; }
.nsa-head { display: flex; font-weight: 400 !important; width: 100%; min-height: 64px; align-items: center; gap: 12px; padding: 12px 16px; border: 0; border-radius: 12px; background: none; text-align: left; cursor: pointer; font: inherit; color: inherit; }
.nsa-head:hover { background: #f8fafc; }
.nsa button:focus-visible { outline: 2px solid var(--nsa-accent); outline-offset: 2px; }
.nsa-head svg { width: 20px; height: 20px; flex-shrink: 0; fill: none; stroke: #64748b; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; transition: transform .2s; }
.nsa-head[aria-expanded="true"] svg { transform: rotate(90deg); }
.nsa-text { flex: 1; }
.nsa-gt { display: block; font-size: 15px; font-weight: 600; }
.nsa-gs { display: block; font-size: 14px; color: #475569; }
.nsa-count { padding: 2px 10px; border-radius: 999px; background: var(--nsa-soft); color: #5b21b6; font-size: 12px; font-weight: 500; }
.nsa-rows { list-style: none; margin: 0 8px 8px 44px; padding: 0; border-left: 1px solid var(--nsa-line); }
.nsa-rows li { display: flex; align-items: center; gap: 16px; padding: 12px 8px 12px 16px; }
.nsa-rows li > span { flex: 1; }
.nsa-label { display: block; font-size: 14px; font-weight: 500; }
.nsa-hint { display: block; font-size: 13px; color: #475569; }
.nsa [role="switch"] { position: relative; width: 44px; height: 24px; flex-shrink: 0; border: 0; border-radius: 999px; background: #cbd5e1; cursor: pointer; transition: background-color .2s; }
.nsa [role="switch"] span { position: absolute; left: 2px; top: 2px; width: 20px; height: 20px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.2); transition: transform .2s; }
.nsa [role="switch"][aria-checked="true"] { background: var(--nsa-accent); }
.nsa [role="switch"][aria-checked="true"] span { transform: translateX(20px); }
@media (prefers-reduced-motion: reduce) { .nsa-head svg, .nsa [role="switch"], .nsa [role="switch"] span { transition: none; } }
</style>
<script>
(function () {
var root = document.querySelector(".nsa");
if (!root) return;
root.querySelectorAll(".nsa-group").forEach(function (group) {
var head = group.querySelector(".nsa-head"), rows = group.querySelector(".nsa-rows"), count = group.querySelector(".nsa-count");
head.addEventListener("click", function () {
var on = head.getAttribute("aria-expanded") !== "true";
head.setAttribute("aria-expanded", on);
rows.hidden = !on;
});
rows.querySelectorAll('[role="switch"]').forEach(function (sw) {
sw.addEventListener("click", function () {
sw.setAttribute("aria-checked", sw.getAttribute("aria-checked") !== "true");
count.textContent = rows.querySelectorAll('[aria-checked="true"]').length + " on";
});
});
});
})();
</script>
With reduced motion: Chevrons and switch knobs jump to their new position without sliding.
Running cost: Closed groups are not rendered at all, so a long settings page stays light.
- Add a group by pushing another object into groups with its own rows; ids must be unique because they wire up the labels.
- Open every group at load by seeding openGroups with all ids, or none with an empty array.
- Replace violet-600 on the switch and violet-100 on the badge to match your app's accent.
Single-Open React Accordion With aria-controls
A returns policy for an outdoor shop where opening one section closes the last, kept in a single piece of React state. Each header is a button inside an h3 with aria-expanded and aria-controls, and the panel is a labelled region. Height animates with the grid-template-rows 0fr to 1fr trick, so no measuring is needed. Cards stack full width on phones.
<section class="soa">
<div class="soa-inner">
<h2 class="soa-title">Returns and repairs</h2>
<p class="soa-sub">Fellside Outfitters. One section opens at a time.</p>
<div class="soa-list">
<div class="soa-item" data-open>
<h3><button type="button" id="soa-b1" aria-expanded="true" aria-controls="soa-p1">Return window<svg viewBox="0 0 20 20" aria-hidden="true"><path d="M5 7.5l5 5 5-5"/></svg></button></h3>
<div class="soa-panel" id="soa-p1" role="region" aria-labelledby="soa-b1"><div><p>Unused gear can come back within 60 days of delivery. Tents and sleeping bags that have been slept in once are still accepted if they are clean and dry.</p></div></div>
</div>
<div class="soa-item">
<h3><button type="button" id="soa-b2" aria-expanded="false" aria-controls="soa-p2">Repairs before replacements<svg viewBox="0 0 20 20" aria-hidden="true"><path d="M5 7.5l5 5 5-5"/></svg></button></h3>
<div class="soa-panel" id="soa-p2" role="region" aria-labelledby="soa-b2" inert><div><p>Torn seams, stuck zips and snapped poles are fixed free in our Kendal workshop for the life of the product. Most repairs are posted back inside ten working days.</p></div></div>
</div>
<div class="soa-item">
<h3><button type="button" id="soa-b3" aria-expanded="false" aria-controls="soa-p3">How refunds are paid<svg viewBox="0 0 20 20" aria-hidden="true"><path d="M5 7.5l5 5 5-5"/></svg></button></h3>
<div class="soa-panel" id="soa-p3" role="region" aria-labelledby="soa-b3" inert><div><p>Refunds go back to the card you paid with within three working days of the parcel reaching us. Store credit is added instantly if you prefer it.</p></div></div>
</div>
<div class="soa-item">
<h3><button type="button" id="soa-b4" aria-expanded="false" aria-controls="soa-p4">Items we cannot take back<svg viewBox="0 0 20 20" aria-hidden="true"><path d="M5 7.5l5 5 5-5"/></svg></button></h3>
<div class="soa-panel" id="soa-p4" role="region" aria-labelledby="soa-b4" inert><div><p>Worn base layers, opened gas canisters and anything cut to length, such as guy line or webbing, cannot be returned for hygiene and safety reasons.</p></div></div>
</div>
</div>
</div>
</section>
<style>
/* Change these: accent, open tint, border, open speed. */
.soa { --soa-accent: #047857; --soa-tint: #f0fdf4; --soa-line: #e2e8f0; --soa-speed: .3s; }
.soa { background: #fff; padding: 72px 20px; font-family: ui-sans-serif, system-ui, sans-serif; color: #0f172a; }
.soa-inner { max-width: 42rem; margin: 0 auto; }
.soa-title { margin: 0; font-size: clamp(24px, 3.4vw, 30px); letter-spacing: -.02em; }
.soa-sub { margin: 8px 0 0; font-size: 15px; color: #475569; }
.soa-list { margin-top: 32px; display: grid; gap: 12px; }
.soa-item { border: 1px solid var(--soa-line); border-radius: 12px; transition: border-color .2s, background-color .2s; }
.soa-item[data-open] { border-color: var(--soa-accent); background: var(--soa-tint); }
.soa h3 { margin: 0; }
.soa button { display: flex; width: 100%; min-height: 56px; align-items: center; justify-content: space-between; gap: 16px; padding: 16px 20px; border: 0; border-radius: 12px; background: none; font: 600 16px ui-sans-serif, system-ui, sans-serif; color: inherit; text-align: left; cursor: pointer; }
.soa button:focus-visible { outline: 2px solid var(--soa-accent); outline-offset: 2px; }
.soa button svg { width: 20px; height: 20px; flex-shrink: 0; fill: none; stroke: #64748b; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; transition: transform var(--soa-speed); }
.soa-item[data-open] button svg { transform: rotate(180deg); stroke: var(--soa-accent); }
/* The 0fr to 1fr grid row trick animates to the content's real height. */
.soa-panel { display: grid; grid-template-rows: 0fr; transition: grid-template-rows var(--soa-speed) cubic-bezier(.2,.7,.2,1); }
.soa-item[data-open] .soa-panel { grid-template-rows: 1fr; }
.soa-panel > div { overflow: hidden; }
.soa-panel p { margin: 0; padding: 0 20px 20px; font-size: 15px; line-height: 1.65; color: #334155; }
@media (prefers-reduced-motion: reduce) { .soa-panel, .soa button svg, .soa-item { transition: none; } }
</style>
<script>
(function () {
var root = document.querySelector(".soa");
if (!root) return;
var items = root.querySelectorAll(".soa-item");
items.forEach(function (item) {
item.querySelector("button").addEventListener("click", function () {
var wasOpen = item.hasAttribute("data-open");
items.forEach(function (other) {
var on = other === item && !wasOpen;
other.toggleAttribute("data-open", on);
other.querySelector("button").setAttribute("aria-expanded", on);
other.querySelector(".soa-panel").toggleAttribute("inert", !on);
});
});
});
})();
</script>
With reduced motion: Sections appear and disappear in one step with no sliding height.
Running cost: One state value; the grid row transition repaints only the panel that changes.
- Start with every section closed by changing useState<number | null>(0) to useState<number | null>(null).
- Allow several open sections by storing a Set of indexes instead of one number and toggling membership on click.
- Swap the emerald-600 border and emerald-50 tint on the open card for your own accent pair.
Performance notes
Accordions are cheap as long as you never measure heights in script. These animate the grid-template-rows track or the block-size of ::details-content, both handled by the browser in one layout pass, instead of reading scrollHeight on every click and writing a pixel value back.
Closed panels stay in the document so search engines and find-in-page can still reach them, but they are marked inert or hidden so the keyboard skips them. The settings version goes further and does not render closed groups at all, which matters when each row holds its own switch and state. None of the five loads anything beyond its own few kilobytes.
Questions
How do I make an accordion with only HTML and CSS?
Wrap each question and answer in a details element with the question inside summary. The browser handles opening, closing and keyboard support. Add the same name attribute to every details element if only one should be open at a time.
How do I animate accordion height to auto?
Put the panel in a grid container and transition grid-template-rows from 0fr to 1fr, with overflow hidden on the inner element. In Chrome and Edge you can also set interpolate-size: allow-keywords and transition height to auto directly.
Which ARIA attributes does an accordion need?
Each header should be a button inside a heading element, with aria-expanded showing whether its panel is open and aria-controls pointing at the panel id. The panel can be a region labelled by its button. Native details and summary need none of this.
Should an accordion allow more than one open panel?
For an FAQ, letting several stay open helps people compare answers. For a policy or a checkout step, one at a time keeps the page short and focused. Both patterns are shown here so you can pick per page.
Do search engines index text inside a closed accordion?
Yes. Google reads content that is in the HTML even when it is collapsed, as long as it is not loaded only after a click. Keep the answers in the markup rather than fetching them on open.
Also in the gallery: Accordion Collection with Styled details and summary, ARIA and React, plus FAQ Schema, FAQ Accordion in React Beside an Opal Glass Globe Lamp in WebGL That Glows Brighter as Each Answer Opens.
More collections: Bento Grid Layouts in CSS and Tailwind, React Table Components With Tailwind and Plain HTML, Expanding Cards and Image Panels. All faq and accordions. Build your site free to have a full site built around them.
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App