A modal dialog with the dialog element
Open a native dialog as a modal, name it, close it three ways and send focus back.
What you will be able to do
- Explain what the dialog element does that a div does not
- Open a dialog as a modal with showModal() and style its backdrop
- Name the dialog and choose the control that gets focus first
- Close it with Escape, a button and a form with method dialog
- Open and close the same dialog from markup with XON
Why the native element
A modal dialog asks for an answer before the visitor can go on. Building one from a div means writing three things yourself: a layer above the page, a way to keep keyboard focus inside it, and a way to stop the page behind it from reacting. The dialog element does all three once you open it with the right method.
When you open a dialog as a modal, the browser puts it in the top layer, which sits above everything else on the page. It also makes the rest of the document inert, so Tab no longer reaches the page behind it and clicks on that page do nothing. One limit matters in this tutorial: the effect stops at the edge of the document that holds the dialog. In a preview frame, the page around the frame stays active.
showModal() and the backdrop
A dialog is hidden until you open it. Call showModal() to open it as a modal. The method show() opens it without the modal behaviour, which is a different tool. A dialog that you open by setting the open attribute is also not modal, so use the method.
The browser draws a layer behind a modal dialog. Style it with the ::backdrop pseudo-element, for example to dim the page.
opener.addEventListener('click', function () {
dialog.showModal();
});
HTML
<div class="dlg-native">
<button type="button" class="dlg-native__open" id="dlg-native-open">Show details</button>
<dialog class="dlg-native__dialog" id="dlg-native-dialog" aria-labelledby="dlg-native-title">
<h2 class="dlg-native__title" id="dlg-native-title">Shipping details</h2>
<p>This is an example dialog. Press Escape, use the Close button, or leave through the form below.</p>
<form method="dialog" class="dlg-native__actions">
<button class="dlg-native__close" id="dlg-native-close" autofocus>Close</button>
</form>
</dialog>
</div>CSS
.dlg-native {
--dn-bg: #ffffff;
--dn-fg: #1b1f2a;
--dn-line: #c9ced9;
--dn-accent: #3730a3;
--dn-on-accent: #ffffff;
color: var(--dn-fg);
}
.dlg-native__open,
.dlg-native__close {
min-height: 44px;
padding: 0.5rem 1.25rem;
font: inherit;
color: var(--dn-on-accent);
background: var(--dn-accent);
border: 1px solid var(--dn-accent);
border-radius: 8px;
cursor: pointer;
}
.dlg-native__open:focus-visible,
.dlg-native__close:focus-visible {
outline: 3px solid var(--dn-fg);
outline-offset: 2px;
}
.dlg-native__dialog {
width: min(90vw, 26rem);
padding: 1.25rem;
color: var(--dn-fg);
background: var(--dn-bg);
border: 1px solid var(--dn-line);
border-radius: 10px;
}
.dlg-native__dialog::backdrop { background: rgb(18 20 28 / 0.6); }
.dlg-native__title { margin: 0 0 0.5rem; font-size: 1.25rem; }
.dlg-native__actions { margin: 1rem 0 0; text-align: end; }
@media (prefers-color-scheme: dark) {
.dlg-native {
--dn-bg: #1d2130;
--dn-fg: #eceef5;
--dn-line: #444b5c;
--dn-accent: #b4b8ff;
--dn-on-accent: #12141c;
}
}JavaScript
var dialog = document.getElementById('dlg-native-dialog');
var opener = document.getElementById('dlg-native-open');
// showModal() makes the rest of the page inert and draws the ::backdrop.
opener.addEventListener('click', function () {
dialog.showModal();
});A name and the first focus
A dialog needs an accessible name, so that a screen reader can say what it is when it opens. Point aria-labelledby at the heading inside the dialog. Do not put tabindex on the dialog itself: it is not interactive and does not take focus.
When a modal opens, focus moves into it. Choose where it goes with the autofocus attribute. Put it on the control the visitor is most likely to use first. If nothing else needs attention first, MDN suggests the close button. In a form, the first field is usually better, because the visitor can start typing at once.
Escape, a button and method="dialog"
A visitor should always be able to leave a dialog. The element gives you three ways.
- The Escape key closes a modal dialog. You write nothing for this.
- A button can call
dialog.close()from a click handler. - A form with
method="dialog"closes the dialog when it is sent. It sends nothing to a server. Instead, the dialog'sreturnValuebecomes thevalueof the button that was used.
When the dialog closes, the browser returns focus to the element that had it before the dialog opened. The HTML Standard requires this: its steps for closing a dialog restore focus to the previously focused element. (MDN's dialog page describes only the focus on opening, so this comes from the Standard.) That is normally the button that opened it, so the visitor lands where they were. The examples check this.
The second example uses the third way. Two buttons, "Cancel" and "Save", carry the values cancel and save. A close event handler reads returnValue and shows the outcome with textContent. The Cancel button has formnovalidate, so that it works while the name field is empty.
HTML
<div class="dlg-result">
<button type="button" class="dlg-result__open" id="dlg-result-open">Name this list</button>
<p class="dlg-result__out" role="status">Result: <output id="dlg-result-out">nothing yet</output></p>
<dialog class="dlg-result__dialog" id="dlg-result-dialog" aria-labelledby="dlg-result-title">
<form method="dialog">
<h2 class="dlg-result__title" id="dlg-result-title">Name this list</h2>
<p class="dlg-result__field">
<label for="dlg-result-name">List name</label>
<input id="dlg-result-name" name="name" type="text" autocomplete="off" required>
</p>
<p class="dlg-result__actions">
<button class="dlg-result__button dlg-result__button--plain" id="dlg-result-cancel" value="cancel" formnovalidate>Cancel</button>
<button class="dlg-result__button" id="dlg-result-save" value="save">Save</button>
</p>
</form>
</dialog>
</div>CSS
.dlg-result {
--dr-bg: #ffffff;
--dr-fg: #1b1f2a;
--dr-muted: #4a5163;
--dr-line: #c9ced9;
--dr-accent: #3730a3;
--dr-on-accent: #ffffff;
color: var(--dr-fg);
}
.dlg-result__open,
.dlg-result__button {
min-height: 44px;
padding: 0.5rem 1.25rem;
font: inherit;
color: var(--dr-on-accent);
background: var(--dr-accent);
border: 1px solid var(--dr-accent);
border-radius: 8px;
cursor: pointer;
}
.dlg-result__button--plain { color: var(--dr-accent); background: transparent; }
.dlg-result__open:focus-visible,
.dlg-result__button:focus-visible,
.dlg-result__field input:focus-visible {
outline: 3px solid var(--dr-fg);
outline-offset: 2px;
}
.dlg-result__out { margin: 0.75rem 0 0; color: var(--dr-muted); }
.dlg-result__dialog {
width: min(90vw, 24rem);
padding: 1.25rem;
color: var(--dr-fg);
background: var(--dr-bg);
border: 1px solid var(--dr-line);
border-radius: 10px;
}
.dlg-result__dialog::backdrop { background: rgb(18 20 28 / 0.6); }
.dlg-result__title { margin: 0 0 0.75rem; font-size: 1.25rem; }
.dlg-result__field { display: grid; gap: 0.25rem; margin: 0; }
.dlg-result__field input {
min-height: 44px;
padding: 0.25rem 0.5rem;
font: inherit;
color: var(--dr-fg);
background: var(--dr-bg);
border: 1px solid var(--dr-muted);
border-radius: 6px;
}
.dlg-result__actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin: 1rem 0 0; }
@media (prefers-color-scheme: dark) {
.dlg-result {
--dr-bg: #1d2130;
--dr-fg: #eceef5;
--dr-muted: #b6bccb;
--dr-line: #444b5c;
--dr-accent: #b4b8ff;
--dr-on-accent: #12141c;
}
}JavaScript
var dialog = document.getElementById('dlg-result-dialog');
var opener = document.getElementById('dlg-result-open');
var input = document.getElementById('dlg-result-name');
var output = document.getElementById('dlg-result-out');
opener.addEventListener('click', function () {
input.value = '';
dialog.returnValue = ''; // it keeps the last value otherwise
dialog.showModal();
});
// A form with method="dialog" closes the dialog when it is sent and sets
// dialog.returnValue to the value of the button that was used.
dialog.addEventListener('close', function () {
if (dialog.returnValue === 'save') {
output.textContent = 'saved as "' + input.value.trim() + '"';
} else {
output.textContent = 'cancelled';
}
});Two details are missed often. First, returnValue keeps its last value, so the script sets it back to an empty string before each showModal(). Without that, Escape after a Save would report "saved" again. Second, the Save button is refused while the required field is empty, because the form is checked before it closes the dialog.
The same with XON
XON is a small script that reads plain-word attributes from your HTML. Two of them cover a dialog. data-open="id" on a button opens the dialog with that id as a modal. data-close, with no value, on a button inside the dialog closes the dialog it is in. XON 2.1.2 is the version used here.
The markup is the same dialog element with the same name and the same autofocus. Only the script is gone. The page loads the XON file, and XON does the wiring.
HTML
<div class="dlg-xon">
<button type="button" class="dlg-xon__open" id="dlg-xon-open" data-open="dlg-xon-dialog">Show terms</button>
<dialog class="dlg-xon__dialog" id="dlg-xon-dialog" aria-labelledby="dlg-xon-title">
<h2 class="dlg-xon__title" id="dlg-xon-title">Example terms</h2>
<p>This dialog is opened by <code>data-open</code> and closed by <code>data-close</code>. There is no script in this example.</p>
<p class="dlg-xon__actions"><button type="button" class="dlg-xon__close" id="dlg-xon-close" data-close autofocus>Close</button></p>
</dialog>
</div>CSS
.dlg-xon {
--dx-bg: #ffffff;
--dx-fg: #1b1f2a;
--dx-line: #c9ced9;
--dx-accent: #3730a3;
--dx-on-accent: #ffffff;
color: var(--dx-fg);
}
.dlg-xon__open,
.dlg-xon__close {
min-height: 44px;
padding: 0.5rem 1.25rem;
font: inherit;
color: var(--dx-on-accent);
background: var(--dx-accent);
border: 1px solid var(--dx-accent);
border-radius: 8px;
cursor: pointer;
}
.dlg-xon__open:focus-visible,
.dlg-xon__close:focus-visible {
outline: 3px solid var(--dx-fg);
outline-offset: 2px;
}
.dlg-xon__dialog {
width: min(90vw, 26rem);
padding: 1.25rem;
color: var(--dx-fg);
background: var(--dx-bg);
border: 1px solid var(--dx-line);
border-radius: 10px;
}
.dlg-xon__dialog::backdrop { background: rgb(18 20 28 / 0.6); }
.dlg-xon__title { margin: 0 0 0.5rem; font-size: 1.25rem; }
.dlg-xon__actions { margin: 1rem 0 0; text-align: end; }
@media (prefers-color-scheme: dark) {
.dlg-xon {
--dx-bg: #1d2130;
--dx-fg: #eceef5;
--dx-line: #444b5c;
--dx-accent: #b4b8ff;
--dx-on-accent: #12141c;
}
}This is not a different kind of dialog. XON calls showModal() for you, so Escape, the backdrop and the return of focus behave as they did in the first example.
Opening a dialog needs JavaScript in both versions. Without it, the button does nothing. If a page cannot work without the dialog, offer the same content on a normal page as well.
All examples on this page passed their checks in Chrome 143 on 3 December 2026. They were not tested in other browsers.
References
- MDN: the dialog element (read on 3 December 2026)
- MDN: HTMLDialogElement.showModal() (read on 3 December 2026)
- MDN: HTMLDialogElement.returnValue (read on 3 December 2026)
- HTML Standard: the dialog element (read on 3 December 2026)
- W3C WAI: Dialog (Modal) pattern (read on 3 December 2026)
- XON documentation: the plain words (read on 3 December 2026)
Snippets that go with this tutorial
These snippets in the gallery use the same techniques: