totn HTML

HTML: <legend> tag

This HTML tutorial explains how to use the HTML element called the <legend> tag with syntax and examples.

Description

The HTML <legend> tag is used to generate a caption for the <fieldset> tag when grouping related items in an HTML form. This tag is also commonly referred to as the <legend> element.

Syntax

In HTML, the syntax for the <legend> tag is: (example creates a Customer Information caption for the grouped items)

<body>
<form>
  <fieldset>
    <legend>Customer Information</legend>
    Name: <input type="text"><br>
    Address: <input type="text"><br>
    Phone Number: <input type="text">
  </fieldset>
</form>
</body>

Sample Output


Attributes

Only the Global Attributes apply to the <legend> tag. There are no attributes that are specific to the <legend> tag.

Note

  • The HTML <legend> element is found within the <body> tag. It is used to provide a caption for the related items grouped in the <fieldset> tag.
  • The caption generated by the <legend> tag will be rendered before the related form controls in the <fieldset> and appear to float over the top border of the <fieldset>.

Browser Compatibility

The <legend> tag has basic support with the following browsers:

  • Chrome
  • Android
  • Firefox (Gecko)
  • Firefox Mobile (Gecko)
  • Internet Explorer (IE)
  • Edge Mobile
  • Opera
  • Opera Mobile
  • Safari (WebKit)
  • Safari Mobile

Example

We will discuss the <legend> tag below, exploring examples of how to use the <legend> tag in HTML5, HTML 4.01 Transitional, XHTML 1.0 Transitional, XHTML 1.0 Strict, and XHTML 1.1.