Full answer
A native dialog opened with showModal() handles most of this for you (see the question on the native dialog). If you must build your own overlay, you take on four jobs.
- Move focus into the overlay when it opens. Focus the first useful control, or the heading if the content is long, with
tabindex="-1"on it. - Keep focus inside while it is open. The old way is a script that catches Tab on the last element and sends it to the first. The modern way is to make everything else inert. The
inertattribute makes an element and its children not focusable, not clickable and hidden from assistive technology. Setinerton the page content behind the overlay (the overlay must not be inside that part). - Let the user close it with the Escape key and with a visible close button.
- When it closes, remove
inertand return focus to the control that opened it. Without this, focus falls back to the top of the page, and keyboard users lose their place.
Add role="dialog", aria-modal="true" and an accessible name with aria-labelledby. Mark the background as not scrollable if needed, for example with overflow: hidden on the body, and restore it on close.
Pitfalls: opening several overlays at once, removing the control that opened it from the page while the overlay is open (then there is nowhere to return to), and forgetting that a script may need to restore inert on every code path that closes the overlay.
On opening, focus moves to the Close button, and the buttons behind cannot receive focus. Pressing Escape or Close hides the overlay and puts focus back on the Open button.
Test with the keyboard alone and with a screen reader. Where possible, use the native dialog instead.
<main id="page"><button id="openBtn" type="button">Open</button><button id="behind" type="button">Behind</button></main>
<div id="ov" role="dialog" aria-modal="true" aria-labelledby="t" hidden>
<h2 id="t">Settings</h2>
<button id="closeBtn" type="button">Close</button>
</div>
<script>
openBtn.addEventListener("click", () => { ov.hidden = false; page.inert = true; closeBtn.focus(); });
function shut() { ov.hidden = true; page.inert = false; openBtn.focus(); }
closeBtn.addEventListener("click", shut);
document.addEventListener("keydown", e => { if (e.key === "Escape" && !ov.hidden) shut(); });
</script>Checked with real key presses in Chrome. No screen reader was run. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Focus must move into the overlay on open.
- inert makes the background unfocusable and hidden to assistive tools.
- Escape and a close button must close it.
- Return focus to the control that opened it.
- Prefer native dialog where possible.
Common mistakes
- Not returning focus to the control that opened it on close.
- Putting the overlay inside the element that is set to inert, so it cannot be used either.
- Closing the overlay in one code path and forgetting to remove inert.
Follow-ups
- What does the inert attribute do?
- Why prefer the native dialog element?
Tips
- List the four jobs: move in, keep in, allow close, return focus.
- Name inert as the modern replacement for focus trap scripts.