Q42: What is the autofocus attribute and why should it be used with caution?
The autofocus attribute directs browser focus to a specific form element upon page load. It must be used cautiously because automatically moving focus can disorient screen reader users, scroll the page unexpectedly, or trigger mobile virtual keyboards unexpectedly.
Input Autofocus Usage
<form>
<label for="search">Search Query:</label>
<input type="text" id="search" autofocus>
</form>Placing autofocus automatically places cursor focus inside the search box upon loading.
Key Takeaways
Automatically moves input cursor focus upon page load.
May disrupt accessibility flow for screen reader users if unexpected.
Knowledge Check
Why can auto-focusing inputs automatically create accessibility issues?