HTML provides several elements to define headers in a table. Here are some commonly used elements:
<th>
: This element is used to define a header cell in a table. It is similar to a<td>
cell, but is typically rendered with bold text and centered alignment. Here’s an example:
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table>
In this example, the <th>
element is used to define the header cells in the first row of the table.
<thead>
: This element is used to group the header content in a table. It should be used within the<table>
element and can contain one or more<tr>
elements. Here’s an example:
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</tbody>
</table>
In this example, the <thead>
element is used to group the header cells in the first row of the table.
<tfoot>
: This element is used to group the footer content in a table. It should be used within the<table>
element and can contain one or more<tr>
elements. Here’s an example:
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Footer 1</td>
<td>Footer 2</td>
</tr>
</tfoot>
</table>
In this example, the <tfoot>
element is used to group the footer cells in the last row of the table.
These are some of the commonly used HTML elements to define headers in a table. You can choose the element that best suits your needs based on the structure and content of your table.