Learn HTML – Part 45 – legend Tag – Form Caption
The HTML legend tag is used to define a caption for a fieldset element.
It provides a title for grouped form elements, helping users understand the purpose of the section.
What is the legend Tag?
The legend element defines a caption for the fieldset element.
It appears as a title at the top of the grouped form fields.
Basic Syntax
<fieldset>
<legend>Section Title</legend>
Form elements here
</fieldset>
Example – legend with fieldset
<form>
<fieldset>
<legend>Personal Information</legend>
Name:<br>
<input type="text"><br><br>
Email:<br>
<input type="email">
</fieldset>
</form>
Output:
Another Example – Account Details
<fieldset>
<legend>Account Details</legend>
Username:<br>
<input type="text"><br><br>
Password:<br>
<input type="password">
</fieldset>
Output:
Why Use legend?
- Provides a clear title for grouped form fields
- Improves form structure
- Enhances accessibility for users and screen readers
- Makes forms easier to understand
The
legendtag provides a caption for thefieldsetelement in HTML forms.
Common Mistakes Beginners Make
- Using the legend tag outside fieldset
- Not giving meaningful titles
- Using long or unclear captions
Conclusion
The HTML legend tag provides a caption for grouped form elements inside a fieldset.
It improves form organization and helps users understand the purpose of each section.
What’s Next?
Now that you understand the legend tag, the next step is learning about the datalist tag, which provides predefined options for input fields.
Next: Learn HTML – Part 46 – datalist Tag – Input Suggestions →
Series: Learn HTML Series
