Rule
Autofocus on load
autofocus disorients assistive-technology users.
- Rule
no-autofocus- Type
- Best practice, not a WCAG requirement. It runs when Best practices is switched on.
- Severity
- Minor
What it checks
The rule looks at every element with an autofocus attribute. Inside a <dialog>, a role="dialog" or role="alertdialog" element, or a popover, it passes: there the attribute only picks the control that receives focus when the container opens, which is the intended use.
Elsewhere, autofocus moves focus as soon as the page loads. Screen reader and magnifier users can land part way down the page and miss everything before the focused control.
When a person decides
Outside a dialog or popover the rule never fails, it asks for review. The HTML specification allows autofocus when the control is the main purpose of the page, such as the search box on a search page, and markup cannot show whether that is so.
Example
Sent to review
<h1>Contact us</h1>
<label for="name">Your name</label>
<input id="name" autofocus>
Passes
<dialog open aria-labelledby="rename-title">
<h2 id="rename-title">Rename file</h2>
<label for="new-name">New name</label>
<input id="new-name" autofocus>
</dialog>
The engine is tested against both examples.
How to fix it
Remove autofocus unless the control is the reason the page exists, and let people reach it in document order.
Check your own pages
This rule runs in every pour audit: the browser extension, the bookmarklet, the command line and the editor. Get pour, free.