Managing keyboard focus in an accessible Modal
focus management for dialogs.
move focus in on open, trap Tab inside, close on Escape, restore focus to the trigger on close.
leaving focus behind the modal so Tab escapes to the page.
What's really being asked
This checks whether you understand the full keyboard focus lifecycle of a modal dialog, which is the most common accessibility failure in design systems.
The full answer
Four moments matter. On open, programmatically move focus into the dialog, to the first interactive element or, if none is appropriate, the dialog container itself, so screen reader and keyboard users land inside. While open, trap focus: Tab from the last focusable element wraps to the first, and Shift+Tab from the first wraps to the last, so focus never escapes to the page behind. Make the background non-interactive by marking it inert or aria-hidden so assistive tech ignores it. Provide Escape to close. On close, restore focus to the triggering element so the user returns to where they were, not the top of the page. Use the correct semantics, role dialog with aria-modal true and a label via aria-labelledby pointing at the title, so the dialog is announced properly.
The mistakes people make
Not moving focus in, leaving it on the trigger so screen reader users do not know a dialog opened. No focus trap, so Tab leaks to the page behind. Forgetting to restore focus on close, disorienting the user. Missing Escape. Hiding the modal visually but leaving background focusable.
What usually comes next
How do you implement the focus trap robustly. Where should initial focus go when there is a destructive action. How does inert differ from aria-hidden.
A concrete example
A user opens a confirm dialog from a button. Focus moves to the dialog, Tab cycles only between Cancel and Confirm, the background is inert, Escape cancels, and on close focus returns to the original button, so a keyboard user never loses their place.
Interview question
After a user closes a modal dialog with the keyboard, where should focus go to meet accessibility expectations?
- a.Back to the element that triggered the modalCorrect
- b.To the document body so nothing is focused
- c.To the first focusable element on the page
- d.To the next modal in the DOM if one exists
Why? this is the answer
Returning focus to the trigger restores the user's context where they left off. Sending focus to the page top, the body, or another modal disorients keyboard and screen reader users, violating the dialog pattern.
Just read this? Test yourself on what you have been reading.
Read the original → w3.org
- #accessibility
- #modal
- #focus-management
- #aria
- #design-systems
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on accessibility — each one lists the topics its interview covers.
See open roles