You're reading an article. Suddenly the screen darkens and a box appears: "Subscribe to our newsletter!" The page behind it can't be touched until you find the tiny × in the corner.
That newsletter box is a modal: a box that appears on top of the page and stops the page behind it until you answer. Overlay is the general name for anything shown on top of the page. A modal is the strictest overlay, because the page behind it can't be used. Used at the right moment, a modal isn't annoying at all. Take Mrs. Sri's front desk. Before she writes your name in the guest book, she holds her pen and asks: "Terrace Room, 12 to 14 Dec, $70, right?" While she asks, you can't wander off to look at other rooms. You can only answer "yes" or "cancel", and afterwards you're back exactly where you were standing. On a web page, Mrs. Sri's question is the modal, the other rooms are the page behind it, and the place where you were standing is the button you pressed to open the modal.
The booking confirmation modal
On the Mist Homestay page, the "Book the room, pay on arrival" button now opens one modal: a summary of the booking and two choices. The browser already has an element for this, <dialog>. To open it as a modal, call showModal(), a JavaScript command (JavaScript is the programming language in the browser). Here is the HTML, CSS and JavaScript:
<button class="book" id="book">Book the room, pay on arrival</button>
<dialog class="modal" aria-labelledby="modal-title">
<form method="dialog">
<button class="close-btn" value="cancel" aria-label="Close">×</button>
<h2 id="modal-title">Book the Terrace Room?</h2>
<p>12 to 14 Dec · 2 nights · 2 guests</p>
<p class="total">Total $70, pay on arrival</p>
<p class="note">Free cancellation up to 3 days before check-in.</p>
<div class="actions">
<button value="cancel" class="cancel">Go back</button>
<button value="book" class="book">Yes, book it</button>
</div>
</form>
</dialog>
<style>
.modal { max-width: 20rem; padding: 24px; border: 0; border-radius: 12px; color: #1f2937; }
.modal::backdrop { background: rgb(31 41 55 / 0.6); } /* #1f2937 at 60% */
html:has(dialog:modal) { overflow: hidden; } /* the page behind doesn't scroll */
</style>
<script>
const book = document.querySelector('#book');
const modal = document.querySelector('.modal');
book.addEventListener('click', () => modal.showModal());
modal.addEventListener('close', () => {
if (modal.returnValue === 'book') {
// send the booking to Mrs. Sri
}
});
</script>
<dialog>is HTML's built-in element for a dialog box. WithoutshowModal(), it stays hidden.aria-labelledby="modal-title"is an attribute that gives the dialog a name. Its value points at theid(an element's unique name) of the heading, so a screen reader announces "Book the Terrace Room?, dialog" when the modal opens.<form method="dialog">: the buttons inside this form close the dialog with no JavaScript. Thevalueof the button pressed becomesmodal.returnValue:"book"or"cancel".- The
×button getsaria-label="Close"as its name. Without it, a lone × is read out as the symbol's name ("times"), not "close". ::backdropis the dark layer between the modal and the page. Its colour is#1f2937at 60% opacity.showModal()opens the dialog as a modal. Thecloseevent (the moment the dialog closes) happens every time the dialog is closed, by a button or by the Esc key. The code that waits for that event readsreturnValueto learn the guest's choice.
What showModal() does for you
Say Mrs. Sri's team builds the confirmation modal from a plain <div>, with a dark layer behind it. Mr. Hadi uses the keyboard. He presses "Book the room, pay on arrival", then presses Tab a few times. Does his focus stay inside the modal? Guess before you look at the table below.
A proper modal meets three accessibility requirements:
- it has a name (
aria-labelledby), - focus stays inside the modal (a focus trap): Tab never leaves for the page behind,
- Esc closes it.
We compared showModal() with a modal built from a <div> plus a dark layer in Chrome, Safari and Firefox:
| Behaviour | <dialog> + showModal() |
<div> + overlay |
|---|---|---|
| On opening, focus moves to | the first focusable element in the modal (here the × button) | stays on the "Book" button behind |
| Tab | never lands on the page behind | goes through to the page behind (here "Room facilities", under the Book button) |
| Esc | closes the modal | nothing happens |
| After closing, focus returns to | the "Book" button that opened it | lost |
| Scrolling the page behind | still moves, unless locked | still moves |
showModal() makes everything on the page outside the dialog inert: it can't be clicked, can't be focused, and isn't read by a screen reader. That is the focus trap. After the modal's last button, Tab may move to the browser's address bar, and then back to the modal. That behaviour is correct, because WCAG 2.1.2 (No Keyboard Trap) requires that people can always get out. What matters is that the page behind is never reached.
So the answer to the guess: no. With a <div>, focus starts on the "Book" button behind the dark layer, and Tab goes through to the page's buttons under the dark layer. Mr. Hadi can't see where his focus is, and Esc closes nothing.
Lock the page's scroll
There is one thing showModal() doesn't do. In all three browsers, turning the mouse wheel over the dark layer (the backdrop) still scrolls the page behind. In our test, the scroll position went from 300px to 700px. One line of CSS locks it while the modal is open:
html:has(dialog:modal) { overflow: hidden; }
:modal selects a dialog that is open through showModal(). :has() makes this rule apply to <html> only while a dialog is open as a modal. On <html>, overflow: hidden stops the page from scrolling. With this rule, the scroll position stays at 300px.
When a modal is the wrong choice
A modal forces people to stop, so use a modal only when that stop is needed. Nielsen Norman Group, a UX research company, gives this guidance (Therese Fessenden, 2017). Use modals for important warnings that prevent errors, and for information required before continuing. Don't use modals for information that isn't essential, or for decisions that need information from outside the modal.
In this table, inline means the message is written right on the page, next to the thing it is about:
| On Mrs. Sri's page | Choice | Why |
|---|---|---|
| The summary before the booking is sent | Modal | One short decision: yes or cancel |
| "Free cancellation up to 3 days before check-in." | Inline | Read while deciding, must sit next to the price |
| Name, phone number, arrival time, special requests | Own page | A long form; on a 375px phone the keyboard covers half the modal |
| "The Terrace Room is fully booked on 12 Dec" | Inline | The error belongs to the date, shown next to the date |
| "Booking sent" | Inline | News, not a question; nothing to dismiss |
📌 Other overlays: drawer, popover, toast
- Drawer: a panel that slides in from the side, such as room filters on a phone. If the page behind can't be used, that drawer is a modal too, and the same
<dialog>builds it. - Popover: a small box attached to a button, such as a "Share" menu. The page stays usable.
- Toast: a short message in a corner that disappears by itself, such as "Copied". The page stays usable, and the message shouldn't hold an important button.
Other options. HTML has a built-in tool for each kind of overlay:
| Tool | When to pick it |
|---|---|
<dialog> + showModal() |
a modal: a decision that must be answered. This lesson uses it |
The popover attribute: <button popovertarget="share"> opens <div id="share" popover> |
a "Share" menu and a toast. A click outside the box or Esc closes it with no JavaScript (Chrome 114, Firefox 125, Safari 17) |
The inert attribute on the part of the page behind |
when you must build your own overlay from a <div>. That part cannot be clicked or focused (Chrome 102, Firefox 112, Safari 15.5) |
There are two newer helpers for <dialog id="confirm">. The button <button commandfor="confirm" command="show-modal"> opens it as a modal with no JavaScript (Chrome 135, Firefox 144, Safari 26.2). The attribute closedby="any" closes it when the backdrop is clicked (Chrome 134, Firefox 141, and Safari only in a beta version). We tried them all in Chromium, Firefox and WebKit 26.4: all of them work, except closedby in WebKit.
⚠️ Looks like a modal, but isn't
- A
<div>with a high z-index as the modal. It looks like a modal, but Tab goes through to the page behind and Esc does nothing. Use<dialog>withshowModal(). show()instead ofshowModal().show()opens an ordinary dialog: the page behind can still be clicked and reached with Tab, and Esc doesn't close it.aria-labelledbypointing at anidthat doesn't exist or is empty. The modal opens with no name. The heading'sidand the value ofaria-labelledbymust match exactly.- A × button without
aria-label. A screen reader reads the symbol ("times"), not "close". Addaria-label="Close". - A modal on top of a modal. People can't tell which one to close. Finish the first modal, or move its content to a page.
TL;DR: A modal is an overlay (something shown on top of the page) that stops the page until people answer. Use <dialog>, then open it with the JavaScript command showModal(). The browser then moves focus into the modal and makes the page behind inert (it can't be clicked or focused), so focus stays inside the modal: that is the focus trap. The browser also closes the modal on Esc, and returns focus to the button that opened it. Give the modal a name (what a screen reader reads out) with aria-labelledby pointing at its heading's id, and give the × button aria-label="Close". Lock the scroll of the page behind with html:has(dialog:modal) { overflow: hidden; }. <form method="dialog"> closes the modal with no JavaScript. Use a modal only for a short decision that must be answered (a confirmation, a warning). The cancellation policy, errors next to a field and success news stay inline (written right on the page), and a long form gets a page of its own.
⚡ A dialog with a name
Write:
<dialog class="modal" aria-labelledby="modal-title">
<form method="dialog">
<button class="close-btn" value="cancel" aria-label="Close">×</button>
<h2 id="modal-title">Book the Terrace Room?</h2>
<button value="book" class="book">Yes, book it</button>
</form>
</dialog>
Meaning: A modal (a box that appears on top of the page and stops the page until you answer) is built with <dialog>. The aria-labelledby attribute points at its heading's id (its unique name), so a screen reader reads this modal out as "Book the Terrace Room?, dialog". The × button needs an aria-label, because the × symbol alone doesn't mean "close". <form method="dialog"> closes the modal when one of its buttons is pressed, with no JavaScript.
⚡ showModal() handles the focus
Guess first: a modal is built from a plain <div> with a dark layer. Mr. Hadi, who uses the keyboard, presses Tab a few times. Does his focus stay inside the modal?
No. His focus stays on the button behind, Tab goes through to the page under the dark layer, and Esc closes nothing.
Meaning: A proper modal has a name (aria-labelledby), a focus trap (focus stays inside the modal), and closes on Esc. showModal(), the JavaScript command that opens a <dialog> as a modal, makes the page behind inert (it can't be clicked, focused or read by a screen reader). With showModal(), Esc also closes the modal, and focus returns to the button that opened it.
⚡ Lock the scroll, use it sparingly
Write:
html:has(dialog:modal) { overflow: hidden; }
Meaning: showModal() doesn't lock the scroll of the page behind. This line locks it: while a dialog is open as a modal (dialog:modal), <html> gets overflow: hidden, so the page stops scrolling. A modal is only for a short decision that must be answered, like a confirmation. The cancellation policy, errors next to a field and success news stay inline (written right on the page).