Explore Bato Web Agency Webflow Certified Partner Profile Explore Bato Web Agency Webflow Certified Partner Profile

How should keyboard focus work inside a modal dialog?

Illustration without a description Illustration without a description

When a modal dialog opens, keyboard focus should move to an appropriate element inside it, and the page behind it should become inactive. Tab and Shift+Tab must remain within the active dialog until it closes. The dialog needs a clear accessible name, an operable close method and predictable Escape behavior where closing is allowed.

 

Initial focus depends on the content. A short confirmation may focus the safest or most likely action. A long dialog often benefits from focusing a heading or introductory element with tabindex="-1" so the top is read without scrolling past it. For destructive, difficult-to-reverse actions, placing focus on Cancel can reduce accidental confirmation.

 

The expected sequence is:

 

  1. store a reference to the element that opened the dialog;
  2. show the dialog and make background content inert;
  3. move focus inside after the content is available;
  4. cycle Tab from the last focusable item to the first and Shift+Tab in reverse;
  5. close through a visible button and, where appropriate, Escape;
  6. restore focus to a logical location.

 

Do not use aria-modal="true" unless the behavior is genuinely modal. ARIA changes how assistive technology understands the interface but does not trap focus, disable the background or create keyboard handlers. Prefer the native dialog element when its behavior fits the support requirements, while still testing initial and returned focus.

 

Test dialogs with few controls, long scrolling content, validation errors and nested confirmations. Confirm that the close button is reachable and that background controls cannot receive pointer or keyboard interaction. A modal is accessible when focus, visual layering and the accessibility tree all describe the same active context.