---
title: "How do I build a modal with the HTML dialog element?"
description: "The native dialog element gives you a modal with a backdrop, Escape to close and focus handling built in. Learn showModal, close, ::backdrop and closedby."
url: https://www.danny.engineering/answer/html/how-do-i-build-a-modal-with-the-html-dialog-element
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["HTML"]
---

# How do I build a modal with the HTML dialog element?

## 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"`.


```html
<button type="button" id="open">Edit profile</button>

<dialog id="profile">
  <h2>Edit profile</h2>
  <form method="dialog">
    <button value="cancel">Cancel</button>
    <button value="save">Save</button>
  </form>
</dialog>

<script>
  const dialog = document.querySelector("#profile");
  document.querySelector("#open").addEventListener("click", () => dialog.showModal());
</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 the `open` attribute, 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.


```javascript
dialog.addEventListener("close", () => {
  if (dialog.returnValue === "save") {
    saveProfile();
  }
});
```


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.


```css
dialog {
  border: 0;
  border-radius: 0.75rem;
  padding: 1.5rem;
  max-width: min(32rem, 100% - 2rem);
}

dialog::backdrop {
  background: rgb(0 0 0 / 0.5);
}

/* Stop the page behind scrolling while a modal is open */
body:has(dialog[open]) {
  overflow: hidden;
}
```


### 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.


```html
<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](https://caniuse.com/mdn-html_elements_dialog_closedby) 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.


```javascript
dialog.addEventListener("click", (event) => {
  if (event.target === dialog) dialog.close();
});
```


## Common pitfalls

- Opening with the `open` attribute and wondering why there is no backdrop and Escape does not work. Use `showModal()`.
- Setting `display: flex` on `dialog` without scoping it to `dialog[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

- [MDN: The dialog element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog)
- [MDN: HTMLDialogElement closedBy](https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement/closedBy)
