HTML autofocus attribute

HTML autofocus attribute: This attribute defines that the element should focus on when the pages load automatically. And this attribute is of boolean type. And this attribute has the elements like <button>,<input>, <select> and <textarea>.

HTML autofocus attribute

This attribute can be used for elements like <button>, <input>, <select> and <textarea>.

Syntax: <elementName autofocus>

Supported Browsers

This attribute supports different types of browsers as follows:

  • Chrome 5.0
  • Internet Explorer 10.0
  • Opera 9.6
  • Safari 5.0

Example 1: Button

<!DOCTYPE html>
<html>
<body>
<button type="button" autofocus>Click Me!</button>
<p><strong>Note:</strong> The autofocus attribute of the button tag is not supported in IE 9 and earlier versions.</p>
</body>
</html>

Output:

HTML autofocus attribute

 

Example 2: Input 

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  First name: <input type="text" name="fname" autofocus><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit">
</form>

<p><strong>Note:</strong> The autofocus attribute of the input tag is not supported in Internet Explorer 9 and earlier versions.</p>

</body>
</html>

Output:

HTML autofocus input attribute

Example 3: Textarea

<textarea autofocus>
At tutorials.freshersnow.com you will learn how to make a website. We offer free tutorials in all web development technologies. 
</textarea>

Output:

HTML autofocus textarea element