Skip to main content

HTML Tags

HTML <fieldset> Tag

Written by Updated HTML <fieldset> Tag exercises

The fieldset tag groups related fields in a form, like "Personal information" and "Account details", and draws a box around them. Its legend names the group, and one disabled attribute on it can switch off every field inside at once.

The title of the group goes in the legend tag, as the first child. Screen readers then read that title together with each field in the group.

Syntax

html

<fieldset>
  <legend>Group Name</legend>
  <!-- Form elements go here -->
</fieldset>
AttributeWhat it does
disabledDisables every field inside, so none can be changed or sent.
formThe id of the form it belongs to, if it sits outside.
nameA name for scripts. It is not sent with the form.

Example

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Fieldset Example</title>
</head>
<body>
  <h2>Registration Form</h2>
  <form action="/samples/form_processor.php" method="post">
    <fieldset>
      <legend>Personal Information</legend>
      <label for="fname">First Name:</label>
      <input type="text" id="fname" name="fname" required><br><br>

      <label for="lname">Last Name:</label>
      <input type="text" id="lname" name="lname" required><br><br>

      <label for="email">Email:</label>
      <input type="email" id="email" name="email" required>
    </fieldset><br>

    <fieldset>
      <legend>Account Details</legend>
      <label for="username">Username:</label>
      <input type="text" id="username" name="username" required><br><br>

      <label for="password">Password:</label>
      <input type="password" id="password" name="password" required>
    </fieldset><br>

    <input type="submit" value="Register">
  </form>
</body>
</html>

The form shows two boxes, "Personal Information" with name and email fields, and "Account Details" with username and password, each with its title on the top border.

When to use a fieldset

  • Radio buttons and checkboxes that answer one question. This is where it matters most: "Yes" and "No" mean nothing without the question in the legend.
  • Sections of a long form, like shipping and billing addresses.
  • Fields that are switched on and off together, using disabled on the fieldset.

For a form with only two or three fields, you usually do not need one.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • Remove the default box with fieldset { border: 0; padding: 0; margin: 0; } and style the legend like a heading, if the design has no boxes.
  • Fieldsets can be nested, for a group inside a group, but keep it simple.

HTML <fieldset> Tag exercises

Group related fields

Group two text fields named first and last, and give the group the title Your name.

Result

Check your answer to see it here.