How to group the body content in a table using HTML5?


HTML tables are commonly used to present data in a structured manner. Tables consist of rows and columns where each row represents a record and each column represents a field in that record. In some cases, you may want to group the content in the body of the table, such as when you have multiple data points that belong together. In this blog post, we will explore different methods to group body content in HTML tables.

Method 1: Using the <tbody> Element

The simplest way to group body content in an HTML table is to use the <tbody> element. The <tbody> element is used to group a set of rows in a table and is typically used to group the main content of a table. Here's an example.

Example

<table>
   <thead>
      <tr>
         <th>Name</th>
         <th>Age</th>
         <th>City</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>John Doe</td>
         <td>25</td>
         <td>New York</td>
      </tr>
      <tr>
         <td>Jane Smith</td>
         <td>30</td>
         <td>Los Angeles</td>
      </tr>
   </tbody>
</table>

In this example, we have used the <tbody> element to group the two rows that contain the main content of the table. By default, all rows that are not explicitly wrapped in a <thead> or <tfoot> element are automatically wrapped in a <tbody> element.

Method 2: Using the <colgroup> and <tbody> Elements

Another way to group body content in an HTML table is to use the <colgroup> and <tbody> elements together. The <colgroup> element allows you to group columns together and apply styles or attributes to the entire group, while the <tbody> element allows you to group rows together. Here's an example −

Example

<table>
   <colgroup span="2"></colgroup>
   <colgroup span="1"></colgroup>
   <thead>
      <tr>
         <th colspan="2">Name</th>
         <th>Age</th>
         <th>City</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td colspan="2">John Doe</td>
         <td>25</td>
         <td>New York</td>
      </tr>
      <tr>
         <td colspan="2">Jane Smith</td>
         <td>30</td>
         <td>Los Angeles</td>
      </tr>
   </tbody>
</table>

In this example, we have used the <colgroup> element to group the first two columns together and the last column separately. We have also used the colspan attribute to span the first two columns of the <thead> element. The <tbody> element is used to group the two rows that contain the main content of the table.

Method 3: Using CSS

Another way to group body content in an HTML table is by using CSS. With CSS, you can apply different styles to specific rows or cells in your table, creating a visual grouping effect. Here's an example 

Example

<style>
   .group1 {
      background-color: #f2f2f2;
   }
   .group2 {
      background-color: #e5e5e5;
   }
</style>
<table>
   <thead>
      <tr>
         <th>Name</th>
         <th>Age</th>
         <th>City</th>
      </tr>
   </thead>
<tbody>

In this example, we have defined two groups with different background colors using CSS classes .group1 and .group2. We then apply these classes to the appropriate rows in the table body to visually group the content.

You can also use CSS to style individual cells or columns in your table. For example, you can apply a specific style to every cell in the first column, or to cells that meet a certain criteria based on their content. This can help to further group and differentiate the content in your table.

Using CSS to group body content in an HTML table provides a lot of flexibility in terms of the visual design and styling of your table. However, it requires a good understanding of CSS and can be more time-consuming than the other methods discussed.

Creating Dynamic Grouped Body Content using JavaScript

Similar to grouping table headers, you can also use JavaScript and jQuery to create dynamic grouped body content in a table. This can be especially helpful if you have a large amount of data in your table that you want to make more manageable for users. You can use JavaScript to toggle the visibility of rows based on user interaction, such as clicking on a header row. Here's an example.

Example

<script>
   $(document).ready(function() {
      $('.group-header').click(function() {
         $(this).toggleClass('collapsed');
         $(this).nextUntil('.group-header').toggle();
      });
   });
</script>
<table>
   <thead>
      <tr class="group-header">
         <th>Group 1</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>Row 1, Group 1</td>
      </tr>
      <tr>
         <td>Row 2, Group 1</td>
      </tr>
      <tr class="group-header">
         <th>Group 2</th>
      </tr>
      <tr>
         <td>Row 1, Group 2</td>
      </tr>
      <tr>
         <td>Row 2, Group 2</td>
      </tr>
   </tbody>
</table>

In this example, we've added a click event handler to the group header rows using jQuery. When a user clicks on a header row, we toggle a class to show or hide the collapse icon and use the nextUntil() function to toggle the visibility of all the rows until the next group header row.

Using Nested Tables to Group Content

Another way to group content in an HTML table is to use nested tables. This involves creating a new table within one or more cells of an existing table. You can then apply styles or attributes to the nested table to group the content. Here's an example.

Example

<table>
   <thead>
      <tr>
         <th>Name</th>
         <th>Age</th>
         <th>City</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>John Doe</td>
         <td>27</td>
         <td>
            <table>
               <tbody>
                  <tr>
                     <td>Los Angeles</td>
                  </tr>
               </tbody>
            </table>
         </td>
      </tr>
      <tr>
         <td>Jane Smith</td>
         <td>35</td>
         <td>
            <table>
               <tbody>
                  <tr>
                     <td>San Francisco</td>
                  </tr>
               </tbody>
            </table>
         </td>
      </tr>
   </tbody>
</table>

In this example, we've nested a table within the City column of our main table. We've used a separate tbody element for each nested table to keep the HTML structure organized. You can then apply styles or attributes to the nested tables to group the content.

Accessibility Considerations for Grouped Table Content

When using grouped table content, it's important to consider accessibility for users who rely on screen readers or other assistive technologies. You should ensure that your markup is structured in a way that makes sense when read out of context, and that you use appropriate HTML attributes such as aria-label to provide context to users who cannot see the visual grouping cues.

Here are some additional tips to ensure accessibility:

  • Use meaningful headers for each group and ensure they are marked up properly using the <th> element.

  • Provide additional context for each group using descriptive captions or labels that are associated with the group using the aria-describedby attribute.

  • Use the tabindex attribute to ensure that keyboard-only users can navigate to and interact with the grouped content.

  • Use CSS to visually distinguish the grouped content, but ensure that the visual cues are not the only way to identify the grouping. For example, you can use color, borders, or shading to visually group content, but also use meaningful headers and labels to provide context.

  • Test your markup with a screen reader or other assistive technology to ensure that the grouping is conveyed accurately and effectively.

Conclusion

Grouping body content in an HTML table is a useful technique that can improve the readability and organization of data. There are several methods available to achieve this, including the <tbody> element, the <colgroup> element, and CSS. Additionally, using JavaScript and jQuery to create dynamic grouped body content or using nested tables can provide even more flexibility in how you group your data. When implementing grouped table content, it's important to consider accessibility for users who rely on assistive technologies.

Updated on: 07-Aug-2023

654 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements