: The Dialog element - HTML | MDN
Skip to search
: The Dialog element
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2022.
* Some parts of this feature may have varying levels of support.
See full compatibility
Report feedback
The

HTML
element represents a modal or non-modal dialog box or other interactive component, such as a dismissible alert, inspector, or subwindow.
Attributes
This element includes the
global attributes
Warning:
The
tabindex
attribute must not be used on the

element. See
Additional notes
closedby
Specifies the types of user actions that can be used to close the

element. This attribute distinguishes three methods by which a dialog might be closed:
light dismiss user action
, in which the

is closed when the user clicks or taps outside it. This is equivalent to the
"light dismiss" behavior of "auto" state popovers
platform-specific user action
, such as pressing the
Esc
key on desktop platforms, or a "back" or "dismiss" gesture on mobile platforms.
A developer-specified mechanism such as a


This dialog was opened using an invoker command.




Non-modal dialogs using popover commands
Non-modal dialogs can be declaratively opened, closed, and toggled using the
Popover API
HTML attributes
popovertarget
and
popovertargetaction
, which can be defined on


This dialog was opened using a popovertargetaction attribute.




The Popover API also provides properties that can be used to get and set the state in JavaScript.
Closing dialogs
It is important to provide a closing mechanism for every

element, and to ensure that this works on devices that might not have a physical keyboard.
There are numerous ways to close a dialog:
Submitting the form within the

element with
method="dialog"
set on the

element (see the
Using the dialog open attribute
example).
Clicking outside the dialog area when "light dismiss" is enabled (see the
Popover API HTML attributes
example).
Pressing the
Esc
key, in dialogs where it is enabled (see the
Popover API HTML attributes
example).
Calling the
HTMLDialogElement.close()
method (see the
modal example
).
CSS Styling

can be selected using its element name (like any other element), and you can also match its state using pseudo-classes such as
:modal
and
:open
The CSS
::backdrop
pseudo-element can be used to style the backdrop of a modal dialog, which is displayed behind the

element when the dialog is displayed using the
HTMLDialogElement.showModal()
method.
This pseudo-element could be used, for example, to blur, darken, or otherwise obfuscate the inert content behind the modal dialog.
Additional notes
HTML

elements can be used to close a dialog box if they have the attribute
method="dialog"
or if the button used to submit the form has
formmethod="dialog"
set. When a

within a

is submitted via the
dialog
method, the dialog box closes, the states of the form controls are saved but not submitted, and the
returnValue
property gets set to the value of the button that was activated.
The
autofocus
attribute should be added to the element the user is expected to interact with immediately upon opening a modal dialog. If no other element involves more immediate interaction, it is recommended to add
autofocus
to the close button inside the dialog, or the dialog itself if the user is expected to click/activate it to dismiss.
Do not add the
tabindex
property to the

element as it is not interactive and does not receive focus. The dialog's contents, including the close button contained in the dialog, can receive focus and be interactive.
When implementing a dialog, it is important to consider the most appropriate place to set user focus. When using
HTMLDialogElement.showModal()
to open a

, focus is set on the first nested focusable element. Explicitly indicating the initial focus placement by using the
autofocus
attribute will help ensure initial focus is set on the element deemed the best initial focus placement for any particular dialog. When in doubt, as it may not always be known where initial focus could be set within a dialog, particularly for instances where a dialog's content is dynamically rendered when invoked, the

element itself may provide the best initial focus placement.
Ensure a mechanism is provided to allow users to close the dialog. The most robust way to ensure that all users can close the dialog is to include an explicit button to do so, such as a confirmation, cancellation, or close button.
By default, a dialog invoked by the
showModal()
method can be dismissed by pressing the
Esc
key. A non-modal dialog does not dismiss via the
Esc
key by default, and depending on what the non-modal dialog represents, it may not be desired for this behavior. Keyboard users expect the
Esc
key to close modal dialogs; ensure that this behavior is implemented and maintained. If multiple modal dialogs are open, pressing the
Esc
key should close only the last shown dialog. When using

, this behavior is provided by the browser.
While dialogs can be created using other elements, the native

element provides usability and accessibility features that must be replicated if you use other elements for a similar purpose. If you're creating a custom dialog implementation, ensure that all expected default behaviors are supported and proper labeling recommendations are followed.
The

element is exposed by browsers in a manner similar to custom dialogs that use the ARIA
role="dialog"
attribute.

elements invoked by the
showModal()
method implicitly have
aria-modal="true"
, whereas

elements invoked by the
show()
method or displayed using the
open
attribute or by changing the default
display
of a

are exposed as
[aria-modal="false"]
. When implementing modal dialogs, everything other than the

and its contents should be rendered inert using the
inert
attribute. When using

along with the
HTMLDialogElement.showModal()
method, this behavior is provided by the browser.
Examples
Invoker Command API HTML attributes
This example demonstrates how you can use open and close a modal dialog using the
commandfor
and
command
HTML attributes of the
Invoker Commands API
First, we declare a


