HTML table headers and captions make tables more understandable and accessible. The <th> tag defines header cells, which label rows or columns, while the <caption> tag provides a title or summary for the table. This tutorial explains how to use headers and captions with examples and previews, helping beginners create clear and accessible tables.
Headers (<th>) are special cells that describe the content of a row or column, typically bold and centered by default. They help users and screen readers understand the table’s structure. Captions (<caption>) provide a brief description of the table’s purpose, appearing above or below the table (styled with CSS). Both improve usability and accessibility.
Let’s explore how to add headers and captions to tables with examples and previews.
The <th> tag is used in place of <td> for header cells, typically in the first row or column.
<table style="border: 1px solid black; border-collapse: collapse;">
<tr>
<th style="border: 1px solid black; padding: 8px;">Name</th>
<th style="border: 1px solid black; padding: 8px;">Age</th>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Alice</td>
<td style="border: 1px solid black; padding: 8px;">25</td>
</tr>
</table>
Name | Age |
---|---|
Alice | 25 |
The <caption> tag is placed immediately after the <table> tag to describe the table.
<table style="border: 1px solid black; border-collapse: collapse;">
<caption>Student Grades</caption>
<tr>
<th style="border: 1px solid black; padding: 8px;">Student</th>
<th style="border: 1px solid black; padding: 8px;">Grade</th>
</tr>
<td style="border: 1px solid black; padding: 8px;">Bob</td>
<td style="border: 1px solid black; padding: 8px;">A</td>
</table>
Student | Grade |
---|---|
Bob | A |
The scope attribute in <th> specifies whether a header applies to a row or column, improving accessibility for screen readers.
<table style="border: 1px solid black; border-collapse: collapse;">
<tr>
<th scope="col" style="border: 1px solid black; padding: 8px;">Item</th>
<th scope="col" style="border: 1px solid black; padding: 8px;">Price</th>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Pen</td>
<td style="border: 1px solid black; padding: 8px;">$2</td>
</tr>
</table>
Item | Price |
---|---|
Pen | $2 |
Headers can also label rows using <th> with scope="row".
<table style="border: 1px solid black; border-collapse: collapse;">
<caption>Employee Roles</caption>
<tr>
<th scope="row" style="border: 1px solid black; padding: 8px;">Alice</th>
<td style="border: 1px solid black; padding: 8px;">Designer</td>
</tr>
<tr>
<th scope="row" style="border: 1px solid black; padding: 8px;">Bob</th>
<td style="border: 1px solid black; padding: 8px;">Developer</td>
</tr>
</table>
Alice | Designer |
---|---|
Bob | Developer |
Headers and captions can be enhanced with attributes and CSS:
col
, row
, colgroup
, or rowgroup
) for accessibility.
<th scope="col">Column Header</th>
background-color
, font-weight
, or text-align
.
<th style="background-color: #f0f0f0; padding: 8px;">Header</th>
caption-side
(e.g., top
or bottom
) or style with font-size
.
<caption style="caption-side: bottom; font-style: italic;">Table Description</caption>
To make your headers and captions effective:
Beginners often make these errors:
background-color
or font-weight
and position captions with caption-side: bottom
for a polished look.
Create a simple HTML file and experiment with headers and captions. Try different scope values and CSS styles.
<!DOCTYPE html>
<html lang="en">
<body>
<h3>Weekly Schedule</h3>
<table style="border: 1px solid navy; border-collapse: collapse;">
<caption style="caption-side: top; font-weight: bold;">Daily Tasks</caption>
<tr>
<th scope="col" style="border: 1px solid navy; padding: 8px; background-color: #e6f0ff;">Time</th>
<th scope="col" style="border: 1px solid navy; padding: 8px; background-color: #e6f0ff;">Task</th>
</tr>
<tr>
<td style="border: 1px solid navy; padding: 8px;">9:00 AM</td>
<td style="border: 1px solid navy; padding: 8px;">Meeting</td>
</tr>
</table>
</body>
</html>
Time | Task |
---|---|
9:00 AM | Meeting |
By mastering <th> and <caption> tags, you can create accessible and clear tables that enhance your website’s usability. Practice regularly to get comfortable!