HTML Attributes
HTML autofocus Attribute
The autofocus attribute makes a form control receive focus automatically when the page loads.
The autofocus Attribute
autofocus is a boolean global attribute most useful on form controls such as <input>, <textarea>, <select> and <button>. When present, the element automatically receives keyboard focus as soon as the page finishes loading, so the user can start typing without clicking first.
Syntax
Syntax: <input autofocus>. Being boolean, it takes no value. Only one element per page should have autofocus; if several do, the browser focuses the first.
<form action="/search">
<input type="search" name="q" placeholder="Search internships" autofocus>
</form>| Value | Description |
|---|---|
| autofocus | Present: the element is focused automatically on page load |
| (omitted) | Absent: no automatic focus is applied |
Use autofocus carefully. Automatically moving focus can disorient screen-reader users and cause the page to scroll unexpectedly on mobile. Avoid it on pages where the main content should be read first.
