Table Headers with <th>
What you'll learn: How to use the <th> element to label your table's columns and rows with semantic meaning.
The Core Idea
In your last lesson, you learned that tables are built with <table>, <tr> (rows), and <td> (data cells). But how does someone looking at your table know what each column or row represents? That's where table headers come in.
The <th> element creates a header cell instead of a regular data cell. It works just like <td>, but it tells browsers and assistive technologies: "This cell is a label, not data."
Think of it like labeling the columns in a spreadsheet. Without headers, a list of numbers means nothing. With headers like "Price," "Quantity," and "Total," those numbers make sense.
How It Works
You use <th> exactly where you'd use <td>, but typically in your first row (for column headers):
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
<tr>
<td>Sarah</td>
<td>28</td>
<td>Portland</td>
</tr>
<tr>
<td>Marcus</td>
<td>34</td>
<td>Seattle</td>
</tr>
</table>
Browsers automatically make <th> text bold and centered by default, visually distinguishing headers from data. More importantly, screen readers announce these as headers, helping users navigate complex tables.
You can also use <th> for row headers by placing them as the first cell in each row, labeling what that row represents.
Why It Matters
Using <th> instead of <td> for headers is semantic HTML at work—it adds meaning to your structure. It helps accessibility tools, improves SEO, and makes your code clearer to other developers.
Key Takeaway: Use <th> instead of <td> for header cells to give your table structure semantic meaning and improve accessibility.