Interactive elements: details, summary & dialog
5 min lesson
- HTML has built-in interactive widgets that need no JavaScript: the details toggle and the dialog box.
- <details> with <summary> creates a collapsible section the user can open and close.
- <dialog> shows a modal box for alerts, confirmations and small forms.
- Think of details as a folded note and summary as its label; dialog is a pop-up card in front of the page.
- The summary is the always-visible heading; the rest of the details is hidden until clicked.
- This builds on Form submission: a dialog often wraps a small form.
- The example shows a details FAQ and a dialog opened by a button; you will see the toggle work and a small pop-up.
- This finishes the Content group; next you can style all of this with CSS.
- Use details and summary for FAQs, spoilers and long optional content.
- Use dialog for focused interruptions, not for main content.
- Always give the dialog a title and a close button.
- These widgets are accessible by default, so prefer them over custom div-based ones.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Interactive elements</title>
</head>
<body>
<!-- A collapsible FAQ item; closed by default -->
<details>
<summary>What is HTML?</summary>
<p>HTML is the language that structures web content.</p>
</details>
<!-- A button that opens the dialog with a tiny script -->
<button id="open">Show dialog</button>
<dialog id="box">
<p>This is a dialog box.</p>
<button id="close">Close</button>
</dialog>
<!-- Minimal script: open and close the dialog -->
<script>
document.getElementById('open').addEventListener('click', function () {
document.getElementById('box').showModal();
});
document.getElementById('close').addEventListener('click', function () {
document.getElementById('box').close();
});
</script>
</body>
</html>What happens: A page with the collapsed summary "What is HTML?" that opens to show "HTML is the language that structures web content." when clicked, plus a "Show dialog" button that opens a pop-up saying "This is a dialog box." with a "Close" button.
Breaking it down
details
<details>...</details>A collapsible section. The browser adds a triangle arrow the user can click to open or close it.
summary
<summary>What is HTML?</summary>The always-visible label of the details. It must be the first child of details.
dialog
<dialog id="box">...</dialog>A pop-up box hidden by default. It is shown with JavaScript's showModal() and closed with close().
script
document.getElementById('open').addEventListener(...)A small JavaScript piece that shows or hides the dialog. You will learn JavaScript properly later.
Interactive elements
| Element | Use for |
|---|---|
| details | A collapsible section |
| summary | The label of a details |
| dialog | A modal pop-up box |
| button | A clickable action |
A helpful analogy
details is like a folded brochure: the title is always visible and the inside opens only when you want it. A dialog is like a teller window that appears in front of the queue and blocks everything else until it is closed.
Nesting rules
- 1.summary must be the first child of details.
- 2.Put dialog content in flow order; do not nest a dialog inside a paragraph.
Common mistakes
Putting a summary outside details
Avoid
<summary>FAQ</summary><details>...</details>Use instead
<details><summary>FAQ</summary>...</details>summary only works inside details. Outside it, it behaves like ordinary text.
Using dialog for ordinary page sections
Avoid
<dialog>Main content</dialog>Use instead
<main>Main content</main>A dialog is a focused interruption. Using it for normal content hides the page and confuses users.
Quick tips
- •Add the open attribute to details if you want it open on first load.
- •Press Esc while a dialog is open; the browser closes it for you.