HTML <legend> Tag
The <legend> tag defines a caption or title for a <fieldset> element. It's used to group related form elements and provide a descriptive label for the group. The legend appears at the top of the fieldset border.
Fieldset Structure
The <legend> tag must be placed as the first child of a <fieldset> element. It provides a semantic title for the grouped form controls. This improves accessibility and helps users understand the form structure.
Form Organization
Use fieldset and legend to organize complex forms into logical sections. For example, group contact information, shipping address, and payment details separately. This makes forms more accessible and easier to navigate.
Accessibility Benefits
The legend appears as a title within the fieldset border. Screen readers announce the legend when users navigate to the fieldset, improving accessibility for form navigation. This helps users understand which form elements belong together.
Best Practices
Always place the legend as the first child of the fieldset. Use descriptive text that clearly identifies the group of form elements. Use fieldset and legend for complex forms with multiple sections, not for simple single-field forms.
