Sub topic 4
What the browser does for you: top layer and inert content
This chapter explains two mechanisms the browser applies when you call showModal(): the top layer and inertness of the rest of the page. It assumes you can open and close a dialog from JavaScript, as covered in the previous chapters. It also assumes you know basic CSS stacking (z-index, position, overflow) and the idea of focus in a web page.
After reading, you should be able to explain why a modal dialog needs no z-index, what "inert" means for the page behind it, and which mistakes defeat these mechanisms.
Two jobs, one method call
Before <dialog>, a modal had to do two separate jobs:
- Appear above everything else. Developers raised
z-indexvalues, moved the modal to the end of<body>, and fought stacking contexts. - Block the page behind it. Developers added an overlay
<div>, trapped Tab with JavaScript, and setaria-hiddenon siblings.
showModal() does both jobs for you. The first job is done by the top layer. The second is done by marking the rest of the document as inert.
The top layer
The top layer is a browser-managed list of elements that are painted above the rest of the page. It is not part of any stacking context in your document. Elements in the top layer are rendered as if they were attached directly to the root, so these things do not affect them:
z-indexon the element or any of its ancestorsoverflow: hiddenorclip-pathon an ancestor- a
transform,filter, oropacityon an ancestor that would normally create a stacking context or a containing block for fixed elements
The dialog stays at its place in the DOM tree. Only its painting changes. It still inherits CSS properties such as color and font-family from its DOM ancestors, and events still bubble through them.
The top layer is not specific to dialogs. Fullscreen elements and popovers (the popover attribute) use it too.
Seeing it work
This page puts a dialog inside a clipped, transformed card, and a fixed banner with a huge z-index sits above the page. Save it and open it.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Top layer demo</title>
<style>
.banner {
position: fixed;
inset: 0 0 auto 0;
z-index: 2147483647;
background: gold;
padding: 0.5rem;
}
.card {
position: relative;
z-index: 1;
width: 14rem;
height: 6rem;
margin-top: 4rem;
padding: 1rem;
border: 1px solid #888;
overflow: hidden;
transform: translateZ(0);
}
</style>
</head>
<body>
<div class="banner">Banner with the largest possible z-index</div>
<div class="card">
<button id="open">Open dialog</button>
<dialog id="dlg">
<p>I live inside a clipped, transformed card.</p>
<button id="close">Close</button>
</dialog>
</div>
<script>
const dlg = document.getElementById('dlg');
document.getElementById('open').addEventListener('click', () => {
dlg.showModal();
console.log('matches :modal?', dlg.matches(':modal'));
});
document.getElementById('close').addEventListener('click', () => dlg.close());
</script>
</body>
</html>When you open the dialog, it appears centered in the viewport, is not clipped by the card, and covers the banner. The :modal pseudo-class matches an element that was opened with showModal(). Use it in CSS and in matches() to tell modal dialogs from non-modal ones.
Stacking order inside the top layer
Elements in the top layer are ordered by when they were added. The most recently added element is on top. z-index is ignored between them.
This matters in three cases:
- Two modal dialogs. If you open a confirmation dialog from inside an edit dialog, the confirmation appears above it. The order is the order of the
showModal()calls. - Toasts and tooltips. A toast in normal page flow with
z-index: 9999will be hidden behind any open modal dialog. Put the toast inside the dialog, or make it a popover that is shown after the dialog opens. - Popovers inside a dialog. A popover opened from inside a modal dialog is added to the top layer after the dialog, so it appears above it.
The backdrop
Each top-layer element can generate a ::backdrop pseudo-element. It is a viewport-sized box placed directly beneath its element and above everything below it in the top layer. A modal dialog gets a backdrop automatically. A dialog opened with show() does not, because it never enters the top layer. Styling the backdrop is covered in a later chapter.
The browser's built-in styles also change the dialog's positioning. A modal dialog gets position: fixed, with a maximum size that leaves a small margin around it. A non-modal dialog gets position: absolute. If you override position, inset, or margin on a dialog, test both modes.
Inert content
The second effect of showModal() is that everything in the document except the dialog and its descendants becomes inert. An inert element:
- cannot be clicked or tapped, because pointer events do not reach it
- cannot receive focus, and Tab skips it
- is left out of the accessibility tree, so screen readers do not announce it
The effect is that keyboard users cannot Tab into the page behind the dialog, and screen reader users hear only the dialog.
The spec describes this as the document being "blocked by a modal dialog". It is a separate state from the inert HTML attribute, although the behavior is the same. A blocked element does not get inert="true". Reading element.inert on a blocked element returns false.
Checking inertness from script
<button id="outside">Outside button</button>
<dialog id="dlg">
<p>Modal content</p>
<button id="close" autofocus>Close</button>
</dialog>
<script>
const dlg = document.getElementById('dlg');
const outside = document.getElementById('outside');
dlg.showModal();
outside.focus();
console.log('focus moved outside?', document.activeElement === outside); // false
console.log('inert attribute?', outside.inert); // false
console.log('active element id:', document.activeElement.id); // close
</script>The call to outside.focus() is ignored because the button is blocked. Focus stays on the dialog's Close button. The inert property is still false, which shows that blocking is a different mechanism from the attribute.
What inert does not block
Inertness is narrower than people expect:
- Browser UI is still reachable. Tab can leave the page and reach the address bar and browser toolbar. The browser does not trap focus in the dialog beyond that.
- Scrolling may still work. The wheel or touch gesture over the backdrop can scroll the page behind the dialog in many browsers. Stop it yourself with
body:has(dialog[open]) { overflow: hidden; }. - The page keeps running. Timers, network requests, and animations continue. Inert affects interaction, not execution.
- The page behind is still visible. The backdrop is what dims it, and only if you style it.
The whole sequence
Comparing show() and showModal()
| Behavior | `show()` | `showModal()` |
|---|---|---|
| Enters the top layer | No | Yes |
| Generates `::backdrop` | No | Yes |
| Rest of the page inert | No | Yes |
| Matches `:modal` | No | Yes |
| Built-in positioning | `position: absolute` | `position: fixed` |
| Escape closes it | No | Yes |
| Ordering against other content | Normal `z-index` rules | Order of insertion into the top layer |
A dialog written as <dialog open> in the markup is non-modal. It has the open attribute but was never passed to showModal(), so it has none of the modal behavior in the table.
Pitfalls specific to these mechanisms
Calling showModal() on a dialog that is already open
If the dialog already has the open attribute because you wrote <dialog open> or called show(), then showModal() throws an InvalidStateError. It also throws if the dialog is not connected to the document. Close the dialog first, or check dlg.open before opening it.
function openModal(dlg) {
if (dlg.open) {
dlg.close();
}
dlg.showModal();
}Putting the dialog inside an inert container
Inertness is inherited down the tree. Suppose you open your own settings panel by setting inert on the app root, and the dialog is a child of that root. The dialog is inert too, so nobody can click or focus anything in it. Keep dialogs outside any element that you might mark inert or aria-hidden. A direct child of <body> is the safest place.
Overriding display on the dialog
The browser hides a closed dialog with display: none from its built-in styles. If you write dialog { display: flex; }, your rule wins and a closed dialog appears in the page, not in the top layer. Scope the rule so it applies only when open:
dialog[open] {
display: flex;
flex-direction: column;
}Fighting the top layer with z-index
Raising z-index on a toast to show it over a modal dialog does nothing, because the dialog is not in the same stacking order. Use the top layer's own ordering instead, as described above.
Expecting focus to be fully trapped
Tab cycles through the dialog's focusable elements and then leaves the page to the browser UI. If you need a strict trap, add one yourself. For most dialogs the built-in behavior is what users of the platform expect.
Summary of the working model
showModal()adds the dialog to the top layer, so ancestors'z-index, clipping, and transforms do not affect it.- Top-layer order is insertion order, and
z-indexis not consulted between top-layer elements. - Everything outside the dialog is blocked: unclickable, unfocusable, and hidden from assistive technology. This is not the same as the
inertattribute, andelement.inertstaysfalse. - Scrolling, browser UI focus, and timers are not blocked.
<dialog open>andshow()give a non-modal dialog with none of this.
Exercise
A page has a toast notification element with position: fixed; z-index: 99999. A user opens a modal dialog with showModal(), and a toast appears while it is open. Where does the toast appear relative to the dialog, and how would you make the toast visible above the dialog?
Show answer
The toast appears behind the dialog and its backdrop. The modal dialog is in the top layer, which paints above all normal page content regardless of z-index. To show the toast above the dialog, put it in the top layer after the dialog is opened, for example by making it a popover (popover="manual") and calling showPopover(). Because top-layer order is insertion order, it will be placed above the dialog. Alternatively, render the toast inside the dialog.
Exercise
You call dlg.showModal() and then run console.log(document.getElementById('menu').inert) on a button outside the dialog. What does it print, and why can the user still not click that button?
Show answer
It prints false. Opening a modal dialog does not set the inert attribute on other elements. The document is instead put into a separate "blocked by a modal dialog" state, which has the same effect as inert: the button cannot be clicked, focused, or read by assistive technology until the dialog closes.
Exercise
A developer opens a settings panel by setting inert on <div id="app">. The page's only <dialog> is a child of that div. When they call showModal(), the dialog appears but none of its buttons work. What is wrong and what is the fix?
Show answer
The dialog is a descendant of an element with the inert attribute, and inertness is inherited by descendants. Because showModal() only exempts the dialog from the modal blocking, not from an inert ancestor, the dialog is inert as well. Move the dialog out of #app, for example to be a direct child of <body>, or remove inert from the ancestor while the dialog is open.