Tag <tbody> In HTML

We will look at HTML Tag tbody with examples in this post. It should meet the learning needs.

The <tbody> tag in HTML is used to group the body content in a table. It is typically used in conjunction with the <table>, <thead>, and <tfoot> tags to organize the structure of a table.

The <tbody> tag contains one or more <tr> (table row) elements, which in turn contain one or more <td> (table data) or <th> (table header) elements. The <tbody> tag is optional, but it is recommended to use it to separate the table header and footer from the body content for better organization and accessibility.

Tag <tbody> stands for Table Body element.

Here is an example of an HTML table with a <thead>, <tbody>, and <tfoot> element:

Example: 

<!DOCTYPE html> <html> <head> </head> <body> <table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 4</td> <td>Data 5</td> <td>Data 6</td> </tr> </tbody> <tfoot> <tr> <td>Footer 1</td> <td>Footer 2</td> <td>Footer 3</td> </tr> </tfoot> </table> </body> </html>

How to use CSS to style the <thead>, <tbody>, and <tfoot> elements of an HTML table:

Example: 

<!DOCTYPE html> <html> <head> <style> thead { background-color: #f2f2f2; font-weight: bold; } tbody { background-color: #ffffff; } tfoot { background-color: #f2f2f2; font-style: italic; } </style> </head> <body> <table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 4</td> <td>Data 5</td> <td>Data 6</td> </tr> </tbody> <tfoot> <tr> <td>Footer 1</td> <td>Footer 2</td> <td>Footer 3</td> </tr> </tfoot> </table> </body> </html>

Here is an example of how to use CSS to align the content with <tbody> elements of an HTML table:

Example: 

<!DOCTYPE html> <html> <head> <style> tbody { text-align: center; } tbody td { vertical-align: middle; } </style> </head> <body> <table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 4</td> <td>Data 5</td> <td>Data 6</td> </tr> </tbody> <tfoot> <tr> <td>Footer 1</td> <td>Footer 2</td> <td>Footer 3</td> </tr> </tfoot> </table> </body> </html>

Here is an example of how the <tbody>tag can be styled in a unique way:

Example: 

<!DOCTYPE html> <html> <head> <style> table { width: 100%; border-collapse: collapse; } th { background-color: #f2f2f2; font-weight: bold; text-align: left; } td { padding: 10px; border: 1px solid #ccc; } tbody { background-color: #f9f9f9; font-size: 0.9em; } tbody tr:hover { background-color: #ddd; } </style> </head> <body> <table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 4</td> <td>Data 5</td> <td>Data 6</td> </tr> </tbody> <tfoot> <tr> <td>Footer 1</td> <td>Footer 2</td> <td>Footer 3</td> </tr> </tfoot> </table> </body> </html>

Tag <tbody> Uses

The Tag <tbody> is employed to organize body information in an HTML table.

To indicate a table’s body (header, footer), add the Tag <tbody> with the Tag <thead> and Tag <tfoot> elements . By using these elements, browsers can navigate the table body on their own from the header and footer. Furthermore, if a large table occupies multiple pages, the table header and footer can be displayed at the beginning and end of each.

The Tag <tbody> should be positioned accordingly: under a <table> element, followed by <caption>, <colgroup>, and <thead> elements.
IMPORTANT: The <tbody> element should contain multiple <tr> tags within it.

Common Uses

Here are some of the primary uses of the <tbody> tag:

  • The <tbody> tag is used to group data rows in a table. This makes it easier to identify the main content of the table and separates it from the header and footer.
  • By using the <tbody> tag, designers can create tables that are more accessible for users who rely on screen readers or other assistive technologies. The <tbody> tag allows these users to skip over the header and footer and focus on the main content of the table.
  • The <tbody> tag can also be used to apply styles to the body content of a table. By using the <tbody> tag to group the data rows, designers can apply styles to the entire body section of the table.
  • The <tbody> tag can also be used to enhance the functionality of a table. For example, designers can use JavaScript to add interactive features to the data rows, such as sorting or filtering.
Advice: Typically, the <thead>, <tbody>, and <tfoot> tags will not impact the table’s layout. In order to style these elements, you will need to use CSS.

Attributes

Global

The Tag <tbody> includes the Global Attributes in HTML.

Event

The Tag <tbody> also accepts the Event Attributes in HTML.


Browser Compatibility

Element
<tbody>YesYesYesYesYes

Predefined CSS

Most internet browsers show the Tag tbody <tbody> with the following default values:

tbody {
display: table-row-group;
vertical-align: middle;
border-color: inherit;
}

HTML Tag tbody Benefits

The <tbody> tag in HTML offers several benefits for structuring and presenting data in a table:

  1. By using the <tbody> tag to group the body content of a table, designers can improve the organization and structure of the table. This makes it easier for users to scan and understand the data being presented.
  2. The <tbody> tag separates the body content of a table from the header and footer. This improves accessibility for users who rely on screen readers or other assistive technologies, as it allows them to skip over the header and footer and focus on the main content of the table.
  3. The <tbody> tag can be used to apply styles to the body content of a table, allowing designers to create a consistent look and feel for the table.
  4. By using the <tbody> tag, designers can add interactive features to the body content of a table, such as sorting or filtering. This enhances the functionality of the table and makes it more useful for users.
  5. The <tbody> tag is widely supported across different web browsers and platforms, ensuring that tables using the <tbody> tag are accessible to a wide range of users.
Do not miss to subscribe to our Newsletter below, in order to be in touch with the latest technical information on this site.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *