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.

Updated on 29-Jun-2020 09:05:05