HTML autofocus Attribute
Last Updated :
26 Mar, 2024
The HTML autofocus attribute is a powerful tool that enhances user experience by automatically setting the focus to a specific element when a webpage loads. This attribute is boolean, meaning it can either be present or absent.
Syntax:
<elementName autofocus>
Supported Tags
Examples of HTML autofocus Attribute
Example 1: This example uses the autofocus attribute to create the focus on the first name while loading the page for the first time.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML autofocus Attribute</title>
</head>
<body>
<h2>HTML autofocus Attribute</h2>
<form>
<label
>First Name:
<input
type="text"
autofocus
placeholder="Enter Name"
/>
</label>
</form>
</body>
</html>
Output:
Example 2: This example we sets autofocus on a textarea element, focusing on it when the page loads. It displays text suggesting it’s a computer science portal for geeks.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML autofocus Attribute</title>
</head>
<body>
<h2>HTML autofocus Attribute</h2>
<textarea rows="3" cols="30" autofocus>
A computer science portal for geeks.
</textarea
>
</body>
</html>
Output:
HTML autofocus Attribute example output
Supported Browsers:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...