th Tag in HTML
The <th> tag is an HTML element that is used to define table header cells. This tag is used within the <table> element to specify the headers for columns or rows.
Syntax:
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table>
The <th> tag should be used in place of <td> when you want to specify header cells. Header cells are typically formatted differently than data cells, often using bold text, centered alignment, or background colors to distinguish them from the rest of the table.
Attributes:
The <th> tag supports the following attributes:
– **colspan**: This attribute specifies the number of columns that the header cell should span.
<th colspan=”2″>Header 1 and Header 2</th>
“`
– **rowspan**: This attribute specifies the number of rows that the header cell should span.
<th rowspan=”2″>Header 1 and Header 2</th>
“`
– **scope**: This attribute specifies whether the header cell applies to a column or row.
<th scope=”row”>Header 1</th>
<th scope=”col”>Header 2</th>
– **abbr**: This attribute specifies an abbreviated version of the header cell text.
<th abbr=”H1″>Header 1</th>
Example:
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>john@example.com</td>
</tr>
<tr>
<td>Jane</td>
<td>Smith</td>
<td>jane@example.com</td>
</tr>
</table>
Output:
| First Name | Last Name | Email |
|————|———–|——————–|
| John | Doe | john@example.com |
| Jane | Smith | jane@example.com |
In the above example, the <th> tag is used to specify the header cells for the table. The text in the header cells is centered and bold by default.