HTML & CSS · Beginner · 15 min
Build an expandable FAQ
Group questions and answers with native HTML controls and test them with a keyboard.
Give the answer an expandable container
Put one question and its answer inside <details>. Make <summary> its first child and use it for the question. Put the answer in a paragraph below it. The browser lets readers expand and collapse the answer without a script.
<details>
<summary>When does the library open?</summary>
<p>The library opens at 9 a.m. on weekdays.</p>
</details>Expected appearance
When does the library open? Expand the question to read the answer.
Choose the starting state
Include the open attribute on <details> to show the answer when the page loads. Omit the open attribute to start with the answer hidden. The browser treats the presence of open as true. Even the invalid form open="false" starts expanded. Press Tab to reach each question, then press Enter or Space to toggle the answer.
<details open>
<summary>When does the library open?</summary>
<p>The library opens at 9 a.m. on weekdays.</p>
</details>Expected appearance
When does the library open? The library opens at 9 a.m. on weekdays.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Edit the page content and include CSS in a style element. The preview supports a limited set of HTML elements. Scripts, external resources, navigation to other sites, and form submission are disabled.
Build and check
Keep the heading and both question and answer pairs inside .faq. Wrap each pair together in one <details> element. Put a <summary> first for the question and a <p> after it for the answer. Add open to the first <details> only.
Wrap each pair in <details>. Replace the question heading with <summary> and leave the answer in <p>. Put open on the first opening <details> tag.
Show worked solution
<style>
.faq { max-width: 42rem; margin: 1rem auto; font-family: sans-serif; line-height: 1.5; }
.faq details { margin-block: 0.75rem; padding: 0.75rem; border: 1px solid #64748b; }
.faq summary { cursor: pointer; font-weight: 600; }
.faq summary:focus-visible { outline: 3px solid #1d4ed8; outline-offset: 3px; }
</style>
<main class="faq">
<h1>Library questions</h1>
<details open>
<summary>When does the library open?</summary>
<p>The library opens at 9 a.m. on weekdays.</p>
</details>
<details>
<summary>Can I study here?</summary>
<p>Yes. The quiet study area is on the second floor.</p>
</details>
</main>Each <summary> controls the answer in its own <details>. Only the first container has open, so only its answer is shown at the start.
Page preview
Run to preview your page. Check tests the task requirements, including the requested screen widths. Check results do not replace keyboard and visual review.
Try it yourself
- Preview your page and press Tab until a question has focus. Use Enter or Space to open and close its answer.
- Toggle both answers and check that each question stays visible. Keep the focus outline so readers can see where they are.