HTML autofocus Attribute

Beginner
⏱️ 4 min read
📚 Updated: Jun 2026
🎯 2 Examples
Forms & Input

Introduction

The autofocus attribute is a useful feature in HTML that allows developers to specify whether an input field should automatically gain focus when a web page loads. This attribute can enhance user experience by directing the user’s attention to a specific input element upon page load.

What You’ll Learn

01

Boolean

Presence = focus.

02

Form Fields

input, textarea.

03

One Per Page

First wins.

04

Login Forms

Username focus.

05

JavaScript

.focus() method.

06

Accessibility

Use sparingly.

Purpose of autofocus

The primary purpose of the autofocus attribute is to designate the default focus of the page, reducing the need for users to manually select an input field. This is particularly beneficial for forms or pages where a specific input requires immediate attention—such as a login username field or a site search box.

💡
Use One autofocus Per Page

If multiple elements have autofocus, the browser typically focuses only the first one encountered in the HTML document.

📝 Syntax

Add the boolean autofocus attribute to a focusable form control:

autofocus.html
<input type="text" autofocus>

Syntax Rules

  • Boolean attribute—presence means true; omit it for no auto-focus.
  • Valid on input, select, textarea, and button.
  • Focus is applied when the page finishes loading.
  • Use on at most one element per page for predictable behavior.

💎 Values

The autofocus attribute has a boolean value:

  • autofocus (present) — When this attribute is present on an input element, it indicates that the element should automatically gain focus when the page loads. If multiple elements have the autofocus attribute, the browser typically focuses on the first one encountered in the HTML document.
autofocus-boolean.html
<!-- Boolean: presence enables focus -->
<input type="search" autofocus>

<!-- Equivalent in HTML -->
<input type="search" autofocus="">

⚡ Quick Reference

Use CaseElementNotes
Login username<input type="text">Most common pattern
Site search<input type="search">Search-focused pages
Comment box<textarea>Discussion threads
Dynamic focusAny focusable elementUse element.focus() in JS
Multiple autofocusOnly first in document wins
Applicable elementsinput, textarea, select, buttonFocusable form controls

Applicable Elements

ElementSupported?Notes
<input>YesMost common use
<textarea>YesMulti-line text
<select>YesDropdown focus
<button>YesLess common
<div>, <span>NoNot focusable by default

Examples Gallery

Login form with autofocus and dynamic JavaScript focus.

👀 Live Preview

Login form with username field set to receive focus on load:

Example

Let’s look at a simple example of how to use the autofocus attribute in an HTML form:

autofocus.html
<form>
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" autofocus>

  <label for="password">Password:</label>
  <input type="password" id="password" name="password">

  <input type="submit" value="Submit">
</form>
Try It Yourself

How It Works

In this example, the autofocus attribute is applied to the username input field. This means that when the page loads, the cursor will automatically be placed in the username field, ready for user input.

Dynamic Values with JavaScript

Similar to other attributes, you can dynamically set focus using JavaScript. This can be beneficial when you want to conditionally set focus based on user interactions or certain events. Here’s a brief example:

dynamic-focus.html
<input type="text" id="dynamicField">

<script>
  // Dynamically set focus for an input field
  document.getElementById("dynamicField").focus();
</script>
Try It Yourself

How It Works

In this script, the focus() method is called on an input field with the id dynamicField. This moves keyboard focus to that element programmatically, which is useful when focus depends on application logic rather than page load alone.

♿ Accessibility

  • Use sparingly — Unexpected focus changes can disorient screen reader users.
  • One primary field — Autofocus works best on the main task field (search, username).
  • Do not skip labels — Always pair focused fields with visible <label> elements.
  • Respect user context — Avoid autofocus on general content pages where users read before typing.
  • Mobile keyboards — Autofocus may open the virtual keyboard immediately; ensure that is intentional.

🧠 How autofocus Works

1

Author adds autofocus

Mark one focusable element with the boolean attribute.

Markup
2

Page finishes loading

Browser parses HTML and builds the DOM.

Load
3

Browser sets focus

The autofocus element receives keyboard focus automatically.

Focus
=

User can type immediately

No extra click or Tab key needed to start entering data.

Browser Support

The autofocus attribute is supported in all modern browsers. Test across browsers when autofocus is critical to your form flow.

HTML5 · Fully supported

Universal auto-focus

All major browsers honor autofocus on form controls.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
autofocus attribute 99% supported

Bottom line: Use autofocus confidently in modern browsers, but test accessibility impact.

💡 Best Practices

✅ Do

  • Use autofocus on the primary field of a focused task
  • Limit to one autofocus element per page
  • Pair with visible labels and logical tab order
  • Use element.focus() when focus depends on runtime logic
  • Test pages across different browsers

❌ Don’t

  • Autofocus multiple fields on the same page
  • Autofocus on long-form content or article pages
  • Rely on autofocus alone for accessibility
  • Surprise mobile users with an unwanted keyboard popup
  • Assume autofocus works inside hidden or disabled elements
  • Use the autofocus attribute sparingly to avoid overwhelming users with too many focused elements when a page loads.
  • Consider user accessibility; ensure that automatically focused elements don’t hinder the navigation experience for users who rely on screen readers or other assistive technologies.
  • Test your pages across different browsers to ensure consistent behavior, as browser support for the autofocus attribute may vary.

Conclusion

The autofocus attribute is a valuable tool for directing user attention to specific input fields on a web page. By strategically implementing this attribute, you can create a more intuitive and user-friendly experience for your website visitors.

Use it on a single, clearly labeled field where immediate typing is expected. When focus must be conditional, reach for JavaScript’s focus() method instead.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about autofocus

Bookmark these before adding auto-focus to your forms.

5
Core concepts
📝 02

Form Controls

input, textarea.

Scope
🔗 03

One Per Page

First wins.

Rule
⚙️ 04

.focus()

Dynamic focus.

JavaScript
05

A11y

Use sparingly.

Access

❓ Frequently Asked Questions

It automatically places keyboard focus on an element when the page loads, so the user can start typing right away.
input, textarea, select, and button elements.
The browser typically focuses only the first autofocus element in document order.
autofocus runs on page load. element.focus() lets you set focus at any time based on user actions or app state.
It can help on task-focused pages but may confuse screen reader users if used unexpectedly. Use on one primary field only.
Yes. On mobile it may also open the virtual keyboard immediately, so use it only when typing is the expected first action.

Guide users to the right field instantly

Practice the autofocus attribute with login form and dynamic focus examples in the Try It editor.

Try login form example →

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.

5 people found this page helpful