HTML <tbody> Tag โ Table Body Section
๐ Introduction
The <tbody> tag is used to group the main data content of a table.
It contains all the rows (<tr>) with actual data (<td>).
It helps separate:
- Header โ
<thead> - Data โ
<tbody> - Footer โ
<tfoot>
๐งฑ Syntax
<table>
<thead>
<tr>
<th>Column 1</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data</td>
</tr>
</tbody>
</table>
๐ก Example
<!DOCTYPE html>
<html>
<head>
<title>TBODY Example</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>yourname</td>
<td>20</td>
<td>Algeria</td>
</tr>
<tr>
<td>Sara</td>
<td>22</td>
<td>Morocco</td>
</tr>
</tbody>
</table>
</body>
</html>
๐ฅ๏ธ Output Explanation
<tbody>contains all data rows- It separates data from headers
- Makes table structure clean and organized
๐ฏ Why <tbody> is Important
1. ๐ง Better Structure
It clearly separates:
- Headers (
<thead>) - Data (
<tbody>)
2. ๐ Used in Real Applications
Most real systems use it:
- Admin dashboards
- Data tables
- Reports
3. โ๏ธ JavaScript Control
You can easily manipulate table data:
document.querySelector("tbody").innerHTML = "";
โ Useful for dynamic data loading
๐งฉ Full Table Structure
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>yourname</td>
</tr>
</tbody>
</table>
โ๏ธ <tbody> vs Other Parts
| Tag | Purpose |
|---|---|
<thead> |
Table headers |
<tbody> |
Main data |
<tfoot> |
Summary/footer |
๐จ Styling Example
<tbody style="background-color:#f9f9f9;">
<tr>
<td>yourname</td>
</tr>
</tbody>
๐ซ Common Mistakes
- Putting headers inside
<tbody>โ - Mixing structure randomly โ
- Not using
<thead>+<tbody>together โ
๐งช Mini Exercise
Create a table with:
<thead>for column titles<tbody>with at least 3 rows- Style alternating row colors (optional challenge)