71 of 133 menu

The autofocus attribute

The autofocus attribute sets the default automatic input focus for HTML form fields when the page loads.

The element that has the autofocus attribute assigned will have focus when the page loads (the cursor will blink in it if you start entering text from the keyboard - it will fall into the field with input focus).

The attribute is an attribute without a value. It should be applied to the tags input or textarea and only one element per page, otherwise there will be a conflict.

Example

Let's look at the first tag when entering the page input which has the input focus, since we have given it the attribute autofocus (just don't click anywhere with the mouse, since the focus will disappear from the input):

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

:

See also

  • pseudo-class focus,
    which sets styles for the element with input focus
  • attribute tabindex,
    which allows you to change focus by pressing Tab
byenru