HTML button Tag autofocus Attribute Tutorial

In this section, we will learn what autofocus attribute is and how it works.

What is autofocus Attribute in HTML <button> Tag?

The autofocus attribute is used to set the focus on the target button when the page is loaded.

This means after the page is loaded, if we hit a space key on the keyboard, the target button will be executed because it has the focus on.

HTML autofocus Attribute in <button> Tag Syntax:

<button autofocus> </button>

<button> Tag autofocus Attribute Values

The autofocus attribute does not take a value. All we need to do is to set this attribute in the opening tag of the HTML <button> element.

Example: using autofocus attribute in HTML <button> tag

See the Pen using autofocus attribute in HTML <button> tag by Omid Dehghan (@odchan1) on CodePen.

Leave a Reply