This translation has not been editorially reviewed yet. The German version is authoritative. Deutsche Fassung
Accessibility
Can a dialog window hold the keyboard focus?
Yes. A modal dialogue window, i.e. one that locks the rest of the page, may keep the keyboard focus with you. The W3C describes this behavior itself as an example that meets the rule against keyboard traps: The tab button circles inside the window, and Escape, Cancel or OK close it. The window becomes a trap only if the output is missing or the focus is lost after closing. What a dialog has to be able to do and which HTML element does it by itself is below.
Hold on yes, lock up no
The explanation of the W3C for criterion 2.1.2 says that a dialog window may restrict the focus as long as the user knows how to solve it again. In the example of the W3C, the focus remains in the window: a tab jumps from the last control element to the first, and the window closes via Cancel, OK or the escape button. The same rules are in the ARIA Authoring Practices dialog pattern: Tab and Switch+Tab never leave the dialog, Escape closes it, and when opened, the focus moves to an element in the dialog. There is a purpose in holding it: the rest of the page is locked, and a focus there would run empty.
After closing, the focus is back to the trigger
The second half is often forgotten. If the visitor closes the window, the focus should return to the element that opened the dialogue according to the dialogue pattern of the W3C. If this element is missing now, the focus comes on a point where the work continues meaningfully. Without this return, the focus ends up at the top of the page, and a read-out visitor again tabs through the menu and header area to where he was. For a newsletter note in the middle of the ordering process, this means: After closing, the focus is back on the field that was before.
The HTML element dialog handles the output by itself
The dialog element, opened with the showModal() method, brings the rules: Escape closes the window, the browser locks everything outside, and the focus goes to the first operable element when opened. With the attribute autofocus you specify which element this should be; MDN recommends the closing button if nothing else should be operated immediately. According to MDN, the element has been working in all popular browsers since March 2022. A self-built dialogue of div elements must recreate each of these rules by script, and this is where the traps arise: Escape without effect, tab that continues behind the window, no closing button.
If the output needs a special button
The criterion also allows outputs other than tab, arrow keys or escape, but then requires that the page indicates the way. The W3C shows it on a text editor in which Tab and Switch+Tab change the indent: A visible guide says that Alt+F10 brings the focus to the toolbar. There is rarely a reason for a dialogue window. A captioned closing button and the escape button cover each case, and both understand a visitor without guidance.
Examples
Dialogue with output and focus return
<div class="overlay">
<div role="dialog">
<p>Newsletter?</p>
<input type="email">
</div>
</div>
<!-- Escape ohne Wirkung, Tab läuft hinter dem Fenster weiter --><button id="oeffnen" onclick="nl.showModal()">Newsletter</button>
<dialog id="nl" onclose="oeffnen.focus()">
<p>Newsletter?</p>
<input type="email">
<button type="button" autofocus onclick="nl.close()">Schließen</button>
</dialog>showModal() locks the rest of the page and closes with Escape. The close event returns the focus to the button that opened the dialog.




