The dialog element is the native HTML element, written as <dialog>, that represents a dialog box or other interactive overlay such as a confirmation prompt, an alert, a settings panel or a short form. It can be shown either as a non-modal dialog, which leaves the rest of the page usable, or as a modal dialog, which renders in the top layer, makes everything behind it inert and closes with the Escape key.
Before the element gained full browser support in 2022, developers built modals from generic div containers plus JavaScript for focus trapping, scroll locking and stacking. The dialog element moves that behavior into the browser, which makes accessible modals far easier to build correctly with less custom code.
How the dialog element works in the browser
The dialog element is hidden by default and becomes visible only when it is opened through its JavaScript API or a declarative trigger. The method used to open it decides whether the dialog is modal, and that choice changes how the browser treats the focus, rendering order and interactivity of the surrounding document.
Calling showModal() places the dialog in the top layer, a browser-managed layer that sits above every element regardless of z-index or the parent’s stacking context. The browser also creates a ::backdrop pseudo-element behind it, marks the rest of the document as inert so that clicks and keyboard focus cannot reach it, and moves focus into the dialog. Calling show() instead opens the dialog in normal document flow without any of those effects.
The following table summarizes the main members of the dialog API and what each one does.
| Member | Type | Effect |
|---|---|---|
| showModal() | Method | Opens as modal in the top layer with backdrop and inert page |
| show() | Method | Opens as non-modal in normal document flow |
| close(value) | Method | Closes the dialog and optionally sets returnValue |
| open | Attribute | Reflects whether the dialog is currently shown |
| returnValue | Property | Holds the string passed to close or the submitting button value |
| close | Event | Fires after the dialog has closed by any route |
| cancel | Event | Fires on Escape or light dismiss and can be canceled |
Modal and non-modal dialog behavior
Modal and non-modal dialogs share one element but behave very differently, so the opening method is a design decision, not a detail. A modal dialog demands a response before the user continues, while a non-modal dialog behaves like a floating panel that coexists with the page.
The comparison below shows how the two modes differ in the areas that matter most for usability and accessibility.
| Aspect | Modal via showModal() | Non-modal via show() |
|---|---|---|
| Rendering layer | Top layer above all content | Normal flow, subject to z-index |
| Rest of page | Inert, cannot be clicked or focused | Fully interactive |
| Backdrop | ::backdrop pseudo-element available | No backdrop |
| Escape key | Closes the dialog by default | No built-in Escape handling |
| Accessibility role | Exposed as modal dialog | Exposed as dialog |
| Typical use | Confirmations, critical forms | Toolbars, chat panels, inspectors |
The open attribute can also be added directly in markup, but doing so produces a non-modal dialog without focus management. Toggling that attribute from script is a common mistake because it bypasses the browser logic that showModal() provides.
Forms, return values and closing mechanisms
The dialog element integrates with HTML form handling through the method="dialog" value. A form with this method does not send a request to a server; instead, submitting it closes the dialog and stores the value of the submitting button in returnValue, which script can read inside the close event handler.
Browsers now support several ways of closing a dialog, and each route has slightly different event behavior. The table below lists them with their support status as of 2026.
| Closing route | Mechanism | Notes |
|---|---|---|
| Form with method dialog | Form submission | Sets returnValue from the button value |
| close() method | Script call | Optional string argument becomes returnValue |
| Escape key | Cancel request | Modal only; fires cancel, then close |
| closedby attribute | Declarative light dismiss | Values any, closerequest, none; newer support |
| Invoker commands | command and commandfor attributes | Buttons open or close without script; newer support |
Because the newer declarative features arrived at different times in different engines, production code should use feature detection or keep a small script fallback for opening and closing.
Styling and accessibility considerations
The dialog element ships with modest default styles, typically a border, padding and centered placement, and authors restyle it with ordinary CSS. The ::backdrop pseudo-element is styled separately to dim or blur the page behind a modal dialog, and the :modal pseudo-class targets a dialog only while it is open modally.
Entry and exit animations are now possible with @starting-style and transitions on display and overlay using transition-behavior: allow-discrete. Without these, a dialog appears and disappears instantly because the closed state uses display: none.
For accessibility, every dialog needs an accessible name, usually provided through aria-labelledby pointing to its heading. Authors should decide where initial focus lands, either on the first interactive control or on a specific element marked with the autofocus attribute, and the browser returns focus to the previously focused element when a modal dialog closes.
- Give each dialog a visible heading and connect it with aria-labelledby.
- Use
role="alertdialog"only for urgent messages that require an immediate decision. - Keep a visible close button even when Escape and light dismiss are available.
- Avoid nesting interactive content that scrolls the body behind a modal.
Common mistakes and how to avoid them
Most dialog problems come from treating the element as a styled container instead of a behavioral primitive. The table below links frequent symptoms to their cause and a reliable fix.
| Symptom | Cause | Fix |
|---|---|---|
| Page behind remains clickable | Opened with show() or the open attribute | Open with showModal() |
| Dialog hidden behind a header | Non-modal dialog inside a stacking context | Use showModal() so it enters the top layer |
| Page scrolls under the modal | Body scroll is not locked by default | Apply overflow hidden to html while open |
| No exit animation | Closed state switches to display none | Use allow-discrete transitions and @starting-style |
| Screen reader announces no name | Missing accessible name | Add aria-labelledby to the heading |
A related misconception is that the dialog element replaces every overlay. For menus, tooltips and other lightweight floating UI, the newer popover attribute is often a better fit, because it offers light dismiss and top-layer rendering without modality.
Stripped to the essentials
A dialog element is a ready-made pop-up box. The browser already knows how to handle it. A website might ask whether to delete a file. The box then appears in front of the page. The page behind it stays temporarily frozen until the visitor answers.
Before this element existed, every website built that behavior by hand. Many got it wrong, especially for keyboard and screen reader users. The element gives developers a correct pop-up out of the box. It is like a door that already comes with hinges and a lock.
Where it is applied
A team maintaining an online invoicing application replaces its custom modal library with the native dialog element. The delete confirmation becomes a <dialog> containing a short message and a form with method="dialog", offering two buttons with the values cancel and delete. Script calls showModal() when the user selects the delete action and reads returnValue in the close handler to decide whether to send the deletion request.
The change removes about 9 KB of minified JavaScript that previously handled focus trapping, Escape handling and z-index conflicts. Keyboard testing shows that focus now stays inside the dialog and returns to the delete button after closing, and an accessibility audit that had flagged the old modal for unreachable background focus now passes without findings for that component.