What is the purpose of the <label> element in HTML forms?
The <label> element connects text to a form control, making it easier for users to understand what input is expected and improving accessibility.
Click to reveal answer
beginner
How do you associate a <label> with an <input> using the for attribute?
Set the for attribute on the <label> to match the id of the <input>. This links the label to the input so clicking the label focuses the input.
Click to reveal answer
intermediate
Can a <label> wrap an <input> element? How does this affect association?
Yes, wrapping an <input> inside a <label> automatically associates them without needing the for attribute. Clicking the label focuses the input.
Click to reveal answer
beginner
Why is label association important for accessibility?
Screen readers use label associations to read out what input fields are for, helping users with disabilities understand and fill forms correctly.
Click to reveal answer
beginner
What happens if a form input has no associated label?
Users may find it hard to know what to enter, and assistive technologies may not describe the input properly, causing confusion and poor user experience.
Click to reveal answer
Which attribute on the <label> element links it to an <input>?
Afor
Bid
Cname
Dtype
✗ Incorrect
The for attribute on
What happens when you click on a label associated with a checkbox?
ANothing
BThe page reloads
CThe form submits
DThe checkbox toggles
✗ Incorrect
Clicking a label linked to a checkbox toggles the checkbox, improving usability.
Which of these is a valid way to associate a label with an input?
A. The input type should be 'username' instead of 'text'
B. The label's for attribute does not match the input's id
C. The label should wrap the input element
D. The input is missing a name attribute
Solution
Step 1: Compare label's for and input's id
Label uses for='username' but input's id='user', so they don't match.
Step 2: Understand association requirement
For label to link input, for and id must be identical.
Final Answer:
The label's for attribute does not match the input's id -> Option B
Quick Check:
for must equal id [OK]
Hint: for and id must be identical to link label and input [OK]
Common Mistakes:
Ignoring mismatch between for and id
Thinking name attribute affects label linking
Assuming wrapping input is always required
5. You want to create a form with two checkboxes labeled 'Subscribe' and 'Accept Terms'. Which code correctly associates each label with its checkbox for best accessibility?
hard
A. <label>Subscribe<input type='checkbox' id='subscribe'></label>
<label>Accept Terms<input type='checkbox' id='terms'></label>
B. <input type='checkbox'><label for='subscribe'>Subscribe</label>
<input type='checkbox'><label for='terms'>Accept Terms</label>
C. <label for='subscribe'>Subscribe</label><input type='checkbox'>
<label for='terms'>Accept Terms</label><input type='checkbox'>
D. <label><input type='checkbox' id='subscribe'>Subscribe</label>
<label for='terms'>Accept Terms</label>
<input type='checkbox'>
Solution
Step 1: Understand two ways to associate labels
Labels can link inputs by wrapping them or using for/id matching.
Step 2: Check each option for correct association
<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.
Final Answer:
<label>Subscribe<input type='checkbox' id='subscribe'></label>
<label>Accept Terms<input type='checkbox' id='terms'></label> -> Option A
Quick Check:
Wrap input inside label or match for/id [OK]
Hint: Wrap input in label or match for and id [OK]
Common Mistakes:
Using for without matching id
Placing label after input without for
Not associating label and input at all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.