Managing focus in an accessible modal dialog
Keyboard focus management for modal dialogs.
move focus in on open, trap Tab within the dialog, close on Escape, and restore focus to the trigger on close.
leaving focus behind the modal in the background.
What's really being asked
The interviewer wants the complete keyboard focus lifecycle of a modal, because a visually correct overlay that lets keyboard focus wander behind it is unusable for keyboard and screen reader users.
The full answer
Mark the container with role dialog and aria-modal true, and give it an accessible name via aria-labelledby pointing at the title. On open, move focus into the dialog: typically the first interactive element, or the dialog container itself if the first control is destructive. While open, trap focus so Tab from the last focusable element wraps to the first and Shift+Tab from the first wraps to the last, and prevent the background from receiving focus, often by setting inert or aria-hidden on the rest of the page. Provide Escape to dismiss. On close, restore focus to the triggering element so the user returns to where they were. Record that trigger when opening.
The mistakes people make
Forgetting to restore focus dumps the user at the top of the document. Not trapping Tab lets focus slip to hidden background controls. Relying only on a CSS overlay without inert or aria-hidden leaves the background reachable by screen readers. Auto-focusing a destructive button risks accidental activation.
What usually comes next
How does the native dialog element and showModal simplify this, including the top layer and automatic backdrop? When is aria-hidden risky compared to inert? How do you handle nested dialogs? Where should focus go if the dialog has no focusable content?
A concrete example
A delete confirmation opens from a row menu. Focus moves to the Cancel button, not Delete, to avoid accidental destruction. Tab cycles only between Cancel and Delete; the page behind is inert. Escape and Cancel both close it, and focus returns to the row menu button, so a keyboard user continues seamlessly.
Interview question
After a user closes an accessible modal dialog with the Escape key, where should keyboard focus go?
- a.To the top of the document body
- b.It should remain on the now-hidden dialog
- c.Back to the element that originally opened the dialogCorrect
- d.To the first focusable element in the page
Why? this is the answer
Restoring focus to the triggering element returns the user to their prior context. Sending focus to the body or page top disorients keyboard users, and focus must never stay on a hidden dialog.
Just read this? Test yourself on what you have been reading.
Read the original → w3.org
- #accessibility
- #modal
- #focus-management
- #aria
- #keyboard
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