HTML & CSSEasy
What is <details> and <summary>?
<details> creates a native disclosure widget, a section that users can expand and collapse, and <summary> is its always-visible, clickable heading. It works with the keyboard, is announced correctly by screen readers and needs no JavaScript, making it ideal for FAQs, accordions and "show more" sections.
Features
- The
openattribute controls (and reflects) the expanded state. - The
toggleevent fires whenever it opens or closes. - Exclusive accordions: give several
<details>the samenameattribute and only one can be open at a time (supported in all modern browsers). - Find-in-page: browsers can automatically expand a closed
<details>when the user searches (Ctrl+F) for text inside it.
Styling
- The marker is
summary::marker(or hide it withlist-style: noneand draw your own icon). - Style the open state with
details[open]. - Newer browsers support
::details-contentandinterpolate-size: allow-keywordsto animate the height smoothly.
Accessibility notes
<summary>acts as a button with an expanded/collapsed state; do not put other interactive elements inside it.- Keep summary text concise and descriptive.
When not to use it
For tabs, menus or anything that needs a different interaction model, use the appropriate pattern instead; <details> is strictly a show/hide disclosure.
Code example
html
1<style>2 body { font-family: system-ui; max-width: 480px; }3 details { border: 2px solid #0b1020; border-radius: 8px; padding: 8px 12px; margin: 8px 0; }4 summary { font-weight: 700; cursor: pointer; list-style: none; }5 summary::before { content: "+ "; }6 details[open] summary::before { content: "− "; }7 details[open] { background: #fef9c3; }8</style>9<h2>FAQ</h2>10<!-- Same name = exclusive accordion: opening one closes the others -->11<details name="faq" open>12 <summary>Do I need an account?</summary>13 <p>No. Your progress is saved in your browser.</p>14</details>15<details name="faq">16 <summary>Is it free?</summary>17 <p>Yes, every question and the playground are free.</p>18</details>19<details name="faq">20 <summary>Can I export my progress?</summary>21 <p>Use the export button to download a JSON backup.</p>22</details>23<p id="log"></p>24<script>25 document.querySelectorAll("details").forEach((d) =>26 d.addEventListener("toggle", () => {27 if (d.open) document.getElementById("log").textContent = "Opened: " + d.querySelector("summary").textContent;28 })29 );30</script>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.