HTML Fieldset and Legend: Group Form Controls
A meaningful group
Use fieldset to group related controls. Its legend supplies a group caption and helps assistive technologies communicate the shared question. Individual choices still need their own labels.
<fieldset> <legend>Preferred format</legend> <label> <input type="radio" name="format" value="text"> Text </label> <label> <input type="radio" name="format" value="audio"> Audio </label></fieldset>The group asks Preferred format; each radio has its own label.
Separate groups
A form can contain several fieldsets. Each legend identifies its own topic. Radio groups are still determined by name, form owner and tree, not by fieldset boundaries, so unrelated radio groups need different names.
<form> <fieldset> <legend>Delivery</legend> <label> <input type="radio" name="delivery" value="pickup" checked> Pickup </label> <label> <input type="radio" name="delivery" value="mail"> Mail </label> </fieldset> <fieldset> <legend>Updates</legend> <label> <input type="checkbox" name="updates" value="yes"> Email updates </label> </fieldset></form>Two distinct group captions organize the form; delivery=pickup is initially included.
Disable a group
The disabled attribute on fieldset makes most descendant controls unavailable for interaction and excludes them from form data. Controls inside its first legend child are an exception. This is useful when an option group is not applicable.
<form> <fieldset disabled> <legend>Optional details</legend> <label> Code <input name="code" value="A5"> </label> </fieldset> <label> Title <input name="title" value="Plan"> </label></form>Code is disabled and omitted; Title remains editable and included.
Common mistakes and practice
Place legend as the first child element of the fieldset. It names the group, not each individual input. A disabled fieldset disables descendant form controls except those inside its first legend element child. Do not use fieldset merely to draw borders; choose it when controls form a meaningful group.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Fieldset and Legend: Group Form Controls — Knowledge Check
Apply the form-control concepts from this lesson.
Which element supplies a caption for a fieldset?
What determines whether two radio controls in the same form and tree belong to the same group?
A control outside the first legend is inside a disabled fieldset. What happens?
A disabled fieldset contains a control inside its first legend. What is the special rule?
Why group a set of related radio choices with fieldset and legend?