Two pieces of interface appear on almost every website: sections that expand and collapse (FAQs, "show more" panels) and pop-up boxes ("Are you sure you want to delete?", login windows). For years, developers built both from <div> elements and a lot of JavaScript, and they often got keyboard and screen reader support wrong.
Modern HTML has built-in elements for both: <details> with <summary> for collapsible content and <dialog> for pop-ups. The browser handles the clicking, the keyboard and the accessibility for you. In this chapter we'll learn both, and finish with two newer helpers: popovers and invoker commands.
<details> and <summary>
<details>creates a disclosure widget: content that is hidden until the user opens it. Its first child,<summary>, is the always-visible label that the user clicks to toggle it.
<details>
<summary>What is the time complexity of binary search?</summary>
<p>O(log n), because each step halves the search range.</p>
</details>Output (closed, as the page first loads):
▶ What is the time complexity of binary search?After clicking the summary (or focusing it with Tab and pressing Enter or Space):
▼ What is the time complexity of binary search?
O(log n), because each step halves the search range.No JavaScript was needed. The browser draws the triangle marker, makes the summary keyboard-focusable, and tells screen readers whether the section is expanded or collapsed.
Rules for <summary>
<summary>must be the first child of<details>, and there should be only one.Everything after the summary is the hidden content, and it can be any normal content: paragraphs, lists, images, even another
<details>.If you leave out
<summary>, the browser shows its own default label (usually "Details"). Always write your own.Don't put links or buttons inside
<summary>; clicking them would fight with the toggling.
The open Attribute
To show a section expanded from the start, add the boolean attribute open:
<details open>
<summary>Syllabus</summary>
<p>Unit 1: Arrays and Linked Lists ...</p>
</details>Like every boolean attribute (we met them in the Elements & Attributes chapter), its presence is what matters, not its value. <details open="false"> is still open! To close it, remove the attribute. The browser also adds and removes open by itself as the user clicks, so in CSS you can style the open state with details[open].
Exclusive Accordions with the name Attribute
An accordion is a group of collapsible sections where opening one closes the others. Give several <details> elements the same name, and the browser enforces "only one open at a time", just like radio buttons with the same name:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GATE FAQ</title>
</head>
<body>
<h1>GATE CSE: Frequently Asked Questions</h1>
<details name="faq" open>
<summary>How many questions are there?</summary>
<p>65 questions carrying a total of 100 marks.</p>
</details>
<details name="faq">
<summary>Is there negative marking?</summary>
<p>Yes, for MCQs: one third of the marks for a wrong answer.
MSQ and NAT questions have no negative marking.</p>
</details>
<details name="faq">
<summary>How long is the exam?</summary>
<p>3 hours.</p>
</details>
</body>
</html>Output:
GATE CSE: Frequently Asked Questions
▼ How many questions are there?
65 questions carrying a total of 100 marks.
▶ Is there negative marking?
▶ How long is the exam?Click "Is there negative marking?" and it opens while the first question closes automatically. The name attribute has worked in all major browsers since late 2024 (Chrome 120, Safari 17.2, Firefox 130). In an older browser, the sections still work; they simply don't close each other, so it is safe to use today.
Reacting with JavaScript: the toggle Event
When a <details> opens or closes, it fires a toggle event, and its open property tells you the new state:
const faq = document.querySelector("details");
faq.addEventListener("toggle", () => {
console.log(faq.open ? "Opened" : "Closed");
});Output (after clicking the summary twice):
Opened
ClosedYou can also open or close it from code: faq.open = true;.
Styling the Marker
A <summary> is displayed as a list item, so its triangle is a list marker. You can change or remove it with CSS:
summary {
cursor: pointer;
}
summary::marker {
content: "+ ";
}
details[open] > summary::marker {
content: "− ";
}(Older Safari versions use a non-standard ::-webkit-details-marker instead of ::marker for this triangle, so test your styling there.)
The <dialog> Element
<dialog>represents a dialog box, such as a confirmation, alert or small form, that appears over the page. It can be opened as modal (blocking the rest of the page) or non-modal.
Modal dialog | Non-modal dialog | |
|---|---|---|
Opened with |
|
|
Rest of the page | Becomes inert: can't be clicked, focused or read until the dialog closes | Stays usable |
Position | Centred in the viewport, above everything else (the "top layer") | Placed where it is in the page flow |
Backdrop | Yes, styleable with | No |
Esc key closes it | Yes | No |
Example use | "Delete this file?" confirmation, login box | A chat widget, an "inspector" panel |
A dialog is hidden until it is opened. Both kinds are closed with dialog.close().
Worked Example: A Delete Confirmation
Here is a complete page. Clicking "Delete note" opens a modal dialog that asks for confirmation, and the page shows which button the user chose.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Delete Note</title>
<style>
dialog::backdrop { background: rgb(0 0 0 / 0.5); }
</style>
</head>
<body>
<h1>My Notes</h1>
<p>Operating Systems: Deadlocks</p>
<button type="button" id="delete-btn">Delete note</button>
<p id="result">No action yet.</p>
<dialog id="confirm-dialog" aria-labelledby="dialog-title">
<h2 id="dialog-title">Delete this note?</h2>
<p>This cannot be undone.</p>
<form method="dialog">
<button type="submit" value="cancel" autofocus>Cancel</button>
<button type="submit" value="delete">Delete</button>
</form>
</dialog>
<script>
const dialog = document.getElementById("confirm-dialog");
const result = document.getElementById("result");
document.getElementById("delete-btn").addEventListener("click", () => {
dialog.returnValue = ""; // clear the answer from last time
dialog.showModal();
});
dialog.addEventListener("close", () => {
result.textContent = "You chose: " + (dialog.returnValue || "(Esc pressed)");
});
</script>
</body>
</html>Output when "Delete note" is clicked:
The whole page is dimmed by a semi-transparent black backdrop.
In the centre of the window, a white box with a border shows:
Delete this note?
This cannot be undone.
[Cancel] [Delete]
The Cancel button already has keyboard focus.After clicking a button, the dialog closes and the paragraph changes:
You chose: deleteLet's unpack what happened:
showModal()opens the dialog in the top layer, dims the page with the backdrop and makes everything behind it inert.Focus moves into the dialog. Because of
autofocus, the safe "Cancel" button gets focus. Without it, the browser focuses the first focusable element inside the dialog.<form method="dialog">is special: submitting it does not send anything to a server. It just closes the dialog and setsdialog.returnValueto thevalueof the button that submitted it ("cancel"or"delete").The dialog fires a
closeevent, where we readreturnValue.If the user presses Esc, the dialog fires a
cancelevent and then closes. No button was used, soreturnValueis not changed. That's why the click handler resets it to""before opening:returnValuekeeps its old value between openings, so without the reset an Esc press would wrongly report the previous answer. Here it stays empty and our code prints "You chose: (Esc pressed)".When the dialog closes, focus returns to the "Delete note" button that opened it, so keyboard users don't lose their place.
Imagine writing all of that (focus trapping, Esc handling, focus return, inert background, backdrop) by hand with <div>s. That's why <dialog> is the right tool.
Closing a Dialog: All the Ways
Way | Sets returnValue? |
|---|---|
| Only if you pass a value: |
A submit button inside | Yes, to the button's |
Pressing Esc (modal only) | No |
| Optional; it fires |
You can also prevent Esc from closing a dialog (for example, when the user has unsaved changes) by calling event.preventDefault() in a cancel listener, but use this sparingly: users expect Esc to work.
The open Attribute on <dialog>
<dialog open> displays the dialog without any JavaScript, but always as non-modal. Don't use it to make a modal, and don't toggle it by hand; use show(), showModal() and close() so the browser can manage focus and state correctly.
Light Dismiss with closedby (Newer)
The newer closedby attribute controls how a dialog may be closed: closedby="any" also allows "light dismiss" (clicking the backdrop outside the dialog), "closerequest" allows Esc but not outside clicks (the default for modals), and "none" allows only your own buttons and code. As of late 2026, Chrome, Edge and Firefox support it but Safari does not yet, so treat it as an extra rather than something your page depends on.
Opening Dialogs Without JavaScript: Invoker Commands
Since December 2025, all major browsers support invoker commands: two button attributes that let a button control another element directly.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Invoker Commands</title>
</head>
<body>
<button type="button" command="show-modal" commandfor="help">Open help</button>
<dialog id="help">
<p>Press Esc or use the button below to close this dialog.</p>
<button type="button" command="close" commandfor="help">Close</button>
</dialog>
</body>
</html>commandforholds theidof the element to control.commandsays what to do. For dialogs the main built-in commands areshow-modalandclose; popovers haveshow-popover,hide-popoverandtoggle-popover.
Clicking "Open help" calls showModal() on the dialog, and "Close" closes it, all with zero lines of script. Because this feature is new, people still using older browser versions will see buttons that do nothing; if your audience may have older phones, keep a small JavaScript fallback like the one in the previous example.
A Quick Look at Popovers
A popover is lighter than a dialog: menus, tooltips-with-content, "toast" notifications. Any element becomes one with the popover attribute, and a button opens it with popovertarget:
<button type="button" popovertarget="menu">Menu</button>
<div id="menu" popover>
<a href="/notes">Notes</a>
<a href="/quiz">Quiz</a>
</div>A popover appears in the top layer, closes when you click outside it or press Esc ("light dismiss"), and doesn't make the page inert. The Popover API has been supported in all major browsers since January 2025.
Need | Use |
|---|---|
Show/hide a section of content in place |
|
Several sections, only one open at a time |
|
Ask the user something and block the page until they answer |
|
A panel that floats but lets the user keep working |
|
A menu or small panel that disappears on an outside click |
|
Common Mistakes
Writing
open="false"on<details>or<dialog>and expecting it to be closed. Boolean attributes are on when present; remove the attribute instead.Putting
<summary>anywhere but first inside<details>.Using
<dialog open>for a modal. It is never modal that way. CallshowModal().Forgetting a visible close button. Mouse and touch users may not know about Esc. Every dialog needs an obvious way out.
Putting
tabindexon the<dialog>itself. The dialog isn't interactive; put focus on a control inside it (withautofocusif needed).Expecting
method="dialog"to send data to the server. It only closes the dialog. Use a normal form method (see the Forms chapter) to submit data.Building custom modals from
<div>s and forgetting focus management, Esc handling and making the background inert.
Interview Questions
What is the difference between show() and showModal()?
showModal() opens the dialog as modal: it goes into the top layer, gets a ::backdrop, makes the rest of the page inert and can be closed with Esc. show() opens it as non-modal: the rest of the page stays interactive, there is no backdrop, and Esc doesn't close it.
What does <form method="dialog"> do?
Submitting it closes the enclosing dialog instead of sending a request to a server. The dialog's returnValue is set to the value of the submit button that was used, and a close event fires.
How can you build an accordion where only one section is open, without JavaScript?
Use several <details> elements with the same name attribute. The browser closes the others when one opens.
Is <details open="false"> open or closed? Why?
Open. open is a boolean attribute, so any value, even "false", means it is present and true. To close it, remove the attribute (or set element.open = false in JavaScript).
Why is <dialog> better than a custom <div> modal?
The browser provides the behaviour for free: it moves focus into the dialog and returns it afterwards, makes the background inert, closes on Esc, renders it above everything in the top layer with a backdrop, and exposes the correct role to assistive technology.
Conclusion
<details>+<summary>give you a keyboard-accessible, collapsible section with no JavaScript.opencontrols the initial state, and a sharednamemakes an exclusive accordion.<dialog>is the built-in pop-up box. Open it withshowModal()(modal) orshow()(non-modal) and close it withclose(), Esc, or a<form method="dialog">.returnValuetells you which button closed the dialog;::backdropstyles the dimmed background.Newer helpers:
command/commandforopen dialogs from buttons without script,popovermakes light-dismiss menus, andclosedbycontrols light dismiss (not yet in Safari).
The browser gives these elements good accessibility automatically. In the next chapter, Accessibility Basics, we'll learn what accessibility actually means and how to make every part of a page usable by everyone.