HTML Dialog
HTML <dialog> Element
The HTML <dialog> element 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
Here, we show a simple dialog example:
Example
A dialog box. Here, one button opens the dialog and another button inside it 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 »
How it Works
- The
<dialog>element holds the content. It stays hidden at first showModal()opens the dialog on top of the pageclose()closes it again
Modal and Non-Modal Dialogs
There are two ways to open a dialog, and they behave differently:
Example
dialog.showModal(); // modal
dialog.show();
// non-modal
- A modal dialog takes over the page. The user must deal with it before doing anything else.
- A non-modal dialog stays open while the user keeps working on the page.
| showModal() | show() | |
|---|---|---|
| Type | Modal | Non-modal |
| Rest of the page | Blocked until the dialog closes | Still usable |
| Backdrop | Yes, can be styled with ::backdrop | No |
| Closes with Esc | Yes | No |
| Good for | Confirm boxes, forms, important messages | Small helper boxes, chat windows |
Closing a Dialog
There are several ways to close a dialog.
With JavaScript
Use the JavaScript close() method:
Example
dialog.close();
With a Form
A form with method="dialog" closes the dialog when it is submitted. No JavaScript is needed:
Example
<dialog
open>
<form method="dialog">
<p>Do you want to save your changes?</p>
<button
value="no">No</button>
<button value="yes">Yes</button>
</form>
</dialog>
With the ESC key
A modal dialog closes when the user presses Esc. This works by itself.
By Clicking Outside It
The closedby="any" attribute lets users close the dialog by clicking anywhere on the backdrop:
Example
<dialog id="myDialog"
closedby="any">
<p>Hello! I am a dialog.</p>
<button
id="closeBtn">Close</button>
</dialog>
Try it Yourself »
Styling a Dialog
You can style a <dialog> element with CSS, just like any other element:
Example
dialog {
border: none;
border-radius: 8px;
padding: 20px;
max-width: 400px;
box-shadow: 0 4px 20px
rgba(0, 0, 0, 0.2);
}
Try it Yourself »
Style the Dialog backdrop
When a dialog is opened with showModal(), a backdrop covers the page behind it. Style it with the
CSS
::backdrop pseudo-element:
Here we style the backdrop with a gradient:
Example
dialog::backdrop {
background-color: #1fc8db; /* for browsers that
do not support gradients */
background-image:
linear-gradient(140deg, #EADEDB 0%, #BC70A4 50%, #BFD641 75%);
opacity:0.75;
}
Try it Yourself »
Open a Dialog Without JavaScript
Newer browsers let you open and close a dialog with HTML only, using the
commandfor and command attributes on the
<button> element:
Example
<button commandfor="myDialog" command="show-modal">Open dialog</button>
<dialog id="myDialog">
<p>Hello! No JavaScript needed.</p>
<button
commandfor="myDialog" command="close">Close</button>
</dialog>
Try it Yourself »
-
commandforis the id of the dialog the button controls -
commandsays what to do: show-modal opens it, and close closes it
Note: These attributes are new. Check browser support, and use the JavaScript methods if you need to support older browsers.
Use <dialog> or Popover?
Both show content on top of the page, but they are made for different jobs.
The popover is explained in the Popover API chapter.
| Modal <dialog> | Popover | |
|---|---|---|
| Blocks the page | Yes | No |
| Closes when you click outside | Only with closedby="any" | Yes, by default |
| Needs JavaScript | Optional | No |
| Good for | Questions the user must answer, forms, warnings | Menus, tooltips, hints, notifications |
A simple rule: If the user must respond before going on, use a modal <dialog>. If the content is just extra help, use a popover.
Summary
<dialog>makes a box that appears on top of the page- Open it with
showModal()orshow() - Close it with
close(), a form with method="dialog", or the Esc key - Style the dimmed area behind it with
::backdrop - Use a modal dialog when the user must respond, and a popover for lighter content
HTML Tag Reference
| Tag | Description |
|---|---|
| <dialog> | Creates a dialog box that pops up on top of the page |
For a complete list of all available HTML tags, visit our HTML Tag Reference.