Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Syntax of the Fieldset Tag
2.1.
HTML
2.2.
Explanation
3.
Attributes of the Fieldset Tag
3.1.
Disabled
3.1.1.
Example:
3.2.
HTML
3.3.
Form
3.3.1.
Example:
3.4.
HTML
3.5.
Name
3.5.1.
Example:
3.6.
HTML
4.
Examples Using the Fieldset Tag
4.1.
Example 1: Basic Contact Form
4.2.
HTML
4.2.1.
Output
4.2.2.
Explanation:
4.3.
Example 2: Multiple Choice Questions
4.4.
HTML
4.4.1.
Output
4.4.2.
Explanation:
5.
Supported Browsers for the Fieldset Tag
5.1.
List of Browsers That Support the Fieldset Tag:
5.2.
What This Means for You
6.
Frequently Asked Questions
6.1.
Can I use multiple <fieldset> tags in one form?
6.2.
What happens if I don't use a <legend> tag inside a <fieldset>?
6.3.
Is the <fieldset> tag necessary for all forms?
7.
Conclusion
Last Updated: May 26, 2024
Easy

HTML Fieldset Tag

Author Rahul Singh
0 upvote

Introduction

HTML provides various tags to structure & organize web page content. One such tag is the <fieldset> tag, which groups related form elements together. It creates a box around the grouped elements, making the form more readable & user-friendly. 

HTML Fieldset Tag

In this article, we will talk about the <fieldset> tag in detail, including its syntax, attributes, & examples.

Syntax of the Fieldset Tag

The <fieldset> tag in HTML is simple & straightforward to use. This tag is usually placed within a form to group similar elements, making the form easier to understand & use. Here’s how you write it:

  • HTML

HTML

<form>

 <fieldset>

   <legend>Personal Information</legend>

   <!-- Form elements like input, label, etc. go here -->

 </fieldset>

</form>

Output

Output


In this example, the <fieldset> tag is used inside a form. The <legend> tag, which is optional, provides a title or explanation for the group of form elements it surrounds, like 'Personal Information' in this case. This helps users know what type of information they are expected to enter in that section of the form.

Explanation

  • Form: This is the container that holds your inputs & labels. Everything inside the <form> tag is part of your form.
     
  • Fieldset: This tag groups related items within a form. It makes the form easier to read & fill out.
     
  • Legend: This tag is used inside a <fieldset> to give a title to the group of fields. It helps people understand what the grouped fields are for.
     

Note -: By organizing form elements in this way, you make it much easier for users to navigate the form, especially if it is a long or complex one. This organization also aids in designing responsive forms that work well on different devices & screen sizes.

Attributes of the Fieldset Tag

The <fieldset> tag supports several attributes that enhance its functionality & make forms more versatile & user-friendly. Here are some of the main attributes you might use with this tag:

Disabled

This attribute is used to disable all the form elements inside the <fieldset>. When you add disabled to your <fieldset>, users can't interact with any of the elements inside it. This is useful if you need to turn off parts of your form under certain conditions.

Example:

  • HTML

HTML

<fieldset disabled>

 <legend>Payment Information</legend>

 <!-- All inputs here are disabled -->

</fieldset>

 

Output

Output

Form

This attribute links the <fieldset> to one specific form. Sometimes you have multiple forms on a page & you need to make it clear which <fieldset> belongs to which form. The form attribute helps you do that by taking the ID of the form you want to link to.

Example:

  • HTML

HTML

<form id="user-registration">

 <!-- Some other elements here -->

</form>

<fieldset form="user-registration">

 <legend>Additional Details</legend>

 <!-- Elements here are part of 'user-registration' form -->

</fieldset>

Output

Output

Name

The name attribute is used to give a name to the <fieldset> which can be useful in scripting or when you're handling form data on the server-side.

Example:

  • HTML

HTML

<fieldset name="user-info">

 <legend>Profile</legend>

 <!-- Elements with names like 'username', 'password' -->

</fieldset>

Output

Output

Examples Using the Fieldset Tag

Now, let’s look at some practical examples of Fieldset Tag : 

Example 1: Basic Contact Form

