<dialog> is a native dialog element, Baseline in all major browsers since 2022. dialog.showModal() opens it as a modal: it's rendered in the top layer (above everything, with no z-index battles), the rest of the page becomes inert (not clickable, focusable or exposed to screen readers), focus moves into the dialog, Escape closes it, and a ::backdrop pseudo-element styles the overlay. When it closes, focus returns to the element that opened it. Hand-rolled modals need hundreds of lines and usually still get one of these wrong. dialog.show() opens a non-modal dialog without any of the inertness.
<button id="open">Delete project…</button>
<dialog id="confirm" aria-labelledby="confirm-title">
<form method="dialog">
<h2 id="confirm-title">Delete “Apollo”?</h2>
<p>This permanently deletes 42 files.</p>
<button value="cancel" autofocus>Cancel</button>
<button value="delete">Delete</button>
</form>
</dialog>
const dlg = document.getElementById('confirm');
document.getElementById('open').addEventListener('click', () => dlg.showModal());
dlg.addEventListener('close', () => {
if (dlg.returnValue === 'delete') deleteProject();
});
dialog::backdrop { background: rgb(0 0 0 / .5); backdrop-filter: blur(2px); }
dialog { max-inline-size: min(32rem, 100% - 2rem); border: none; border-radius: 12px; }
html:has(dialog[open]:modal) { overflow: hidden; } /* prevent background scroll */
<form method="dialog"> closes the dialog on submit and sets returnValue to the submit button's value, with no JS needed for the close itself.cancel event fires on Escape. Call preventDefault() to stop it (for example, unsaved changes). Browsers limit repeated cancel prevention to stop abuse.showModal() by default. Newer browsers support a closedby="any" attribute for it; elsewhere you detect clicks whose target is the dialog element itself.autofocus. For destructive confirmations, focus the safe option.showModal() | show() | popover attribute | |
|---|---|---|---|
| Top layer | Yes | No | Yes |
| Rest of page inert | Yes | No | No |
| Escape closes | Yes | No | Yes (auto popovers) |
| Light dismiss | Opt-in (closedby) | No | Yes (auto) |
| Use for | Confirmations, forms that block the task | Inline, non-blocking panels | Menus, tooltips, pickers, toasts |
aria-labelledby pointing at the heading (or aria-label).:has() rule above or overflow: hidden on open.@starting-style, transition-behavior: allow-discrete and transitions on overlay and display. Support for these varies, so make them progressive.open ? ref.current.showModal() : ref.current.close()). Setting the open attribute directly opens it non-modally, which is a common bug.open attribute instead of showModal(), which loses inertness and top-layer behaviour.transform or overflow: hidden, which clips it or breaks position: fixed. The top layer avoids this entirely, which is one reason to switch.display styles on dialog (e.g. display: flex) override the hidden state, so the dialog shows even when closed. Scope them to dialog[open].::backdrop as the free features.form method="dialog" and returnValue.Spotted a mistake or a rights concern? Report an issue