HTML includes a handful of interactive and status elements that used to need scripts. They come with keyboard support and accessibility built in, which makes them better than most hand-made versions.
details and summary#
<details> creates a disclosure widget: a heading that expands to reveal more content. <summary> is the always-visible label and must be the first child.
<details>
<summary>Do you have vegan options?</summary>
<p>Yes. We offer oat and soy milk, a vegan brownie and a chickpea wrap.</p>
</details>
<details open>
<summary>Opening hours</summary>
<p>Every day, 7am to 7pm.</p>
</details>The open attribute starts it expanded. Browsers provide the toggle arrow, keyboard operation with Enter and Space, and screen reader announcements. This is ideal for FAQs. Giving several <details> elements the same name attribute makes them behave as an accordion, where opening one closes the others, in browsers that support it.
dialog#
<dialog> represents a popup window. Opened as a modal with a small script, it blocks the rest of the page, traps focus and closes with the Escape key.
<dialog id="order-dialog">
<h2>Confirm your order</h2>
<p>Two lattes and one brownie, Rs. 640.</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="confirm">Place order</button>
</form>
</dialog>
<button type="button" id="open-order">Review order</button>
<script>
const dialog = document.getElementById("order-dialog");
document.getElementById("open-order").addEventListener("click", () => dialog.showModal());
</script>showModal()opens it as a modal, andshow()opens a non-modal one.- A form with
method="dialog"closes the dialog when submitted, and the button'svaluebecomes the dialog'sreturnValue. - Give the dialog a heading so it has an accessible name, and make sure focus lands on a sensible control.
- The
::backdroppseudo-element in CSS styles the dimmed area behind a modal.
progress#
<progress> shows how far a task has advanced.
<label for="upload">Uploading menu photos</label>
<progress id="upload" value="60" max="100">60%</progress>Omit value for an indeterminate state, meaning "working, no idea how long". The text inside is a fallback.
meter#
<meter> displays a scalar measurement within a known range, such as disk usage or a rating, not progress.
<label for="stock">Oat milk in stock</label>
<meter id="stock" min="0" max="50" low="10" high="40" optimum="45" value="8">8 of 50 litres</meter>low, high and optimum let the browser colour the bar: good, caution or bad. In this example 8 litres is below low, so it warns about running out.
| Use | Element |
|---|---|
| A task moving from 0 to complete | progress |
| A reading within a known scale | meter |
Common mistakes#
- Building a custom accordion with divs and scripts when
detailsexists. - Opening a dialog by toggling the
openattribute. - Using
progressfor a rating, ormeterfor task progress. - Forgetting labels for progress and meter.
- Hiding essential content inside
detailsthat people may never open.
Try it yourself#
Create a small FAQ with three details elements. Add a dialog that confirms a "Clear cart" action with Cancel and Confirm buttons, opened by a button. Add a progress bar set to 40 percent, and a meter showing a rating of 4 out of 5. Press Escape on the dialog to confirm it closes.
Recap#
details and summary make accessible expandable sections. dialog provides modal popups opened with showModal(). progress shows task completion, and meter shows a measurement within a range.
Check your understanding
Lesson quiz
OptionalA short quiz to check what you just learned.
- Questions
- 4
- Time limit
- 5 min
- Pass mark
- 70%
- Best score
- -