- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML <legend> Tag
HTML Tags
HTML <legend> Tag
The legend tag is the title of a fieldset: "Personal details", "Delivery address", "Payment method". It sits on the fieldset's border, and screen readers read it before each field in the group.
That is especially important for radio buttons and checkboxes. A group of radio buttons labelled "Yes" and "No" makes no sense without the question, and the legend is the question. See the fieldset tag for the group itself.
Syntax
html
<fieldset>
<legend>Group Title</legend>
<!-- Form controls go here -->
</fieldset>It has no attributes of its own today; the old align attribute is obsolete. Global attributes such as id and class work.
Example
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Legend Example</title>
</head>
<body>
<h2>Survey Form</h2>
<form action="/samples/form_processor.php" method="post">
<fieldset>
<legend>Personal Details</legend>
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" required>
</fieldset><br>
<fieldset>
<legend>Preferences</legend>
<label for="color">Favorite Color:</label>
<input type="text" id="color" name="color"><br><br>
<label for="hobby">Hobby:</label>
<input type="text" id="hobby" name="hobby">
</fieldset><br>
<input type="submit" value="Submit">
</form>
</body>
</html>The form has two boxes, "Personal Details" and "Preferences", each with its title written into the top border.
A typical use
A legend asks the question for a radio group
html
<fieldset>
<legend>Do you want a newsletter?</legend>
<label><input type="radio" name="news" value="yes"> Yes</label>
<label><input type="radio" name="news" value="no"> No</label>
</fieldset>A screen reader reaching "Yes" says "Do you want a newsletter? Yes, radio button".
Browser Support
Chrome | Edge | Firefox | Safari | Opera | IE9+ |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
Good to know
- The
<legend>must be the first child of the<fieldset>. - Keep it short; it is repeated for every field in the group.
- Styling the legend's position on the border is tricky. Many designs remove the border with
fieldset { border: 0; }and style the legend like a heading.
HTML <legend> Tag exercises
Name a fieldset
Make a group of fields titled Contact that holds an email field named email.
Result
