Build an Accessible FAQ Accordion with HTML, CSS and JavaScript
Most websites need an FAQ section. Instead of a heavy plugin or library, you can build a fast, accessible accordion with the native HTML <details> and <summary> elements, a little CSS, and an optional few lines of JavaScript. It is the same pattern used on this website.
Step 1: HTML
Each question is a <summary> inside a <details> element. This already works: click a question to open its answer.
<div class="faq">
<details>
<summary>How long does a website take?</summary>
<p>About two to three weeks for a business website.</p>
</details>
<details>
<summary>Do I own the website?</summary>
<p>Yes. The domain, hosting and admin access are in your name.</p>
</details>
</div>
Step 2: CSS
Hide the default triangle, add a plus and minus icon, and keep a visible focus outline for keyboard users:
.faq details {
border: 1px solid #ddd;
border-radius: 6px;
margin-bottom: 10px;
background: #fafafa;
}
.faq summary {
cursor: pointer;
padding: 16px 48px 16px 20px;
font-weight: 600;
list-style: none; /* hide the default triangle */
position: relative;
}
.faq summary::-webkit-details-marker {
display: none; /* Safari */
}
.faq summary::after {
content: "+";
position: absolute;
right: 20px;
color: #3d5af1;
font-size: 20px;
}
.faq details[open] summary::after {
content: "\2212"; /* minus sign */
}
.faq summary:focus-visible {
outline: 2px solid #3d5af1;
outline-offset: 2px;
}
.faq details p {
margin: 0;
padding: 0 20px 16px;
}\2212 is the Unicode minus sign, which lines up better with the plus sign than a hyphen.
Step 3: JavaScript (optional): keep one item open
If you want only one answer open at a time, close the others when one opens. The toggle event fires whenever a details element opens or closes:
document.querySelectorAll('.faq details').forEach(function (item) {
item.addEventListener('toggle', function () {
if (!item.open) return;
document.querySelectorAll('.faq details[open]').forEach(function (other) {
if (other !== item) other.open = false;
});
});
});In current browsers you can get the same behaviour with no JavaScript at all by giving every item the same name attribute:
<details name="faq">...</details>
<details name="faq">...</details>
Why this approach is better
- Fast: no library, only a few lines of CSS.
- Accessible: keyboard and screen readers work out of the box.
- SEO-friendly: answers are in the HTML, and you can add FAQPage schema for rich results.
- Works without JavaScript, so nothing breaks if a script fails to load.
Frequently asked questions
Why use details and summary instead of a JavaScript accordion?
They work without JavaScript, are keyboard and screen-reader accessible by default, and the content stays in the HTML, which search engines can read.
Can I open only one item at a time without JavaScript?
Yes, in current browsers. Give every details element the same name attribute and the browser closes the others automatically. Keep the small script as a fallback for older browsers.
How do I get FAQ rich results on Google?
Add FAQPage structured data (JSON-LD) with the same questions and answers that are visible on the page.
Need help with your website?
Tell me what you need and receive a free, page-by-page quotation.
Copyright © 2026 Himanshu Bisoyi All Rights Reserved.
Designed by Himanshu