In this example, we create a simple contact form where users can enter their name, email, & a short message. The <fieldset> tag helps organize these inputs neatly.

  • HTML

HTML

<form action="/submit-form" method="post">
 <fieldset>
   <legend>Contact Us</legend>
   <label for="name">Name:</label>
   <input type="text" id="name" name="name" required>
   <label for="email">Email:</label>
   <input type="email" id="email" name="email" required>
   <label for="message">Message:</label>
   <textarea id="message" name="message" rows="4" required></textarea>
   <button type="submit">Send</button>
 </fieldset>
</form>


Output

Output

Explanation:

  • Form tags define where to send the form data when submitted.
     
  • Fieldset groups the form elements, making them easy to manage.
     
  • Legend provides a clear title for what the fieldset contains.
     
  • Labels and Inputs are basic form elements where users can type their info.

Example 2: Multiple Choice Questions

Here, we use the <fieldset> tag to group a set of radio buttons for a survey or quiz form. This organizes the choices clearly, making it easy for users to select an option.

  • HTML

HTML

<form action="/submit-answers" method="post">
 <fieldset>
   <legend>Survey Question</legend>
   <p>How satisfied are you with our service?</p>
   <label><input type="radio" name="satisfaction" value="very-satisfied"> Very Satisfied</label>
   <label><input type="radio" name="satisfaction" value="satisfied"> Satisfied</label>
   <label><input type="radio" name="satisfaction" value="neutral"> Neutral</label>
   <label><input type="radio" name="satisfaction" value="dissatisfied"> Dissatisfied</label>
   <label><input type="radio" name="satisfaction" value="very-dissatisfied"> Very Dissatisfied</label>
   <button type="submit">Submit</button>
 </fieldset>
</form>


Output

Output

:

Explanation:

  • Radio Buttons let users choose one option from several. They are grouped under one question, making it easy to respond.
     
  • Labels are used for each choice so users can click on the text to select an option, enhancing usability.

Supported Browsers for the Fieldset Tag

When you use the <fieldset> tag in your web forms, it's important to know which web browsers will display it correctly. Luckily, the <fieldset> tag is supported by all major browsers, which means that your forms will work well for almost everyone who visits your site.

List of Browsers That Support the Fieldset Tag:

  • Google Chrome: Chrome supports the <fieldset> tag, so users on this browser will see your forms exactly as you intended.
     
  • Mozilla Firefox: Firefox also supports this tag, ensuring that forms are displayed properly for its users.
     
  • Safari: Users on both macOS & iOS devices can view forms using the <fieldset> tag correctly in Safari.
     
  • Microsoft Edge: Edge provides full support for the <fieldset> tag, which is great for users on Windows devices.
     
  • Internet Explorer: Even older browsers like Internet Explorer support the <fieldset> tag, although some newer HTML5 features may not work as well here.

What This Means for You

You can use the <fieldset> tag in your forms without worrying about compatibility issues. This is great because it means more people can use your forms without problems, no matter what device or browser they are using. It's always good to check your forms in different browsers to make sure they look & work as expected.

Frequently Asked Questions

Can I use multiple <fieldset> tags in one form?

Yes, you can use multiple <fieldset> tags in a single form to group different types of information separately. This makes your form easier to navigate & understand for users.

What happens if I don't use a <legend> tag inside a <fieldset>?

While the <legend> tag is not mandatory, using it helps visually & accessibility-wise by providing a title for the group of fields. Without it, users might find it a bit harder to understand the purpose of the fieldset.

Is the <fieldset> tag necessary for all forms?

No, it's not necessary for all forms, but it's highly recommended for complex forms with multiple sections or related groups of information. It enhances form usability & accessibility.

Conclusion

In this article, we have learned about the <fieldset> tag in HTML, which is crucial for organizing form elements effectively. We discussed its syntax, various attributes, practical examples, & browser support. Understanding how to use the <fieldset> tag correctly can significantly improve the structure & user experience of your web forms, ensuring that they are accessible & easy to use for everyone. By combining related items & making forms easier to navigate, the <fieldset> tag plays a key role in web form design.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass