The <tbody>
tag is used to group a set of table rows (<tr>
tags) together as a body. It is used in conjunction with the <table>
tag to create a table structure in HTML.
The <tbody>
tag is optional in HTML, but it is recommended to use it for better organization and structure of the table. It is also useful for styling purposes, as it allows you to apply styles to the entire body of the table.
Here are some examples of how to use the <tbody>
tag:
In this example, we will create a basic table structure with the <tbody>
tag:
<table>
<tbody>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</tbody>
</table>
In this example, we have created a table with two rows and two columns. The <tbody>
tag groups the two rows together as a body.
In this example, we will apply some styles to the table body using the <tbody>
tag:
<style>
tbody {
background-color: #f2f2f2;
}
</style>
<table>
<tbody>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</tbody>
</table>
In this example, we have applied a background color to the table body using the <tbody>
tag. This style will be applied to the entire body of the table.
In this example, we will use multiple <tbody>
tags to group different sections of the table:
<table>
<tbody>
<tr>
<td>Header 1</td>
<td>Header 2</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Section 1, Row 1</td>
<td>Section 1, Row 2</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Section 2, Row 1</td>
<td>Section 2, Row 2</td>
</tr>
</tbody>
</table>
In this example, we have used multiple <tbody>
tags to group different sections of the table. The first <tbody>
tag contains the table header, while the other two <tbody>
tags contain different sections of the table body.
These are just a few examples of how to use the <tbody>
tag in HTML. It is a simple but powerful tag that can help you create organized and structured tables in your web pages.