References

Beginner-friendly references for web development, with live, editable examples.

The HTML autofocus attribute

Global attribute Works on every element All modern browsers Updated
Quick answer

The HTML autofocus attribute makes the browser move keyboard focus to the element automatically when the page loads. It is a boolean global attribute — only one element per page should use it, and you should use it sparingly because unexpected focus can disorient users.

Overview

Adding autofocus tells the browser to focus this element as soon as the page is ready. Historically it only worked on form controls; in modern HTML it is a global attribute that works on any focusable element.

Only one element per document should have autofocus — if several do, the browser focuses the first. Use it where focus is genuinely expected (such as the single search box on a search page) and avoid it on content-heavy pages, where jumping focus past the heading and navigation can confuse keyboard and screen-reader users.

Values

Value
autofocus (boolean)

Example

Live example
<input type="text" autofocus placeholder="I receive focus on load" style="padding:8px; width:100%;">

Best practices

  • Use autofocus sparingly — auto-focusing can disorient screen-reader users.
  • Use inert to disable everything behind a modal so focus cannot reach it.
  • Only one element per page should have autofocus.
  • Pair these with deliberate focus management for accessible dialogs and overlays.

Accessibility

Automatic focus moves the user's starting point without their action, which can be disorienting for screen-reader and keyboard users — they may miss the page heading and skip links entirely. Only auto-focus when a single field is the obvious task, ensure the element has a clear label, and never auto-focus content that scrolls the page unexpectedly.

Frequently asked questions

What does the autofocus attribute do?
Automatically moves keyboard focus to the element when the page loads.
Should I use autofocus?
Sparingly. It can move focus unexpectedly for keyboard and screen-reader users; reserve it for pages whose sole purpose is that input.
What does the inert attribute do?
It makes an element and its subtree non-interactive, unfocusable and ignored by assistive technology — ideal for content behind a modal.
Is autofocus a global attribute?
Yes — it is a global attribute, so it can be set on any HTML element (it is a global attribute).