Sub topic 1
What the dialog element is
Prerequisites
This chapter assumes you know basic HTML structure and forms, basic CSS selectors and pseudo-elements, and how to select DOM elements and attach event listeners in JavaScript. You do not need any dialog experience.
What the element does
The <dialog> element represents a window that sits on top of the page. It can hold a confirmation, a form, a message, or any other content that needs the user's attention.
Before <dialog>, developers built these windows from <div> elements, z-index rules, and JavaScript for focus handling. A modal built with the native element needs none of that. The browser provides the following:
- Hiding and showing through a built-in
openattribute. - Focus handling when the dialog opens and closes.
- Closing with the Escape key.
- A layer that sits above all other content, with the rest of the page blocked from interaction.
This chapter covers the element itself and its open attribute. Later chapters cover modal behavior, events, and styling in detail.
The open attribute
A <dialog> is hidden by default. The browser shows it only when it has the boolean attribute open.
<dialog>
<p>I am hidden.</p>
</dialog>
<dialog open>
<p>I am visible.</p>
</dialog>The first dialog does not render. The second renders as a bordered box with its content, positioned in the normal flow of the page, near the top and horizontally centered.
You can read the state from JavaScript. The open property is true when the attribute is present.
const dlg = document.querySelector('dialog');
console.log(dlg.open); // false while the attribute is absentSetting the attribute by hand works for a quick test, but it is not how you normally open a dialog. A dialog opened with open is non-modal. It lacks the modal features listed above. The next chapters introduce the methods that open it properly.
Anatomy of a dialog
The browser gives <dialog> default styles. These are a border, padding, a white background, and centering. You can override all of them.
Inside the element you can place any flow content. A typical dialog has a heading, some text, and a form or a set of buttons. This example is the structure used throughout the tutorial:
<button id="open">Edit profile</button>
<dialog id="dlg" aria-labelledby="dlg-title">
<h2 id="dlg-title">Edit profile</h2>
<form method="dialog">
<label>Name <input name="name" required autofocus></label>
<menu>
<button value="cancel" formnovalidate>Cancel</button>
<button value="save">Save</button>
</menu>
</form>
</dialog>
<script>
const dlg = document.getElementById('dlg');
document.getElementById('open').addEventListener('click', () => dlg.showModal());
dlg.addEventListener('close', () => {
console.log('closed with:', dlg.returnValue);
});
</script>You do not need to understand every line yet. Notice these parts:
- The
<dialog>has anidso scripts can find it, and it has noopenattribute, so it starts hidden. - A button outside the dialog opens it with
showModal(). - A
<form method="dialog">inside it closes the dialog when submitted and records which button was used. - The
aria-labelledbyattribute gives the dialog an accessible name from its heading.
Why not a div
A <div> with role="dialog" can look the same, but you must add every behavior yourself. The table compares the two.
| Behavior | Native `<dialog>` | `<div role="dialog">` |
|---|---|---|
| Hidden until opened | Built in | You write CSS and JavaScript |
| Escape closes it | Built in for modal dialogs | You write a key handler |
| Page behind is blocked | Built in for modal dialogs | You write a focus trap and overlay |
| Stacks above other content | Built in for modal dialogs | You manage `z-index` |
| Role exposed to assistive technology | Built in | You add the `role` attribute |
How the states relate
A dialog is closed until something opens it. It then opens in one of two ways, and a close action returns it to the closed state.
Both open states have the open attribute set. Only the modal state adds the browser-managed behavior.
Browser support
The element is supported in all current major browsers. You can use it without a polyfill in new projects.
Exercise
A dialog is written as <dialog id="d"><p>Hello</p></dialog>. Nothing appears on the page. Why, and what does document.getElementById('d').open return?
Show answer
The dialog has no open attribute, so the browser keeps it hidden. The open property returns false. Adding the attribute shows the dialog as non-modal, and calling showModal() shows it as modal.
Exercise
Name three behaviors you would have to build yourself if you used a <div role="dialog"> instead of a modal <dialog>.
Show answer
Any three of these: hiding and showing it, closing it with Escape, blocking interaction with the page behind it (a focus trap), and placing it above other content with z-index.