The short answer
#Put your modal content in a <dialog> element and open it with showModal(). The browser places it above everything else, makes the rest of the page inert, closes it on Escape and gives you a ::backdrop to style. Close it with close() or a form with method="dialog".
_14<button type="button" id="open">Edit profile</button>_14_14<dialog id="profile">_14 <h2>Edit profile</h2>_14 <form method="dialog">_14 <button value="cancel">Cancel</button>_14 <button value="save">Save</button>_14 </form>_14</dialog>_14_14<script>_14 const dialog = document.querySelector("#profile");_14 document.querySelector("#open").addEventListener("click", () => dialog.showModal());_14</script>
Why it happens
#Hand-built modals need a lot of code to behave properly: a stacking context that beats every z-index on the page, an overlay, a focus trap, Escape handling, hiding the background from screen readers and returning focus afterwards. Each of those is easy to get slightly wrong.
The <dialog> element moves that work into the browser. When you call showModal(), the dialog is placed in the top layer, which sits above all other content regardless of z-index or overflow. Everything outside the dialog becomes inert, so it cannot be clicked, focused or read by assistive technology. The element is exposed with the dialog role, and the Escape key fires a cancel event and then closes it. The <dialog> element has been supported in all major browsers since 2022.
How to fix it
#Open with showModal, not show or the open attribute
There are two ways to open a dialog, and they are not the same.
showModal()opens it as a modal: top layer, backdrop, inert page, Escape to close.show(), or adding theopenattribute, opens it as a non-modal dialog in the normal flow. No backdrop, no inert page and Escape does nothing by default.
For a modal, always use showModal(). Avoid toggling the open attribute yourself.
Close it and read the result
Call dialog.close() from a button, or put buttons in a form with method="dialog". Submitting that form closes the dialog without a network request and sets dialog.returnValue to the value of the button that was pressed. Listen for the close event to act on it.
_10dialog.addEventListener("close", () => {_10 if (dialog.returnValue === "save") {_10 saveProfile();_10 }_10});
Pressing Escape closes the dialog with an empty return value, so treat anything other than your confirm value as a cancel.
Style the dialog and its backdrop
The ::backdrop pseudo-element is drawn behind a modal dialog and covers the viewport.
_15dialog {_15 border: 0;_15 border-radius: 0.75rem;_15 padding: 1.5rem;_15 max-width: min(32rem, 100% - 2rem);_15}_15_15dialog::backdrop {_15 background: rgb(0 0 0 / 0.5);_15}_15_15/* Stop the page behind scrolling while a modal is open */_15body:has(dialog[open]) {_15 overflow: hidden;_15}
Get focus right
When a modal opens, the browser moves focus into it, to the first focusable element by default. If that is not the best place, for example it would land on a destructive button, put the autofocus attribute on the element that should receive focus. Keep a visible close or cancel button inside the dialog so keyboard and touch users always have a way out.
When the dialog closes, current browsers return focus to the element that was focused before it opened, usually the button that opened it. If you remove that button from the page while the dialog is open, move focus somewhere sensible yourself.
Close on backdrop click with closedby
The closedby attribute controls how a dialog can be dismissed. closedby="any" allows Escape and a click outside the dialog (light dismiss). closedby="closerequest" allows Escape but not outside clicks, which is the default for modals. closedby="none" allows neither, so only your own code can close it.
_10<dialog id="filters" closedby="any">...</dialog>
At the time of writing, closedby is supported in Chrome and Edge 134 and later and Firefox 141 and later, but not yet in a stable Safari release. Check caniuse before relying on it. A small fallback covers the gap: a click on the backdrop is reported with the dialog itself as the target, as long as the dialog has its content in a child element with no padding gap.
_10dialog.addEventListener("click", (event) => {_10 if (event.target === dialog) dialog.close();_10});
Common pitfalls
#- Opening with the
openattribute and wondering why there is no backdrop and Escape does not work. UseshowModal(). - Setting
display: flexondialogwithout scoping it todialog[open]. It overrides the hidden state, so the dialog shows even when closed. - Making the backdrop click handler close the dialog when users click inside padding. Wrap content in an inner element that fills the dialog, or rely on
closedby. - Using
closedby="none"and providing no close button, which traps keyboard users. - Forgetting a heading or
aria-labelledby, so screen readers announce an unnamed dialog.
Related reading
#HTMLquick question
Was this resource helpful?