Dialog element

On this page 7

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.

MemberTypeEffect
showModal()MethodOpens as modal in the top layer with backdrop and inert page
show()MethodOpens as non-modal in normal document flow
close(value)MethodCloses the dialog and optionally sets returnValue
openAttributeReflects whether the dialog is currently shown
returnValuePropertyHolds the string passed to close or the submitting button value
closeEventFires after the dialog has closed by any route
cancelEventFires on Escape or light dismiss and can be canceled
Core dialog API members

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.

AspectModal via showModal()Non-modal via show()
Rendering layerTop layer above all contentNormal flow, subject to z-index
Rest of pageInert, cannot be clicked or focusedFully interactive
Backdrop::backdrop pseudo-element availableNo backdrop
Escape keyCloses the dialog by defaultNo built-in Escape handling
Accessibility roleExposed as modal dialogExposed as dialog
Typical useConfirmations, critical formsToolbars, chat panels, inspectors
Differences between the two opening modes

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 routeMechanismNotes
Form with method dialogForm submissionSets returnValue from the button value
close() methodScript callOptional string argument becomes returnValue
Escape keyCancel requestModal only; fires cancel, then close
closedby attributeDeclarative light dismissValues any, closerequest, none; newer support
Invoker commandscommand and commandfor attributesButtons open or close without script; newer support
Ways to close a dialog

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.

SymptomCauseFix
Page behind remains clickableOpened with show() or the open attributeOpen with showModal()
Dialog hidden behind a headerNon-modal dialog inside a stacking contextUse showModal() so it enters the top layer
Page scrolls under the modalBody scroll is not locked by defaultApply overflow hidden to html while open
No exit animationClosed state switches to display noneUse allow-discrete transitions and @starting-style
Screen reader announces no nameMissing accessible nameAdd aria-labelledby to the heading
Typical dialog problems

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.