Full answer
The dialog element is the built-in way to build modals. Calling dialog.showModal() opens it as a modal, and the browser takes care of work that custom modals usually get wrong.
- Top layer: a modal dialog is rendered in the browser's top layer, above all other content, regardless of
z-indexor the stacking contexts of its ancestors. You do not need to move it to the end ofbody. - Inert background: the rest of the page cannot be focused or clicked while the modal is open.
- Focus: focus moves into the dialog on open (to the first focusable element or one with
autofocus) and returns to the element that opened it when the dialog closes. - Keyboard: the Escape key closes it and fires a
cancelevent first, which you can intercept. ::backdrop: a pseudo-element you style for the dimmed layer behind the dialog.- Semantics: it exposes the dialog role. Give it a name with
aria-labelledby. method="dialog"on a form inside it closes the dialog when submitted, and puts the clicked button's value intodialog.returnValue.
dialog.show() opens a non-modal dialog, which does not block the page. Use showModal() for a true modal.
After the click, the dialog is open and matches :modal. Focus moves inside it, and the dialog is on top even though the opener has z-index: 99999. Clicking Delete closes the dialog, sets returnValue to confirm, and focus returns to the opener.
Things the browser does not do for you: the page behind the dialog can still scroll in some setups (set overflow: hidden on the body while open if you need to prevent it), and you must write your own close button text, labels and animations. Animating open and close needs extra care, because the dialog is removed from rendering when closed.
<style>
dialog { border: 0; border-radius: 8px; padding: 1rem; max-width: 20rem; }
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
.high { position: relative; z-index: 99999; }
</style>
<button id="open" class="high" type="button">Delete course</button>
<dialog id="dlg" aria-labelledby="dlg-title">
<h2 id="dlg-title">Delete this course?</h2>
<p>This cannot be undone.</p>
<form method="dialog">
<button value="cancel" id="cancel">Cancel</button>
<button value="confirm" id="confirm">Delete</button>
</form>
</dialog>
<script>
const dlg = document.getElementById("dlg");
document.getElementById("open").addEventListener("click", () => dlg.showModal());
dlg.addEventListener("close", () => { document.title = "closed:" + dlg.returnValue; });
</script>Checked by clicking the button, reading elementFromPoint at the centre of the viewport, closing with the Delete button, and closing with the Escape key. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- showModal() puts the dialog in the top layer regardless of z-index.
- The background becomes inert while the modal is open.
- Focus moves in on open and returns to the opener on close.
- Escape fires a cancel event and closes it; ::backdrop styles the overlay.
- Give the dialog an accessible name and a visible close button.
Common mistakes
- using show() and expecting a modal. show() leaves the page interactive and gives no backdrop or focus handling.
- building a modal from div elements without focus trapping or an inert background, so keyboard users can tab behind it.
Follow-ups
- how do you close the dialog when the user clicks the backdrop?
- How do you close the dialog when the backdrop is clicked?
- What does a form with method="dialog" do?
Tips
- Always give the dialog an accessible name and a visible way to close it. Do not rely on Escape alone, since touch users have no Escape key.
- List what you would otherwise have to write yourself: stacking, focus trap, Escape, restore focus.
- Mention that Escape alone is not enough for touch users.