details, summary, and JavaScript enhancement
Native disclosure elements provide a built-in way to reveal optional details without requiring a custom widget.
✓ What you will learn in this topic
- Explain the purpose of details, summary, and JavaScript enhancement in a real HTML document.
- Choose and apply the elements shown in both examples.
- Recognize the specific mistake described in this lesson.
Concept explanation
Details creates a disclosure widget, and its first summary child labels the control. Browsers provide its open and close behavior, reducing custom keyboard code. JavaScript can enhance a native interaction, but it should preserve usable markup if scripts fail. Browser APIs such as the DOM let code read and update elements; HTML provides the document and native behavior that code works with.
Syntax
Place a summary first inside details and put the optional content after it.
Example 1: a small page
The summary is the interactive label and the paragraph is revealed when opened.
<details>
<summary>What should I bring?</summary>
<p>Bring gloves, water, and a notebook.</p>
</details>Expected result: A disclosure control expands or collapses the answer using native browser behavior.
Example 2: a practical page
The section remains understandable without JavaScript and the disclosure is labeled by its summary.
<section aria-labelledby=\"hours-title\">
<h2 id=\"hours-title\">Visiting hours</h2>
<details><summary>Holiday schedule</summary><p>The gallery opens at noon on Monday.</p></details>
</section>Expected result: The schedule heading is visible and the holiday information can be expanded.
Common mistakes
Do not treat data attributes as a secure store or assume contenteditable automatically saves edits.
Best practices
Use native disclosure and form controls first; keep data attributes small, non-sensitive, and documented for their script consumer.
Practice exercise
Create an FAQ with details and summary, add a small data attribute for a non-sensitive category, and describe how JavaScript might enhance it without removing the native behavior.
Lesson summary
- Native disclosure elements provide useful behavior before JavaScript adds enhancements.
- Use native disclosure and form controls first; keep data attributes small, non-sensitive, and documented for their script consumer.
Knowledge check
Which element labels a details disclosure?
Show the explanation
The summary element is the visible interactive label and should be the first child of details.
Practical example
html<details>
<summary>What should I bring?</summary>
<p>Bring gloves, water, and a notebook.</p>
</details>
Expected result:
A disclosure control expands or collapses the answer using native browser behavior.
Hands-on practice task
Create an FAQ with details and summary, add a small data attribute for a non-sensitive category, and describe how JavaScript might enhance it without removing the native behavior.
In this section
Other lessons in HTML APIs Overview
Broaden your knowledge