The autofocusglobal attribute is a boolean attribute. When present, the element should receive focus on page load — or, if it sits inside a <dialog> or a popover, when that dialog or popover is shown.
It applies to any element (not only form controls), including contenteditable regions. Common uses are search boxes and the first field of a short login form.
Baseline · Widely available.
Not deprecated and not experimental. Widely available across browsers since about March 2023 (dialog/popover timing included in modern engines).
Accessibility caution.
MDN advises careful use. Autofocus can “teleport” screen-reader users, scroll the page, and open a mobile keyboard before people see surrounding context. Prefer it only when focus is clearly expected.
Syntax
HTML Syntax
syntax.html
<element autofocus>
Boolean attribute: write autofocus alone (or autofocus="autofocus"). Presence means true; omit it for false.
Values
Attribute Values
autofocus has no keyword list — only whether it is present:
Value
Description
Try it
autofocus (present)
Focus this element on load, or when its dialog/popover opens
Put autofocus on the control inside the dialog, not only on the page root.
Call showModal() (or show()) when the user opens it.
Focus moves to the input as the dialog appears — matching the HTML autofocus rule for dialogs.
One autofocus per dialog is enough; extra ones compete and confuse.
Edge Cases & Pitfalls
Most issues are accessibility or competing focus sources.
a11y
Sudden focus skips context
Screen readers may jump straight to the control. Sighted users on small screens may miss headings above the fold.
hash
URL fragments win
If the address has #id pointing at an element, that target generally gets focus instead of the autofocus control.
count
Only one per document or dialog
Multiple autofocus attributes mean the first focusable one wins. Prefer a single intentional target.
mobile
Virtual keyboard may open
Focusing a text field on load can pop the on-screen keyboard and cover content. Confirm that is desirable.
focusable
Disabled controls cannot take focus
If the element is not focusable (for example disabled), autofocus has no useful effect — focus moves to the next candidate or nowhere.
Compatibility
Browser Support
autofocus is an HTML global attribute for focus behavior in the browser. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
autofocus
Widely available since about March 2023. Form-control autofocus is long-standing; dialog/popover timing is supported in modern browsers.
96%Global support (approx.)
Google ChromeModern · Full
Full support
Mozilla FirefoxModern · Full
Full support
Apple SafariModern · Full
Full support
Microsoft Edge79+
Full support
OperaModern · Full
Full support
Samsung InternetModern · Full
Full support
Internet ExplorerPartial (form controls)
Partial
autofocusWidely supported
Bottom line: Safe to use as progressive enhancement. Always weigh accessibility cost against the convenience of auto-focus.
Remember
Key Takeaways
Role: boolean global attribute that focuses an element on load or when its dialog/popover opens.
One target: only one autofocus per document or dialog.
Use sparingly: search and short forms; avoid surprise focus on content-heavy pages.
Fragments override: a matching URL #id usually wins over autofocus.
One line:autofocus saves a click when focus is expected — and confuses people when it is not.
Frequently Asked Questions
It is a boolean global attribute that focuses the element on page load, or when a dialog or popover that contains it is shown.
Any element, not only form controls. It is often used on input, textarea, select, and button, and can also apply to contenteditable areas.
Only one element per document, dialog, or popover should have it. If several do, the first focusable one receives focus.
Use it sparingly. MDN warns that sudden focus can confuse screen-reader users, scroll the page, and open mobile keyboards without context.
Yes. If the URL has a fragment that identifies an element, that target generally receives focus instead of the autofocus control.
autofocus is declarative HTML for load or dialog/popover show. element.focus() is JavaScript you call later, for example after a click or validation error.