How to group related elements in a form using HTML?


When designing a form for a website, it's important to consider the user experience and make it as easy as possible for users to fill out the form. One way to do this is by grouping related elements together so that users can quickly scan and fill out the form. In this blog post, we'll discuss how to group related elements in a form using HTML.

What is Grouping in HTML Forms?

Grouping in HTML forms involves grouping related form elements together, such as radio buttons, checkboxes, and text input fields. By grouping related elements together, you can make it easier for users to understand the structure of the form and complete it more efficiently.

HTML offers several ways to group related form elements together. Some of the most common methods include using the <fieldset> and <legend> tags, as well as grouping elements with labels.

Method 1: Using Fieldset and Legend Tags

The <fieldset> and <legend> tags are commonly used together to group related form elements. The <fieldset> tag is used to group a set of related form controls together, while the <legend> tag provides a caption or title for the group.

Example 

Here's an example of how to use the <fieldset> and <legend> tags to group radio buttons together 

<form>
   <fieldset>
      <legend>Preferred Contact Method:</legend>
      <input type="radio" id="contact-phone" name="contact-method" value="phone">
      <label for="contact-phone">Phone</label><br>
      <input type="radio" id="contact-email" name="contact-method" value="email">
      <label for="contact-email">Email</label><br>
      <input type="radio" id="contact-mail" name="contact-method" value="mail">
      <label for="contact-mail">Mail</label><br>
   </fieldset>
</form>

In this example, the <fieldset> tag groups the three radio buttons together and the <legend> tag provides a title for the group ("Preferred Contact Method"). This makes it clear to the user that these radio buttons are related and should be considered as a group when filling out the form.

Method 2: Grouping Elements with Labels

Another way to group related form elements together is to use labels. In HTML, the <label> tag is used to associate a text label with a form control. When a user clicks on the label, the associated form control is activated.

Example 

Here's an example of how to group checkboxes together using labels −

<form>
   <label for="check-fruit">Select your favorite fruit:</label><br>
   <input type="checkbox" id="check-fruit" name="fruit" value="apple">
   <label for="check-fruit">Apple</label><br>
   <input type="checkbox" id="check-fruit" name="fruit" value="banana">
   <label for="check-fruit">Banana</label><br>
   <input type="checkbox" id="check-fruit" name="fruit" value="orange">
   <label for="check-fruit">Orange</label><br>
</form>

In this example, each checkbox is associated with a label using the "for" attribute. The text label is displayed next to the checkbox, making it clear to the user that these checkboxes are related and should be considered as a group when filling out the form.

Method 3: Using Div Tags

You can also use <div> tags to group related form elements together. This method provides more flexibility than using the <fieldset> and <legend> tags, but it requires more work to style the form elements.

Example 

Here's an example of how you can group form elements using <div> tags −

<form>
   <div>
      <label for="firstName">First Name:</label>
      <input type="text" id="firstName" name="firstName">
   </div>
   <div>
      <label for="lastName">Last Name:</label>
      <input type="text" id="lastName" name="lastName">
   </div>
   <div>
      <label for="email">Email:</label>
      <input type="email" id="email" name="email">
   </div>
   <div>
      <label for="password">Password:</label>
      <input type="password" id="password" name="password">
   </div>
   <div>
      <label for="confirmPassword">Confirm Password:</label>
      <input type="password" id="confirmPassword" name="confirmPassword">
   </div>
   <div>
      <label for="dob">Date of Birth:</label>
      <input type="date" id="dob" name="dob">
   </div>
   <div>
      <input type="submit" value="Submit">
   </div>
</form>

In this example, each form element is wrapped in a <div> tag. You can apply styles to the <div> tags to group related elements together visually. For example, you could give each <div> a border, background color, or padding to make it stand out.

To style the form elements themselves, you can use CSS selectors to target specific input types or IDs. Here's an example of how you can style the <input> elements in the example above −

<style>
   input[type="text"], input[type="email"], input[type="password"], input[type="date"] {
      display: block;
      width: 100%;
      padding: 0.5rem;
      border: 1px solid #ccc;
      border-radius: 0.25rem;
      margin-bottom: 1rem;
   }
</style>

This CSS rule sets a standard style for all the <input> elements in the form. The display: block property makes each input element take up the full width of its container (in this case, the <div> tag). The width, padding, border, and border-radius properties control the appearance of the input fields themselves. Finally, the margin-bottom property adds a bit of space between each input field to make the form easier to read.

Overall, using <div> tags to group related form elements together gives you more control over the appearance and layout of your form. However, it requires more work to style each individual form element.

Method 4: Styling Group Labels

When grouping form elements, it's important to provide clear and distinct labels for each group. This helps users quickly scan the form and understand how the elements are related. In addition to using the <fieldset> and <legend> tags, you can also style the labels associated with each group to make them more visually distinct.

One way to style group labels is to use CSS to apply a background color or border. For example 

<style>
   fieldset {
      border: 1px solid #ccc;
      padding: 10px;
   }
   legend {
      background-color: #f2f2f2;
      padding: 5px;
      font-weight: bold;
   }
</style>

In this example, the <fieldset> tag is given a border and padding to visually separate it from the other elements on the form. The <legend> tag is styled with a background color, padding, and bold font to make it stand out as the label for the group.

You can also use CSS to apply other styles to group labels, such as font size, font weight, and text alignment. For example 

<style>
   legend {
      font-size: 1.2em;
      font-weight: bold;
      text-align: center;
   }
</style>

This example sets the font size to 1.2em, bolds the text, and centers it within the <legend> tag.

Styling group labels can be especially useful when working with longer forms that contain multiple groups of related elements. By applying different styles to each group label, you can create a visual hierarchy that helps users navigate the form more easily.

Using Grid Systems

Another way to group related form elements is to use a grid system. Many front-end frameworks and CSS libraries provide grid systems that allow you to create a more structured and organized form layout that is easy to read and navigate.

A grid system is typically based on a series of columns and rows that are used to lay out the elements on the page. Each column has a fixed width, and the elements are placed within the columns to create a grid-like structure.

One popular grid system is Bootstrap, which provides a 12-column grid that can be used to create a variety of different layouts. To use the Bootstrap grid system for form layout, you can wrap each group of related form elements in a <div> tag with a class of "row", and then divide the row into columns using the Bootstrap grid classes. For example 

Example 

<div class="row">
   <div class="col-md-6">
      <label for="first-name">First Name:</label>
      <input type="text" id="first-name" name="first-name">
   </div>
   <div class="col-md-6">
      <label for="last-name">Last Name:</label>
      <input type="text" id="last-name" name="last-name">
   </div>
</div>

In this example, the form elements for "First Name" and "Last Name" are wrapped in a <div> tag with a class of "row". The row is then divided into two columns using the "col-md-6" class, which specifies that each column should take up 6 of the 12 available columns on medium-sized screens.

By using a grid system, you can create a more organized and visually appealing form layout that is easy for users to navigate. However, it's important to remember that not all users may be familiar with grid layouts, so you should also provide clear and distinct labels for each group of form elements.

Conclusion

Grouping related form elements in HTML is an important aspect of creating user-friendly and accessible web forms. It helps users to understand the purpose of the form and makes it easier to navigate and complete. In this blog post, we have explored different methods to group form elements, including using the <fieldset> and <legend> tags, <div> tags, and semantic HTML5 tags such as <header>, <main>, and <section>. We have also discussed different ways to style and organize grouped form elements using CSS, grid systems, and JavaScript.

Updated on: 07-Aug-2023

340 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements