Table headers help organize data by labeling columns or rows. They make tables easier to understand and navigate.
Table headers in HTML
<thead> <tr> <th>Header 1</th> <th>Header 2</th> </tr> </thead>
The <th> tag defines a table header cell.
Headers are usually inside a <thead> section but can also appear in <tbody> or <tfoot>.
<table> <thead> <tr> <th>Name</th> <th>Age</th> <th>City</th> </tr> </thead> </table>
<thead>.<table> <tr> <th>Product</th> <th>Price</th> </tr> <tr> <td>Apple</td> <td>$1</td> </tr> </table>
scope attribute to specify if header is for a column or row, improving accessibility.<table> <thead> <tr> <th scope="col">Date</th> <th scope="col">Event</th> </tr> </thead> <tbody> <tr> <th scope="row">2024-06-01</th> <td>Meeting</td> </tr> </tbody> </table>
This example shows a simple table with headers for Name, Role, and Location. The headers have a green background and white text to stand out. The table is centered and easy to read.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Table Headers Example</title> <style> table { border-collapse: collapse; width: 100%; max-width: 400px; margin: 1rem auto; font-family: Arial, sans-serif; } th, td { border: 1px solid #333; padding: 0.5rem 1rem; text-align: left; } th { background-color: #4CAF50; color: white; } </style> </head> <body> <main> <h1>Team Members</h1> <table> <thead> <tr> <th>Name</th> <th>Role</th> <th>Location</th> </tr> </thead> <tbody> <tr> <td>Alice</td> <td>Developer</td> <td>New York</td> </tr> <tr> <td>Bob</td> <td>Designer</td> <td>London</td> </tr> <tr> <td>Charlie</td> <td>Manager</td> <td>Tokyo</td> </tr> </tbody> </table> </main> </body> </html>
Always use <th> for headers, not <td>, to help screen readers.
The scope attribute on <th> improves accessibility by clarifying if the header is for a row or column.
Use CSS to style headers differently so users can easily spot them.
Table headers label columns or rows to organize data clearly.
Use <th> tags inside <thead> or directly in rows.
Headers improve readability and accessibility for all users.
Practice
<th> tag in an HTML table?Solution
Step 1: Understand the role of
The<th><th>tag is used to create header cells in a table, which label columns or rows.Step 2: Differentiate from other tags
The<td>tag creates normal data cells, not headers. Captions use<caption>, and merging cells usescolspanorrowspan.Final Answer:
To define a header cell that labels a column or row -> Option AQuick Check:
Table headers = <th> tag [OK]
- Confusing <th> with <td>
- Thinking <th> merges cells
- Using <caption> as header cells
Solution
Step 1: Identify the tag for header cells
The correct tag for a table header cell is<th>.Step 2: Check the options
<td>is for data cells,<header>is a page section tag, and<thead>groups header rows but does not create individual cells.Final Answer:
<th>Name</th> -> Option DQuick Check:
Header cell = <th> [OK]
- Using <td> instead of <th>
- Confusing <thead> with <th>
- Using non-table tags like <header>
<th> elements in most browsers?<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Alice</td>
<td>30</td>
</tr>
</tbody>
</table>Solution
Step 1: Understand default browser styles for <th>
Browsers typically render<th>cells with bold text and center alignment to distinguish headers.Step 2: Compare with data cells
Data cells (<td>) are usually normal weight and left-aligned by default.Final Answer:
The header cells will be bold and centered by default -> Option CQuick Check:
<th> = bold + center [OK]
- Thinking headers are italic
- Assuming headers look like data cells
- Believing headers are hidden
<table>
<thead>
<tr>
<th>Product</td>
<th>Price</th>
</tr>
</thead>
</table>Solution
Step 1: Check tag consistency in header cells
The first header cell starts with<th>but incorrectly closes with</td>, which is invalid.Step 2: Verify other tags
The<thead>and<tr>tags are correctly used and closed. The second header cell uses<th>correctly.Final Answer:
The first header cell closes with </td> instead of </th> -> Option BQuick Check:
Matching tags for <th> = </th> [OK]
- Mixing <th> and <td> tags
- Forgetting to close <tr> or <thead>
- Using <td> inside <thead>
Solution
Step 1: Use <th> for both column and row headers
The top row headers and the first column headers should use<th>tags to indicate headers.Step 2: Check the provided options
Col1 Col2 correctly usesRow1 Data1 Data2 <th>in the top row and first column. The empty<th>in the top-left corner aligns headers properly.Final Answer:
Option A uses <th> for both row and column headers correctly -> Option AQuick Check:
Row and column headers = <th> tags [OK]
- Using <td> for headers in the first column
- Leaving top-left cell empty without <th>
- Mixing <td> and <th> incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
