HTML & CSS Interview Question #39
What are fieldset and legend used for?
Forms, Validation, Metadata, Media, Responsive Images & Accessibility Junior Beginner
Quick Interview Answer
fieldset groups related form controls, while legend provides a caption for that group. They are especially useful for groups of radio buttons, checkboxes, or logically related fields.
Detailed Explanation
fieldset groups related form controls, while legend provides a caption for that group. They are especially useful for groups of radio buttons, checkboxes, or logically related fields.
This native structure improves form organization and helps assistive technologies communicate the relationship between controls and their group label.
Code Example
<fieldset>
<legend>Preferred contact method</legend>
<label>
<input type="radio"
name="contact"
value="email">
Email
</label>
<label>
<input type="radio"
name="contact"
value="phone">
Phone
</label>
</fieldset>