HTML autofocus Global Attribute
Description
The autofocus attribute is a boolean global attribute that can be applied to certain HTML elements to indicate that the element should automatically receive focus when the page loads. Focus, in this context, means that the element becomes active and ready for user input, such as a text field being ready to type into or a button being ready to be clicked via keyboard.
Key Points:
Boolean Attribute:
autofocus does not require a value. Simply including the attribute in an element is enough to enable the behavior:
<input type="text" autofocus>
Default Behavior:
When a page loads, the browser will attempt to focus on the first element with the autofocus attribute. If multiple elements have autofocus, only the first one in the document order will be focused.
Supported Elements:
While it is a global attribute, autofocus has meaningful behavior primarily on interactive form-related elements, including:
<input>(text, email, password, etc.)<textarea><select><button><a>(if it has atabindexattribute)
Focus Interaction:
The autofocus attribute does not override user actions. If the user has already interacted with the page (for example, clicked elsewhere or switched tabs), the element may not automatically receive focus.
Accessibility Considerations: Automatically focusing an element can improve usability for forms by guiding users to the first input. However, overuse can be disruptive for keyboard or screen reader users, so it should be applied judiciously.
Example Usage:
<form>
<label for="username">Username:</label>
<input id="username" type="text" name="username" autofocus>
<label for="password">Password:</label>
<input id="password" type="password" name="password">
<button type="submit">Login</button>
</form>
In this example, the cursor automatically appears in the "Username" input field when the page loads, ready for the user to type.
Syntax
<tagname autofocus>
Values
- autofocusHTML allows writing
autofocusautofocus="autofocus"or evenautofocus="". These are all equivalent to just including the attribute.
Example
Browser Support
The following information will show you the current browser support for the HTML autofocus global attribute. Hover over a browser icon to see the version that first introduced support for this HTML global attribute.
This global attribute is supported by all modern browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 26th December 2025
