HTML <th> Tag โ Table Header Cell
๐ Introduction
The <th> tag defines a header cell in a table.
It is used to label columns or rows in a table.
By default:
- Text is bold
- Text is centered
๐งฑ Syntax
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</table>
๐ก Example
<!DOCTYPE html>
<html>
<head>
<title>TH Example</title>
</head>
<body>
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>yourName</td>
<td>20</td>
<td>Algeria</td>
</tr>
<tr>
<td>Sara</td>
<td>22</td>
<td>Morocco</td>
</tr>
</table>
</body>
</html>
๐ฅ๏ธ Output Explanation
<th>creates column titles- They appear bold and centered by default
- They improve table readability
๐ฏ Why <th> is Important
1. ๐ง Better Structure
It clearly labels:
- Columns
- Rows
2. ๐ Improves Readability
Users immediately understand the data meaning.
3. โฟ Accessibility
Screen readers identify headers easily.
โ๏ธ <th> vs <td>
| Tag | Purpose | Style |
|---|---|---|
<th> |
Header cell | Bold + centered |
<td> |
Data cell | Normal text |
๐งฉ Example in Full Table
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>yourName</td>
<td>20</td>
</tr>
</table>
๐ฏ Advanced Usage
Row Header
<tr>
<th>Student</th>
<td>yourName</td>
</tr>
โ <th> can also be used for row labels
๐จ Styling Example
<th style="background-color:#ddd; padding:5px;">Name</th>
๐ซ Common Mistakes
- Using
<th>inside wrong structure โ - Forgetting
<tr>โ - Using
<td>instead of<th>for headers โ
๐งช Mini Exercise
Create a table with:
- 3 columns using
<th> - At least 3 rows of data
- Style headers with background color
- Add borders