A modal dialog temporarily makes the page behind it unavailable. For keyboard users, clear focus movement helps signal the change and prevents them from getting lost. section 5.3
Manage keyboard focus throughout the dialog’s lifecycle:
- When it opens, move focus inside. For a simple confirmation, focus an interactive element; for a dialog with important information, focus its heading or container. section 5.3.2
- While it remains open, keep focus within it so Tab and Shift+Tab do not reach background content. section 5.3.2
- When it closes, return focus to the element that opened it. If that element no longer exists, move focus to the next logical place on the page. section 5.3.2
Choose the starting point
For a disclaimer dialog containing important information, place initial focus on the heading rather than assuming the first action is the best starting point. section 5.3.2
<h2 id=”modal_title” tabindex=”-1”>Disclaimer</h2>
A heading can be made programmatically focusable as the dialog’s initial focus target. section 5.3.2
document.getElementById(“modal_title”).focus()
Set focus to the heading with JavaScript when the dialog opens. section 5.3.2
