Sub topic 4
What the browser does for you: top layer and inert content
This chapter assumes you can write basic HTML forms, select elements and add event listeners in JavaScript, and use CSS selectors and pseudo-elements. It also assumes you have seen showModal() open a dialog. Here you will learn what the browser does at that moment, so you can predict how a modal behaves and avoid fighting it.
What showModal() changes
Calling showModal() on a dialog does more than set the open attribute. The browser also does the following:
- It puts the dialog in the top layer.
- It makes the rest of the document inert.
- It draws a
::backdroppseudo-element behind the dialog. - It moves focus into the dialog.
The first two items are the subject of this chapter. Focus and the backdrop get their own chapters.
<button id="open">Open</button>
<dialog id="dlg" aria-label="Demo">
<p>Hello</p>
<form method="dialog"><button>Close</button></form>
</dialog>
<script>
document.getElementById('open').addEventListener('click', () => {
document.getElementById('dlg').showModal();
});
</script>The top layer
The top layer is a part of the browser's rendering that sits above the whole page. An element in the top layer is drawn over everything else, however the rest of the page is stacked.
Before <dialog>, a modal had to win a fight with z-index. You gave the overlay a very large number, and then a parent with its own stacking context could still trap it. The top layer removes that problem.
- A modal dialog is painted above all normal content, including elements with a huge
z-index. - It does not matter where the
<dialog>sits in the markup. A dialog nested inside a card withoverflow: hiddenortransformis not clipped by that card. - If several elements are in the top layer, the one added last is on top. Opening a second modal from inside the first stacks it above the first.
Inert content
The second effect is that everything outside the dialog becomes inert. An inert element cannot be interacted with. Inertness has these effects:
- Clicks, taps and text selection on inert content do nothing.
- Inert content cannot receive focus, so Tab and Shift+Tab cycle only through the dialog and the browser interface.
- Assistive technologies treat the inert content as unavailable, so a screen reader user is not left browsing a page that is meant to be blocked.
You do not add the inert attribute yourself. The browser applies the behavior for as long as the dialog is modal, and removes it when the dialog closes.
Seeing the difference
The next example opens the same content in both ways. Try it and press Tab or click the page text after each call.
<p id="page-text">Select this text if you can.</p>
<button id="modal">showModal()</button>
<button id="plain">show()</button>
<dialog id="d">
<p>Dialog content</p>
<button onclick="this.closest('dialog').close()">Close</button>
</dialog>
<script>
const d = document.getElementById('d');
document.getElementById('modal').addEventListener('click', () => d.showModal());
document.getElementById('plain').addEventListener('click', () => d.show());
</script>After showModal(), you cannot select the paragraph or click the other buttons. After show(), you can do both, because nothing is inert and the dialog is not in the top layer.
Common errors
- Expecting inertness from show(). A non-modal dialog leaves the page fully interactive. If you need blocking, use
showModal(). - Fighting the top layer with z-index. A
z-indexon a modal dialog changes nothing useful. Remove it. - Placing other floating UI outside the dialog. The page is inert, so a toast, tooltip or custom menu that lives in the page body cannot be used while the modal is open. It is also drawn beneath the dialog. Put such UI inside the dialog, or open it as a popover so it enters the top layer too.
- Assuming the browser interface is blocked. Inertness covers the document only. Address bar, tabs and browser shortcuts still work.
Exercise
A page has a <dialog> nested inside a container with overflow: hidden. You open it with show() and it is clipped. What single change makes it display fully, and which two browser behaviors does that change add besides the display fix?
Show answer
Open it with showModal() instead. This moves the dialog into the top layer, so the container no longer clips it. It also makes the rest of the page inert, and it draws a ::backdrop. (Focus also moves into the dialog.)
Exercise
With a modal dialog open, you press Tab repeatedly. Why does focus never reach the links in the page behind it?
Show answer
The rest of the document is inert while the dialog is modal, and inert elements cannot receive focus. Focus stays within the dialog and the browser interface.