HTML Fieldset Tag

HTML <fieldset> tag groups the logically related fields/labels in an HTML form.

Using this tag is optional while creating an HTML form, but using the <fieldset> tag makes it easier to understand the purpose of the grouped elements of the form.

The <legend> tag is used with the <fieldset> element as a first child to define the caption for the grouped related fields.

This combination of <fieldset> tag and <legend> tag helps organize and structure the form, which makes it more understandable and accessible to users.

Syntax of <fieldset> Tag

The above syntax contains :

  • <fieldset>: It is the starting tag of the <fieldset> element.
  • </fieldset>: It is the closing tag of the <fieldset> element.

The form elements are placed between <fieldset> tag.

Syntax of <legend> Tag

The above syntax contains :

  • <legend>: It is the starting tag of the <legend> element.
  • </legend>: It is the closing tag of the <legend> element.

The caption is written inside the opening tag and closing tag of the <legend> element.

Examples of the <fieldset> Tag

Example 1:

We create a "User basic information" group using the <fieldset> tag in this example.

Code:

Output:

As you can see below, the group name is displayed, and a boundary line is drawn around the grouped elements.

HTML fieldset tag

Example 2

In this example, we are creating two groups, which are named "Contact Information" and "Address Information", using the <fieldset> tag.

Code:

Output:

As you can see below, a boundary line is drawn around the two groups.

HTML fieldset tag

Example 3

In this example, we are creating two groups, which are named "Product Details" and "Shipping Details", using the <fieldset> tag.

Code:

Output:

As you can see below, a boundary line is drawn around the two groups.

HTML fieldset tag

Example 4

In this example, we are creating a group named "Your Information" and turning off all form controls within the <fieldset> tag using the "disabled" attribute.

Code:

Output:

As you can see below, a boundary line is drawn around a group, and all form controls have been turned off.

HTML fieldset tag

Attributes

Tag-specific attribute:

AttributeValueDescription
DisabledDisabledIt specifies that all form controls within the <fieldset> tag are disabled.
Formform_idIt specifies which form <fieldset> is when the form contains multiple fieldsets.
NameTextIt specifies the name associated with the <fieldset>. It will not display in the browser and is useful with JS.

Global attributes

HTML <fieldset> tag supports the global attributes in HTML.

Event attributes

HTML <fieldset> tag supports the event attributes in HTML.

Browser Support

Below are the browsers that support the <fieldset> tag:

  • Safari
  • Google Chrome
  • Internet Explorer
  • Firefox
  • Microsoft Edge
  • Opera

Conclusion

In this article, you have understood the HTML <fieldset> tag with the help of examples and how it is used to group related fields within an HTML form.






Latest Courses