<th>
The HTML <th>
tag is used for creating a table header cell within an HTML table. This tag is an essential coding element when structuring a table, ensuring that your data can be easily read and understood.
The <th>
element provides a label for a particular column or row, differentiating it from other standard table data cells represented by <td>
tags. By default, the text contained in a <th>
cell will be bold and centered to visually reinforce the hierarchical difference.
An example of the <th>
tag in a simple table might look like this:
<table>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
<tr>
<td>John Doe</td>
<td>[email protected]</td>
</tr>
</table>
In the above example, 'Name' and 'Email' are header names, which explain the context of the data contained in the rest of the column. The data of 'John Doe' and '[email protected]' are enclosed in <td>
tags indicating they're normal table data cells.
The <th>
tag is also useful when you need to add accessibility features to your tables. Using the scope
attribute in combination with the <th>
tag can provide screen readers with contextual knowledge about the table's data, improving the user experience for those using these assistive technologies.
Whether you're crafting a simple contact list or detailed data display, using the <th>
tag to create a clearly defined table header is a vitally important part of creating HTML tables.