Skip to content
easeweb
Search topics

Modals that leave keyboard focus behind them

Keyboard focus stays on the page behind an open dialog, or is not given back to the button that opened it when the dialog closes.

WCAG 2.2
SC 2.4.3A
Required by
Section 508 · EN 301 549 (EU)
Broken
The accessibility problem illustrated: Motor:Am I in the dialog?
01

The problem

Keyboard users are left on the page behind an open dialog, or sent back to the top of the page when it closes

02

Try it

Open the dialog, press Tab, then close it

  • Broken 1: open the dialog, then press Tab and Escape. Does focus go into the dialog, and does Escape close it?

  • Broken 2: open the dialog and Tab through it: focus stays inside. Then close it with Save, Cancel, × or Escape, and press Tab once more. Where does focus go?

  • Apply a fix. Open the dialog and press Tab and Escape again: where does focus go when the dialog closes?

Broken Can you fix this?

Page body
My account

A recreated demo. The broken version is intentionally inaccessible.

03

The fix

Move focus into the dialog, and back when it closes

Before

An overlay that only looks modal

<div class="overlay" hidden>  <div class="modal">    <p class="modal-title">Edit details</p>    <button type="button" class="close" aria-label="Close" onclick="closeModal()">×</button>    <label for="display-name">Display name</label>    <input id="display-name" name="name">    <button type="button" onclick="closeModal()">Save</button>    <button type="button" onclick="closeModal()">Cancel</button>  </div></div>
What is wrong4 points
  • Focus stays behind on Edit details when the dialog opens: removing hidden shows the overlay but moves no focus.
  • Tab reaches links hidden under the overlay: nothing makes the page behind unreachable, so Tab carries on through it.
  • Escape doesn't close the dialog: nothing listens for it.
  • A screen reader user isn't told a dialog opened: the overlay has no role or name.
After

A native dialog

<button type="button" id="edit-trigger">Edit details</button> <dialog id="edit-dialog" aria-labelledby="edit-title">  <form method="dialog">    <h2 id="edit-title">Edit details</h2>    <button value="close" aria-label="Close">×</button>    <label for="display-name">Display name</label>    <input id="display-name" name="name" autofocus>    <button value="save">Save</button>    <button value="cancel">Cancel</button>  </form></dialog>
How it is fixed5 steps
  1. Replace the overlay with a dialog, named by its heading with aria-labelledby.
  2. Open it with showModal(). Focus moves in, the page behind becomes inert, and Escape closes it.
  3. Put autofocus on the field. Without it, focus goes to the first focusable element, the ×.
  4. Put the buttons in a method="dialog" form, so each one closes the dialog. Current browsers then return focus to Edit details.
  5. If a re-render may have replaced Edit details, choose where focus goes in the close event.

This fixes both Befores and passes 2.4.3. An animation, a styled ::backdrop and a click outside to close it (closedby="any", not yet in Safari in October 2026) work on the native dialog too, and focus still moves the moment showModal() runs.

Why this fix: Use native HTML first

04

Verify the fix

5 checks, no mouse.

Test with: Keyboard, Screen reader, Zoom, Forced colors

0 of 5 checked

Record the OS, browser and assistive-technology versions, the build, the date, and the actual result of each step. Repeat after the shared dialog component changes.

How screen readers handle <dialog>

Limits

This is a starting pattern, not a guarantee for every application. The snippets omit saving and validation. Test the complete journey with your target assistive technology.

This is a recreated teaching example, not a finding about a named client. The code is a starting pattern: test it in your own product. No completed assistive-technology test is claimed here.

EaseWeb service

Fixed it? Keep it from coming back.

I turn guides like this one into rules your coding agents follow and checks your CI runs. Then I retest by hand with a keyboard and screen readers, periodically.

See the service