noAutofocus (HTML)
Summary
Section titled βSummaryβ- Rule available since:
v2.4.0 - Diagnostic Category:
lint/a11y/noAutofocus - This rule is recommended, meaning it is enabled by default.
- This rule has an unsafe fix.
- The default severity of this rule is error.
- Sources:
- Inspired from
jsx-a11y/no-autofocus
- Inspired from
How to configure
Section titled βHow to configureβ{ "linter": { "rules": { "a11y": { "noAutofocus": "error" } } }}Description
Section titled βDescriptionβEnforce that the autofocus attribute is not used on elements.
Autofocusing elements can cause usability issues for sighted and non-sighted users, alike.
However, the autofocus attribute is allowed on elements inside a dialog element or
elements with a popover attribute, as these are modal contexts where autofocus is expected.
Examples
Section titled βExamplesβInvalid
Section titled βInvalidβ<input autofocus />code-block.html:1:8 lint/a11y/noAutofocus FIXABLE ββββββββββββββββββββββββββββββββββββββββββββββββ
β This element uses the autofocus attribute outside an allowed modal context.
> 1 β <input autofocus />
β ^^^^^^^^^
2 β
βΉ Autofocusing elements can disrupt navigation and confuse screen reader and keyboard users.
βΉ Unsafe fix: Remove the autofocus attribute.
1 β <inputΒ·autofocusΒ·/>
β ----------
<input autofocus="true" />code-block.html:1:8 lint/a11y/noAutofocus FIXABLE ββββββββββββββββββββββββββββββββββββββββββββββββ
β This element uses the autofocus attribute outside an allowed modal context.
> 1 β <input autofocus="true" />
β ^^^^^^^^^^^^^^^^
2 β
βΉ Autofocusing elements can disrupt navigation and confuse screen reader and keyboard users.
βΉ Unsafe fix: Remove the autofocus attribute.
1 β <inputΒ·autofocus="true"Β·/>
β -----------------
<textarea autofocus>content</textarea>code-block.html:1:11 lint/a11y/noAutofocus FIXABLE βββββββββββββββββββββββββββββββββββββββββββββββ
β This element uses the autofocus attribute outside an allowed modal context.
> 1 β <textarea autofocus>content</textarea>
β ^^^^^^^^^
2 β
βΉ Autofocusing elements can disrupt navigation and confuse screen reader and keyboard users.
βΉ Unsafe fix: Remove the autofocus attribute.
1 β <textareaΒ·autofocus>content</textarea>
β ---------
<input /><div popover><input autofocus /></div><dialog><input autofocus /></dialog>Resources
Section titled βResourcesβ- WHATWG HTML Standard, The autofocus attribute
- The accessibility of HTML 5 autofocus
- MDN Web Docs, HTMLElement: autofocus property
Related links
Section titled βRelated linksβCopyright (c) 2023-present Biome Developers and Contributors.