The dialog Element
How dialog gives you modals for free: showModal vs show, focus handling, Escape, the backdrop, returnValue, and what it still leaves to you.
details and summaryKeyboard, Focus and tabindexHiding Content: hidden, inert and Visually Hidden
The dialog Element
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `dialog.showModal()` opens a modal: it goes in the top layer, gets a `::backdrop`, makes the rest of the page inert, and moves focus into the dialog. `show()` opens a non-modal dialog with none of that.
- Focus goes to the first focusable element, or to an element with `autofocus`, or to the dialog itself if nothing inside is focusable. On close it returns to the element that opened it.
- Escape fires `cancel` then `close`. A `<form method="dialog">` closes the dialog and sets `returnValue` to the clicked button's value.
- Clicking the backdrop did not close a modal by default, and the page behind could still scroll in Chromium and Firefox. You add both behaviours yourself, or use `closedby="any"` where supported.
- A dialog needs a name. Point `aria-labelledby` at its heading, or the dialog exposes no name.
Why modals were hard
A modal has a to-do list longer than it looks. It must appear above everything (z-index fights), dim the page, move focus inside, keep Tab from escaping into the page behind, hide the page from screen readers, close on Escape, and put focus back where it started. Libraries existed for exactly this, and plenty of hand-rolled ones got one of the items wrong.
<dialog> bundles the list. It has been Baseline since March 2022 and widely available since September 2024, per web-features.
<button id="open">Edit profile</button>
<dialog id="dlg" aria-labelledby="t">
<h2 id="t">Edit profile</h2>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="ok">Save</button>
</form>
</dialog>
<script>
open.onclick = () => dlg.showModal();
</script>
What showModal does
I ran this in Chromium 159, Firefox 159 and a WebKit build.
- Top layer and backdrop. The dialog was
position: fixed, centred with auto margins, and::backdropstyling applied (a red background came through as computed). - Focus moves in. Focus went to the first focusable element, the Cancel button. A dialog with no focusable content focused itself, and
autofocuswas honoured. - The rest is inert. Pressing Tab cycled the dialog’s buttons and then left the page to the browser’s own UI and came back. It never reached the links and input behind. In Chromium’s accessibility tree the dialog appeared with its name from
aria-labelledby, and the page behind was gone from the tree. This is the inert behaviour applied for you. - Escape closes it, firing
cancelthenclose. Focus returned to the opening button. <form method="dialog">closes it on submit. The clicked button’svaluebecamereturnValue; after Escape,returnValuewas an empty string.
show() is the non-modal version. In the test it did not match :modal, had no backdrop, and Tab left the dialog and reached outside controls. Use it for things like a floating panel; for anything that must be answered, use showModal().
What it leaves to you
- Outside click. Clicking the backdrop did not close the modal. Either compare
event.target === dialogin a click handler, or tryclosedby="any"(Chromium and Firefox in my test, butweb-featureslists no Safari support yet). - Page scroll. With the modal open a mouse wheel still scrolled the page behind in Chromium and Firefox. The video covers preventing that with CSS.
- A name. Use
aria-labelledbyon the dialog, as above. - Styling. The user-agent styles are heavy; the video spends several minutes on overriding them, including not setting
display: flexon the closed state, which would make it visible. - Animation of the closing state is a CSS topic (transitions).
Also dialog.requestClose() exists in all three engines tested, which fires cancel first so code can veto the close.
Dialog or popover?
A dialog interrupts: the user must deal with it. A popover is lighter and dismisses on outside click. The next lesson, the popover attribute, compares them.
What the video covers
Kevin Powell’s “Build modals in minutes with the dialog element” (January 2026, about 31 minutes). The chapters are: the basics, opening with .show() and .showModal(), adding a close button, dealing with autofocus, making the close button accessible, styling and the many user-agent styles, preventing page scroll when a modal is open, styling the backdrop, adding flex or grid only on the opened state, a use case for .show(), and overcoming positioning issues. I could not read a transcript. The behaviour results above are my own browser tests; Web Dev Simplified’s shorter 2023 intro is the runner-up.
Where this fits
This builds on keyboard focus and the inert section of hiding content. Next is the popover attribute.
Check yourself
What is the difference between `show()` and `showModal()`?
`showModal()` makes the dialog modal: top layer, backdrop, rest of the page inert, Escape to close. `show()` just reveals it in the normal flow with no backdrop. In a test, Tab left a `show()` dialog and reached other controls, while after `showModal()` it stayed within the dialog.
A dialog has no focusable children. Where does focus go on `showModal()`?
To the dialog element itself, in all three engines tested. If you want a specific control focused, put `autofocus` on it, which was honoured in the test.
How do you find out which button closed a form-based dialog?
Use `<form method="dialog">` with buttons that have `value`s. When one is clicked the dialog closes and `dialog.returnValue` holds that value. In a test, clicking OK gave `returnValue` of "ok", and Escape gave an empty string.
A user clicks the dim area outside a modal and nothing happens. Is that a bug?
It is the default. In a test a click on the backdrop left the dialog open. Add a click handler that compares the click target to the dialog, or use `closedby="any"`, which closed the dialog on an outside click in my test but is not in every browser yet.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.