Performance: Label association
Label association affects user interaction speed and accessibility, impacting input responsiveness and visual stability.
<label for="name">Name</label><input type="text" id="name">
<label>Name</label><input type="text" id="name">
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Label without 'for' attribute | Minimal | 0 | 0 | [!OK] |
| Label with 'for' attribute linked to input | Minimal | 0 | 0 | [OK] Good |
<label> element in HTML forms?<label> with an <input> element using the for attribute?for and idfor attribute must match the input's id to associate them.<label for='name'>Name</label><input type='text' id='name'> uses for='name' on label and id='name' on input, correctly linking them.<label for='email'>Email</label> <input type='email' id='email' name='email'>
<label for='username'>Username</label> <input type='text' id='user' name='username'>
<label>Subscribe<input type='checkbox' id='subscribe'></label>
<label>Accept Terms<input type='checkbox' id='terms'></label> wraps each input inside its label, ensuring correct association without needing for/id.Pass the test and this pattern moves unknown