HTML <button> autofocus Attribute

HTMLWeb DevelopmentFront End Technology

The autofocus attribute of the <button> element is used to set focus to the button whenever page loads.

Following is the syntax −

<button type="button" autofocus>

Above, we have set autofocus to a button. Let us now see an example to implement the autofocus attribute of the <button> element −


 Live Demo

<!DOCTYPE html>
   <h2>Demo Heading</h2>
   <p>This is a demo line.</p>
   <button type="button" autofocus >Demo</button>


This will produce the following output displaying the focus is on button 1 −

In the above example, we have set the autofocus attribute for the button element −

<button type="button" autofocus >

Now, when the page loads, the focus would be on the button itself and the same would appear to the visitor on the web page.

Published on 20-Jun-2019 11:50:49