The <dialog> element
The <dialog> tag creates a popup box, such as a confirmation message or a sign-up form. Open it with JavaScript: showModal() shows it as a modal, centered over a dimmed backdrop, with keyboard focus kept inside and the Escape key closing it, while show() opens it without blocking the page. Close it with close().
Last updated: October 3, 2026
Quick facts
| Category | Flow content |
| Content model | Flow content |
| Tag omission | Neither the start nor the end tag can be omitted |
| Permitted parents | Any element that accepts flow content |
| DOM interface | HTMLDialogElement |
Syntax
Live example
Edit the code below and watch the preview update instantly.
Attributes
| Attribute | Description |
|---|---|
| open | Shows the dialog as non-modal. Prefer the show() / showModal() methods instead of setting this yourself |
| closedby | Newer browsers: any lets clicking the backdrop close it, closerequest allows only Escape, none only script |
Methods (JavaScript)
| Attribute | Description |
|---|---|
| showModal() | Opens it as a modal: backdrop, focus trapped inside, Escape closes |
| show() | Opens it without blocking the rest of the page |
| close(value) | Closes it; the optional value becomes dialog.returnValue |
Common examples
A confirmation dialog
Open it with showModal(); a form with method="dialog" closes it and tells you which button was pressed.
Styling the backdrop
The ::backdrop pseudo-element is the dimmed layer behind a modal dialog.
A newsletter sign-up popup
Give the dialog a heading and a clear close button so it’s easy to dismiss.
<dialog> vs a custom <div> modal
A homemade modal built from a <div> needs a lot of JavaScript to behave properly: trap keyboard focus inside, close on Escape, stop people clicking the page behind, and move focus back afterwards. Miss one and keyboard or screen reader users get stuck.
<dialog> opened with showModal() does all of this by default. It sits on the top layer above everything else, makes the rest of the page inert, closes on Escape and returns focus to the button that opened it. Unless you must support very old browsers, it’s the better choice.
| <dialog> | Custom <div> modal | |
|---|---|---|
| Escape key closes | Built in | You code it |
| Background made inert | Built in | You code it |
| Always on top | Yes (top layer) | Depends on z-index |
Common mistakes to avoid
- Using
show()when you need a modal.show()opens it without a backdrop or focus trap; useshowModal()for popups that need an answer. - Adding the
openattribute in HTML for a modal. That shows it as a non-modal dialog; open modals with JavaScript. - No visible way to close it. Always include a close or cancel button — not everyone knows about the Escape key.
- Popups the moment the page loads. Intrusive popups annoy visitors, and Google can rank pages lower on mobile when they cover the content.
Accessibility & SEO tips
- Give every dialog a heading and connect it with
aria-labelledby, so screen readers announce what it’s about. - Put the
autofocusattribute on the element that should get focus first, such as the first field or the safest button. - Avoid covering the main content with popups on mobile right after a visitor arrives from search — it can hurt your rankings.
Browser support
Supported in all modern browsers, including Chrome, Edge, Firefox, Safari and their mobile versions (Safari added it in 2022). Very old browsers display it as a plain box.