HTML <dialog> Tag
HTML <dialog>
The HTML <dialog> tag creates a box that pops up on top of the page, like a message, a question, or a small form.
The browser does most of the work for you. It keeps the dialog on top, can dim the page behind it, closes it when the user presses Esc, and moves keyboard focus into it.
Dialogs are often used for:
- confirm boxes, like "Are you sure you want to delete this?"
- login or sign-up forms
- important messages the user must read
Example
A dialog box. Here, one button opens the dialog and another button inside, closes it:
<button id="openBtn">Open dialog</button>
<dialog id="myDialog">
<p>Hello! I am a dialog.</p>
<button id="closeBtn">Close</button>
</dialog>
<script>
const dialog =
document.getElementById("myDialog");
document.getElementById("openBtn").addEventListener("click", () => {
dialog.showModal();
});
document.getElementById("closeBtn").addEventListener("click", () => {
dialog.close();
});
</script>
Try it Yourself »
Attributes
| Attribute | Value | Description |
|---|---|---|
| open | open | Specifies that the dialog element is open, active and that the user can interact with it (it is recommended to use the .show() or .showModal() method to render dialogs, instead of using the open attribute) |
| closedby | any closerequest none |
Specifies how the dialog can be closed |
The <dialog> tag also supports the Global Attributes in HTML.
Methods and Properties
| Name | Description |
|---|---|
| showModal() | Opens the dialog as a modal, with a backdrop |
| show() | Opens the dialog as non-modal |
| close() | Closes the dialog |
| returnValue | The value the dialog closed with |
Event Attributes
The <dialog> tag also supports the Event Attributes in HTML.
Browser Support
The numbers in the table specify the first browser version that fully supports the element.
| Element | |||||
|---|---|---|---|---|---|
| <dialog> | 37.0 | 79.0 | 98.0 | 15.4 | 24.0 |
Related Pages
HTML Tutorial: HTML Dialog
HTML DOM reference: Dialog Object