<legend> Tag in HTML
The <legend> tag in HTML represents a caption or title for the content of a <fieldset>
element. It provides a way to describe the purpose of the grouped content and helps in improving the accessibility and semantics of the form.
Key Points on <legend> Tag:
- The
<legend>
tag is used to define a heading for a<fieldset>
element. - It helps users understand the content or purpose of the grouped form elements.
- The
<legend>
element must be placed directly inside a<fieldset>
element. - It is often used for accessibility, providing better screen reader support for grouped form controls.
Syntax of <legend> Tag:
Syntax Example
<fieldset>
<legend>Legend Text</legend>
</fieldset>
Example of <legend> Tag in HTML:
This example shows the use of the <legend>
tag inside a <fieldset>
element.
Code Example
<fieldset>
<legend>Personal Information</legend>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<br>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
</fieldset>
Output
Attributes of <legend> Tag:
- align: Specifies the alignment of the legend text (left, right, center).
The <legend>
tag helps group related form elements under a title, improving form structure and accessibility. It should always be placed as the first child within a <fieldset>
element for correct behavior.