<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:

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

Personal Information

Attributes of <legend> Tag:

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.