How to specify that the element should automatically get focus when the page loads in HTML?

The autofocus attribute in HTML is used to specify that an element should automatically receive focus when the page loads. This eliminates the need for users to manually click on the element before interacting with it, improving user experience and accessibility.

Syntax

Following is the syntax for using the autofocus attribute −

<input type="text" autofocus>
<button autofocus>Click Me</button>
<textarea autofocus></textarea>

The autofocus attribute is a boolean attribute, meaning it doesn't require a value. Its mere presence on an element indicates that the element should receive focus automatically.

Supported Elements

The autofocus attribute can be used with the following HTML elements −

  • <input> elements (text, password, email, etc.)

  • <textarea> elements

  • <button> elements

  • <select> elements

Using Autofocus with Input Elements

Example − Text Input with Autofocus

Following example demonstrates the autofocus attribute with a text input field −

<!DOCTYPE html>
<html>
<head>
   <title>Autofocus Input Example</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>User Registration Form</h2>
   <form>
      <label for="username">Username:</label><br>
      <input type="text" id="username" name="username" autofocus style="padding: 5px; margin: 10px 0;"><br>
      
      <label for="email">Email:</label><br>
      <input type="email" id="email" name="email" style="padding: 5px; margin: 10px 0;"><br>
      
      <input type="submit" value="Register" style="padding: 8px 16px; margin-top: 10px;">
   </form>
   <p><em>Note: The username field will automatically be focused when the page loads.</em></p>
</body>
</html>

When the page loads, the username input field will automatically have focus, allowing users to start typing immediately without clicking on the field first.

Using Autofocus with Button Elements

Example − Button with Autofocus

Following example shows how to use autofocus with a button element −

<!DOCTYPE html>
<html>
<head>
   <title>Autofocus Button Example</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Welcome to Our Website</h2>
   <p>Click the button below to get started, or press Enter since it's automatically focused.</p>
   <button type="button" autofocus onclick="alert('Welcome to TutorialsPoint!')" style="padding: 10px 20px; font-size: 16px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">Get Started</button>
   <p><em>The button above is automatically focused when the page loads.</em></p>
</body>
</html>

The button receives focus automatically, allowing users to press the Enter key to trigger the onclick event without first clicking the button.

Using Autofocus with Textarea

Example − Textarea with Autofocus

Following example demonstrates autofocus with a textarea element −

<!DOCTYPE html>
<html>
<head>
   <title>Autofocus Textarea Example</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Contact Us</h2>
   <form>
      <label for="name">Your Name:</label><br>
      <input type="text" id="name" name="name" style="padding: 5px; margin: 10px 0; width: 300px;"><br>
      
      <label for="message">Your Message:</label><br>
      <textarea id="message" name="message" rows="5" cols="40" autofocus placeholder="Enter your message here..." style="padding: 8px; margin: 10px 0; font-family: Arial, sans-serif;"></textarea><br>
      
      <input type="submit" value="Send Message" style="padding: 8px 16px; margin-top: 10px;">
   </form>
</body>
</html>

The textarea will automatically receive focus when the page loads, allowing users to immediately start typing their message.

Autofocus Behavior With Autofocus ? Element gets focus ? Cursor appears ? Ready for input ? No click needed ? Enter key works Without Autofocus ? No automatic focus ? User must click ? Extra step required ? Less user-friendly ? Enter key inactive

Important Considerations

When using the autofocus attribute, keep the following points in mind −

  • Only one element should have the autofocus attribute per page. If multiple elements have autofocus, the first one in the document order receives focus.

  • Accessibility − Screen readers and assistive technologies are notified when an element automatically receives focus, which can be helpful for accessibility.

  • User experience − Use autofocus thoughtfully. It can be helpful for search boxes or primary input fields, but may be disruptive if used inappropriately.

  • Mobile devices − On some mobile devices, autofocus might trigger the virtual keyboard to appear automatically.

Browser Compatibility

The autofocus attribute is supported in HTML5 and works in all modern browsers including Chrome, Firefox, Safari, Edge, and Internet Explorer 10+. It provides a simple way to improve user experience by reducing the number of clicks required to interact with key page elements.

Conclusion

The autofocus attribute is a simple yet effective way to enhance user experience by automatically focusing on important form elements when a page loads. Use it strategically on primary input fields, search boxes, or call-to-action buttons to streamline user interactions and improve accessibility.

Updated on: 2026-03-16T21:38:53+05:30

326 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements