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.
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
Activate Edit details. A dialog covers the page. Press Tab: focus moves through navigation links behind the overlay instead of entering the dialog. Another dialog on the same site does better while it is open: focus moves to its field, Tab stays inside, and Escape closes it. But close it, with Save, Cancel, × or Escape, and focus doesn't go back to Edit details. It falls to the page's body, and the next Tab starts again at the top of the page, so a keyboard user Tabs back through the navigation to find their place.
Success criterion 2.4.3 Focus Order asks that focus moves in an order that keeps the task intact. Showing the first overlay changes only what people see: removing hidden from a div doesn't move focus, and nothing makes the page behind it unreachable, so Tab carries on through the page in source order, under the overlay. The keyboard user reaches links they can't see and may never reach the dialog's field or hear its heading. The second dialog fails when it closes. Hiding it hides the button that had focus, and nothing moves focus anywhere else, so the browser drops it to the page's body. The next Tab goes on from where the dialog sat in the page. Here that is the end of the page, so focus starts again at the top, or goes to the browser's own toolbar first. Keyboard and screen reader users lose their place in the task and have to find Edit details again. That fails 2.4.3 too.
A mouse user sees the dialog, clicks into it, and clicks wherever they need next once it closes, so the problem stays hidden until someone tries it without one.
2 of 10 groups affected
- No vision (affected)
- Low vision (not affected)
- Colour vision (not affected)
- No hearing (not affected)
- Hard of hearing (not affected)
- No speech (not affected)
- Motor (affected)
- Reach, strength (not affected)
- Cognitive (not affected)
- Seizures (not affected)
- Limited manipulation A keyboard user may continue editing the wrong context behind the dialog, and lose their place after closing.
- Without vision A screen reader user may miss required input in the dialog, and lose their place after closing.
Correct role and aria-modal attributes do not move focus or prevent background interaction. A static DOM snapshot cannot prove opening, containment, dismissal, and restoration across the actual workflow.
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?
The fix
Move focus into the dialog, and back when it closes
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
hiddenshows 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.
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
- Replace the overlay with a
dialog, named by its heading witharia-labelledby. - Open it with
showModal(). Focus moves in, the page behind becomes inert, and Escape closes it. - Put
autofocuson the field. Without it, focus goes to the first focusable element, the ×. - Put the buttons in a
method="dialog"form, so each one closes the dialog. Current browsers then return focus to Edit details. - If a re-render may have replaced Edit details, choose where focus goes in the
closeevent.
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
Use the native dialog when you can. The browser handles focus, the inert background, Escape and, in current browsers, returning focus, so there is less code to get wrong. A custom dialog is for when something stops you, such as a framework or design system that already renders its own overlay. In that case, prefer a well-tested library dialog over writing your own, and verify it with the same steps.
Where focus starts, and where it returns, depends on the kind of dialog.
- Long dialog: may need initial focus on a heading with
tabindex="-1"so its beginning stays visible. - Destructive confirmation: put initial focus on the least destructive action; use
autofocuson that button. See confirmation dialogs that don't read out their question. - Nested dialogs: avoid them until their restoration behaviour is tested.
When an action changes what an open dialog shows, move focus to the new content, such as its message with tabindex="-1", or to the close button, so it never falls out of the dialog. Keeping focus inside is only correct when Escape and a focusable close button both work, so test each of them, including on dialogs from component libraries and third-party scripts. A dialog that holds focus and offers no way out is a keyboard trap, which this example doesn't show; see pop-ups that can't be closed with a keyboard.
Verify the fix
5 checks, no mouse.
Test with: Keyboard, Screen reader, Zoom, Forced colors
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.
References
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.