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

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.
Presence = focus.
input, textarea.
First wins.
Username focus.
.focus() method.
Use sparingly.
autofocusThe 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.
If multiple elements have autofocus, the browser typically focuses only the first one encountered in the HTML document.
Add the boolean autofocus attribute to a focusable form control:
<input type="text" autofocus>true; omit it for no auto-focus.input, select, textarea, and button.The autofocus attribute has a boolean value:
autofocus attribute, the browser typically focuses on the first one encountered in the HTML document.<!-- Boolean: presence enables focus -->
<input type="search" autofocus>
<!-- Equivalent in HTML -->
<input type="search" autofocus="">| Use Case | Element | Notes |
|---|---|---|
| Login username | <input type="text"> | Most common pattern |
| Site search | <input type="search"> | Search-focused pages |
| Comment box | <textarea> | Discussion threads |
| Dynamic focus | Any focusable element | Use element.focus() in JS |
| Multiple autofocus | — | Only first in document wins |
| Applicable elements | input, textarea, select, button | Focusable form controls |
| Element | Supported? | Notes |
|---|---|---|
<input> | Yes | Most common use |
<textarea> | Yes | Multi-line text |
<select> | Yes | Dropdown focus |
<button> | Yes | Less common |
<div>, <span> | No | Not focusable by default |
Login form with autofocus and dynamic JavaScript focus.
Login form with username field set to receive focus on load:
Let’s look at a simple example of how to use the autofocus attribute in an HTML form:
<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>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.
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:
<input type="text" id="dynamicField">
<script>
// Dynamically set focus for an input field
document.getElementById("dynamicField").focus();
</script>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.
<label> elements.Mark one focusable element with the boolean attribute.
Browser parses HTML and builds the DOM.
The autofocus element receives keyboard focus automatically.
No extra click or Tab key needed to start entering data.
The autofocus attribute is supported in all modern browsers. Test across browsers when autofocus is critical to your form flow.
All major browsers honor autofocus on form controls.
Bottom line: Use autofocus confidently in modern browsers, but test accessibility impact.
autofocus on the primary field of a focused taskelement.focus() when focus depends on runtime logicautofocus attribute sparingly to avoid overwhelming users with too many focused elements when a page loads.autofocus attribute may vary.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.
autofocusBookmark these before adding auto-focus to your forms.
Presence = focus.
Basicsinput, textarea.
ScopeFirst wins.
RuleDynamic focus.
JavaScriptUse sparingly.
Accessinput, textarea, select, and button elements.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.Practice the autofocus attribute with login form and dynamic focus examples in the Try It editor.
5 people found this page helpful