Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 3 of 9
Details, Summary & Interactive Elements
Build native accordion/disclosure widgets with <details> and <summary> - no JavaScript required. Also learn <dialog> for modal windows and <template> for reusable markup fragments.
Native Interactive Elements
- <details>/<summary> - Collapsible disclosure widget. Click summary to toggle content. open attribute shows expanded by default
- <dialog> - Native modal dialog. showModal() for modal, show() for non-modal. ::backdrop pseudo-element for overlay styling
- <template> - Holds markup that's not rendered until JavaScript clones it into the DOM. Useful for dynamic content
- <slot> - Used inside Web Components to allow custom content insertion. Part of the Shadow DOM API
- autofocus - Automatically focuses an input when the page loads. Use sparingly - only on the primary action element
Interactive Elements Code
<!-- FAQ accordion (no JavaScript!) -->
<details>
<summary>What is HTML5?</summary>
<p>HTML5 is the latest version of the HyperText Markup Language.
It introduced semantic elements, multimedia support, and new APIs.</p>
</details>
<details>
<summary>Is HTML a programming language?</summary>
<p>No, HTML is a markup language. It describes document structure
but cannot perform logic or calculations like JavaScript or Python.</p>
</details>
<details open>
<summary>What are semantic elements?</summary>
<p>Elements like <article>, <nav>, <main> that describe
the purpose of content, not just its appearance.</p>
</details>
<!-- Dialog modal -->
<dialog id="myDialog">
<h2>Confirm Action</h2>
<p>Are you sure you want to proceed?</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="confirm">Confirm</button>
</form>
</dialog>
<button onclick="document.getElementById('myDialog').showModal()">
Open Dialog
</button>Try It Yourself: FAQ Accordion
Warning
Never nest <details> elements more than 2 levels deep. Deeply nested accordions create a confusing user experience and screen readers struggle to announce the state of each level. If you need more than 2 levels, consider using a different UI pattern like tabs or a sidebar navigation.
The details element provides native expand/collapse functionality with zero JavaScript
Quick Quiz: Interactive Elements
Key Takeaways
- Build native accordion/disclosure widgets with <details> and <summary> - no JavaScript required.
- <details>/<summary> - Collapsible disclosure widget. Click summary to toggle content. open attribute shows expanded by default
- <dialog> - Native modal dialog. showModal() for modal, show() for non-modal. ::backdrop pseudo-element for overlay styling
- <template> - Holds markup that's not rendered until JavaScript clones it into the DOM. Useful for dynamic content
Finished this lesson?