# `<dialog>`

> The `<dialog>` HTML element creates modal windows for alerts, notifications or custom forms that require user interaction.

## How do I open a modal dialog in HTML without a library?

The element that creates a dialog box or modal window on a webpage. It can be used for alerts, forms or interactive messages that require user interaction before proceeding.

- Topic: Interactive
- Type: tag
- Browser support: Chrome 37+, Chrome Android 37+, Edge 79+, Firefox 98+, Firefox Android 98+, Safari 15.4+, Safari iOS 15.4+

## Example

Wrong:

```js
const dialog = document.querySelector('#confirm');
openBtn.addEventListener('click', () => {
  dialog.setAttribute('open', '');
});
```

Right:

```js
const dialog = document.querySelector('#confirm');
openBtn.addEventListener('click', () => {
  dialog.showModal();
});
```

`showModal()` puts the dialog in the top layer, makes the page behind inert and closes it on Escape.

## Common mistakes

- Setting `dialog.open = true`, which shows a non-modal dialog without focus handling or Escape.
- Adding a separate overlay and a custom focus trap on top of `showModal()`.

## Key points

- Modal dialogs are opened with `showModal()`, not by adding the `open` attribute.
- The dialog has an accessible name via `aria-labelledby` pointing to its heading.
- A visible close button is inside the dialog, using `close()` or `<form method="dialog">`.
- The backdrop is styled with `::backdrop`, not with an extra overlay `<div>`.

## Interview questions

### What is the difference between show and showModal?

`show()` opens a non-modal dialog, and the page stays interactive. `showModal()` blocks the page behind it and adds a backdrop.

### How do I close a dialog when clicking the backdrop?

Listen for clicks on the `<dialog>` and call `close()` when `event.target` is the dialog itself, not its content.

### How do I get the button the user picked in a dialog?

Use `<form method="dialog">` with buttons that have a `value`. After closing, read `dialog.returnValue`.

## Related cards

- [dialog](https://wonderium.dev/a11y/dialog.md)
- [::backdrop](https://wonderium.dev/css/backdrop.md)
- [\[popover\]](https://wonderium.dev/html/popover.md)
- [\[inert\]](https://wonderium.dev/html/inert.md)
- [\[aria-modal\]](https://wonderium.dev/a11y/aria-modal.md)

---

- Level: [HTML](https://wonderium.dev/html.md)
- HTML version: https://wonderium.dev/html/dialog
- Last updated: 2026-11-25
