html
  1. html-legend-tag

HTML <legend> Tag

The <legend> tag is used in conjunction with the <fieldset> tag to define a caption or a title for the group of form elements inside the <fieldset> element.

Syntax

The basic syntax for using the <legend> tag is:

<fieldset>
  <legend>Caption goes here</legend>
  <!-- Form elements go here -->
</fieldset>

Example

<form>
  <fieldset>
    <legend>Login Information:</legend>
    <label>Username:</label>
    <input type="text" name="username"><br><br>
    <label>Password:</label>
    <input type="password" name="password"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>
Try Playground

The above example will display a form with a fieldset and a legend set as "Login Information".

Explanation

The <legend> tag is used to provide a caption for the <fieldset> element. It is used to group form elements together, and it indicates to the user what the group is for. The <legend> element must be the first child element of the <fieldset> element.

Use

  • The <legend> tag is mainly used to provide a caption for the <fieldset> element.
  • It is used to group related elements inside a form.

Important Points

  • A <fieldset> tag can have only one <legend> tag.
  • The <legend> tag must be the first child element of the <fieldset> tag.
  • The <legend> tag should not be used outside the <fieldset> tag.

Summary

The <legend> tag is used in conjunction with the <fieldset> tag to provide a caption for a group of related form elements. It is useful for creating well-structured forms with clearly labeled sections.

Published on: