The HTML <legend> Tag

Definition and Usage

<legend> Tag Definition <fieldset> Element The title (caption).

See also:

HTML DOM Reference Manual:Legend Object

Instance

Example 1

Group related elements in a form:

<form action="/action_page.php">
  <fieldset>
    <legend>Personal Information:</legend>
    <label for="fname">First Name:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">Last Name:</label>
    <input type="text" id="lname" name="lname"><br><br>
    <label for="email">Email:</label>
    <input type="email" id="email" name="email"><br><br>
    <label for="birthday">Birthday:</label>
    <input type="date" id="birthday" name="birthday"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

Try It Yourself

Example 2

Let the fieldset title float to the right (using CSS):

<form action="/action_page.php">
  <fieldset>
    <legend style="float:right">Personal Information:</legend>
    <label for="fname">First Name:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">Last Name:</label>
    <input type="text" id="lname" name="lname"><br><br>
    <label for="email">Email:</label>
    <input type="email" id="email" name="email"><br><br>
    <label for="birthday">Birthday:</label>
    <input type="date" id="birthday" name="birthday"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

Try It Yourself

Example 3

Using CSS to set <fieldset> And <legend> Styles:

<html>
<head>
<style>
fieldset {
  background-color: #eeeeee;
}
legend {
  background-color: gray;
  color: white;
  padding: 5px 10px;
}
input {
  margin: 5px;
}
</style>
</head>
<body>
<form action="/action_page.php">
  <fieldset>
    <legend>Personal Information:</legend>
    <label for="fname">First Name:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">Last Name:</label>
    <input type="text" id="lname" name="lname"><br><br>
    <label for="email">Email:</label>
    <input type="email" id="email" name="email"><br><br>
    <label for="birthday">Birthday:</label>
    <input type="date" id="birthday" name="birthday"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>
</body>
</html>

Try It Yourself

Global Attributes

<legend> The tag also supports Global Attributes in HTML.

Event Attributes

<legend> The tag also supports Event Attributes in HTML.

Default CSS Settings

Most browsers will display the following default values <legend> Element:

legend {
  display: block;
  padding-left: 2px;
  padding-right: 2px;
  border: none;
}

Try It Yourself

Browser Support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support