HTML autofocus Attribute

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The autofocus global 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.

HTML Syntax

syntax.html
<element autofocus>

Boolean attribute: write autofocus alone (or autofocus="autofocus"). Presence means true; omit it for false.

Attribute Values

autofocus has no keyword list — only whether it is present:

ValueDescriptionTry it
autofocus (present)Focus this element on load, or when its dialog/popover opensTry it
OmittedNo automatic focus from this attributeTry it
Inside <dialog>Focus when the dialog is shown (not only on first page load)Try it
Multiple on one pageInvalid pattern — the first focusable autofocus element winsTry it

Live Preview

Open the dialog. The nested input has autofocus, so it should receive focus when the dialog appears (same idea as MDN’s dialog/popover rule).

Click Open dialog. Focus moves to the autofocused field inside.

Dialog demo

Dialog closed.

Quick search

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor. Reload the editor preview to see page-load focus.

Example 2 — First Field of a Login Form

Focus the username once — never put autofocus on two fields in the same form.

HTML
<form action="/login" method="post">
  <label for="user">Username:</label>
  <input type="text" id="user" name="username" autocomplete="username" autofocus>

  <label for="pass">Password:</label>
  <input type="password" id="pass" name="password" autocomplete="current-password">

  <button type="submit">Sign in</button>
</form>
Try It Yourself

How It Works

  1. One autofocus per document (or per dialog). Here only the username has it.
  2. Users can type immediately without an extra click or Tab.
  3. Pair with autocomplete so password managers still understand the fields.
  4. Password stays unfocused until Tab — avoids flashing a password field’s keyboard first on some phones.

Example 3 — Focus When a Dialog Opens

Inside a <dialog>, autofocus runs when the dialog is shown — ideal for prompt-style UIs.

HTML
<button type="button" id="open">Rename</button>

<dialog id="rename">
  <form method="dialog">
    <label for="title">New title:</label>
    <input type="text" id="title" name="title" autofocus>
    <button type="submit">Save</button>
  </form>
</dialog>

<script>
  const dialog = document.getElementById("rename");
  document.getElementById("open").addEventListener("click", () => {
    dialog.showModal();
  });
</script>
Try It Yourself

How It Works

  1. Put autofocus on the control inside the dialog, not only on the page root.
  2. Call showModal() (or show()) when the user opens it.
  3. Focus moves to the input as the dialog appears — matching the HTML autofocus rule for dialogs.
  4. 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.

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 Chrome Modern · Full
Full support
Mozilla Firefox Modern · Full
Full support
Apple Safari Modern · Full
Full support
Microsoft Edge 79+
Full support
Opera Modern · Full
Full support
Samsung Internet Modern · Full
Full support
Internet Explorer Partial (form controls)
Partial
autofocus Widely supported

Bottom line: Safe to use as progressive enhancement. Always weigh accessibility cost against the convenience of auto-focus.

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.

Next: autoplay

Start media playback automatically — with its own accessibility trade-offs.

autoplay tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful