th
The th element defines a header cell in a table. It establishes relationships between headers and data cells, which is critical for accessibility. Always use the scope attribute to specify what cells the header applies to.
Description
The <th> (table header) element defines a cell that acts as a header for a group of table cells. Headers can apply to columns, rows, or groups of either. The scope attribute explicitly declares which cells the header describes.
Vanilla Breeze styles header cells with a raised background, semi-bold font weight, and a thicker bottom border to distinguish them from data cells.
When to Use
- Column headers: In
<thead>rows withscope="col" - Row headers: First cell of data rows with
scope="row" - Group headers: Headers spanning multiple columns/rows with
scope="colgroup"orscope="rowgroup"
When to Use td Instead
- Data cells: Cells containing actual data values
- Summary cells: Totals in
<tfoot>(though the label cell may be th)
The scope Attribute
The scope attribute is essential for accessibility. It tells assistive technologies which cells the header applies to.
| Value | Description | Use Case |
|---|---|---|
col |
Header applies to all cells in the column | Column headers in thead |
row |
Header applies to all cells in the row | Row headers in first cell of tbody rows |
colgroup |
Header applies to multiple columns | Group headers with colspan |
rowgroup |
Header applies to multiple rows | Group headers with rowspan or section headers |
Important: Always include the scope attribute. Screen readers use it to announce which header applies to each data cell.
Column Headers (scope="col")
Most common use: headers in <thead> that describe entire columns.
| Name | Department | Location | Start Date |
|---|---|---|---|
| Alice Johnson | Engineering | New York | 2022-03-15 |
| Bob Smith | Design | San Francisco | 2021-08-22 |
Row Headers (scope="row")
Use when the first cell of a row identifies that row's data.
| Plan | Basic | Pro | Enterprise |
|---|---|---|---|
| Monthly Price | $9 | $29 | $99 |
| Storage | 5 GB | 50 GB | 500 GB |
| Users | 1 | 10 | Unlimited |
| Support | Priority | 24/7 Phone |
Column Group Headers (scope="colgroup")
Use with colspan for headers that span multiple columns.
| Region | Q1 | Q2 | ||
|---|---|---|---|---|
| Revenue | Profit | Revenue | Profit | |
| North | $150K | $45K | $175K | $52K |
| South | $120K | $36K | $140K | $42K |
Row Group Headers (scope="rowgroup")
Use for headers that apply to multiple rows, often as section dividers.
| Item | Budget | Actual |
|---|---|---|
| Engineering | ||
| Software | $25,000 | $23,500 |
| Hardware | $50,000 | $48,200 |
| Marketing | ||
| Advertising | $30,000 | $32,100 |
| Events | $15,000 | $14,800 |
Numeric Headers
Use data-numeric on header cells for numeric columns to right-align both the header and its associated data cells.
| Product | Quantity | Unit Price | Total |
|---|---|---|---|
| Widget A | 150 | $25.00 | $3,750.00 |
| Widget B | 75 | $40.00 | $3,000.00 |
Accessibility
- Always use scope: The
scopeattribute is essential for screen readers to associate headers with data cells - Screen reader announcement: When navigating to a data cell, the associated headers are announced
- Complex tables: For very complex tables with multiple header levels, consider using
headersattribute on<td>elements - Implicit role: th has an implicit ARIA role of
columnheader(scope="col") orrowheader(scope="row")
Testing Header Associations
Use browser developer tools or screen readers to verify that data cells correctly announce their headers. Navigate through the table with a screen reader to ensure relationships are clear.
Styling
Vanilla Breeze applies these styles to th elements:
| Property | Value | Purpose |
|---|---|---|
padding |
var(--size-s) var(--size-m) |
Consistent spacing |
text-align |
start |
Left-aligned (RTL-aware) |
font-weight |
600 |
Semi-bold for emphasis |
background |
var(--color-surface-raised) |
Subtle distinction from data cells |
border-block-end |
var(--border-width-medium) |
Thicker border for headers |
Attributes
| Attribute | Values | Description |
|---|---|---|
scope |
col, row, colgroup, rowgroup |
Specifies which cells the header applies to |
colspan |
Positive integer | Number of columns the cell spans |
rowspan |
Positive integer | Number of rows the cell spans |
abbr |
String | Abbreviated version of header content for screen readers |
data-numeric |
Boolean attribute | Right-aligns content for numeric columns |