This dialog was opened using an invoker command.




Result
Open the dialog by pressing the "Open dialog" button.
You can close the dialog by selecting the "Close" button or pressing the
Esc
key.
Popover API HTML attributes
This example demonstrates how you can open and close a non-modal dialog using the
popover
popovertarget
, and
popovertargetaction
HTML attributes of the
Popover API
The

is turned into a popover by adding the
popover
attribute.
Since we haven't specified a value for the attribute, the default value of
"auto"
is used.
This enables "light dismiss" behavior, allowing the dialog to be closed by clicking outside the dialog or by pressing
Esc
We could instead have set
popover="manual"
to disable "light dismiss" behavior, in which case the dialog would have to be closed using the "Close" button.
Note that we haven't specified the
popovertargetaction
attribute for the


This dialog was opened using a popovertargetaction attribute.




Result
Open the dialog by pressing the "Open dialog" button.
You can close the dialog by selecting the "Close" button or pressing the
Esc
key.
You can also close it by selecting outside the dialog, as it is non-modal.
Using the dialog
open
attribute
This example demonstrates how you can set the boolean
open
attribute on a

element, in order to create an HTML-only non-modal dialog that is already open when the page loads.
The dialog can be closed by clicking the "OK" button because the
method
attribute in the

element is set to
"dialog"
In this case, no JavaScript is needed to close the form.
html

Greetings, one and all!






Result
This dialog is initially open and non-modal because of the presence of the
open
attribute.
After clicking "OK", the dialog gets dismissed, leaving the Result frame empty.
Note:
Reload the page to reset the output.
When the dialog is dismissed, there is no method provided to reopen it. The preferred method to display non-modal dialogs is to use the
HTMLDialogElement.show()
method.
It is possible to toggle the display of the dialog by adding or removing the boolean
open
attribute, but this is not the recommended practice.
Creating a modal dialog
This example demonstrates a modal dialog with a
gradient
backdrop. The
.showModal()
method opens the modal dialog when the "Show the dialog" button is activated. The dialog can be closed by pressing the
Esc
key or via the
close()
method when the "Close" button within the dialog is activated.
When a dialog opens, the browser, by default, gives focus to the first element that can be focused within the dialog. In this example, the
autofocus
attribute is applied to the "Close" button, giving it focus when the dialog opens, as this is the element we expect the user will interact with immediately after the dialog opens.
HTML
html


This modal dialog has a groovy backdrop!




CSS
We can style the backdrop of the dialog by using the
::backdrop
pseudo-element.
css
::backdrop {
background-image: linear-gradient(
45deg,
magenta,
rebeccapurple,
dodgerblue,
green
);
opacity: 0.75;
JavaScript
The dialog is opened modally using the
.showModal()
method and closed using the
.close()
or
.requestClose()
methods.
js
const dialog = document.querySelector("dialog");
const showButton = document.querySelector("dialog + button");
const closeButton = document.querySelector("dialog button");

// "Show the dialog" button opens the dialog modally
showButton.addEventListener("click", () => {
dialog.showModal();
});

// "Close" button closes the dialog
closeButton.addEventListener("click", () => {
dialog.close();
});
Result
When the modal dialog is displayed, it appears above any other dialogs that might be present. Everything outside the modal dialog is inert and interactions outside the dialog are blocked. Notice that when the dialog is open, with the exception of the dialog itself, interaction with the document is not possible; the "Show the dialog" button is mostly obfuscated by the almost opaque backdrop of the dialog and is inert.
Handling the return value from the dialog
This example demonstrates the
returnValue
of the


element and how to close a modal dialog by using a form. By default, the
returnValue
is the empty string or the value of the button that submits the form within the

element, if there is one.
This example opens a modal dialog when the "Show the dialog" button is activated. The dialog contains a form with a


















JavaScript
The dialog is opened using using an event listener on the "Show the dialog" button, which calls
HTMLDialogElement.showModal()
when the button is clicked.
The dialog is closed when the "Cancel" button is clicked, because the



[type="submit"] {
margin-right: 1rem;
JavaScript
js
const showBtn = document.getElementById("show-dialog");
const dialog = document.getElementById("dialog");
const jsCloseBtn = dialog.querySelector("#js-close");

showBtn.addEventListener("click", () => {
dialog.showModal();
});

jsCloseBtn.addEventListener("click", (e) => {
e.preventDefault();
dialog.close();
});
Result
From the output, we see it is impossible to close the dialog using the
Normal close
button. But the dialog can be closed if we bypass the form validation using the
formnovalidate
attribute on the
Cancel
button. Programmatically,
dialog.close()
will also close such dialog.
Comparison of different closedby behaviors
This example demonstrates the difference in behavior between different values of the
closedby
attribute.
HTML
We provide three