⌨️ User Can Type Immediately
Search page? Login form? User must click input first. autofocus selects input automatically. One less click.
📝 Basic Usage
<!-- Search page --> <input type="search" name="q" autofocus placeholder="Search..." "> <!-- Login page --> <input type="email" name="email" autofocus placeholder="Email"> <!-- Modal dialog --> <input type="text" name="name" autofocus> <!-- Works on any focusable element --> <button autofocus>OK</button> <textarea autofocus></textarea>
✅ Best Practices
- Only ONE element per page (last one wins)
- Don’t autofocus on mobile (keyboard covers screen)
- Use on pages where primary action is typing
- Works on page load and dynamic content
- Respects user’s focus if they clicked elsewhere
“Google homepage: cursor in search box automatically. Added autofocus to my search page. Same experience. Users can type immediately. Simple but impactful.”
