th

The th element defines a header cell in a table. Vanilla Breeze styles headers with a raised background and semi-bold weight. Always use scope for accessibility. Supports sort indicators and numeric alignment via data attributes.

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.

Vanilla Breeze styles header cells with a raised background (var(--color-surface-raised)), semi-bold font weight (600), and a thicker bottom border (var(--border-width-medium)) to visually distinguish them from data cells.

When to Use

  • Column headers: In <thead> rows with scope="col"
  • Row headers: First cell of data rows with scope="row"
  • Group headers: Headers spanning multiple columns/rows with scope="colgroup" or scope="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. Always include it.

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 used with colspan
rowgroup Header applies to multiple rows Section headers spanning all columns

Examples

Column and Row Headers

This example shows both scope="col" for column headers and scope="row" for row headers. Screen readers use these to announce context when navigating cells.

Column Group Headers

Use scope="colgroup" with colspan for headers that span multiple columns, commonly seen in multi-level header rows.

Row Group Headers

Use scope="rowgroup" for section headers that apply to multiple rows within a group.

Basic Table

A standard table showing headers in context with data rows.

Sort Indicators

Add data-sort to make a column sortable. Vanilla Breeze appends a sort indicator via ::after. The value specifies the data type for sort logic.

  • data-sort="string" - Alphabetical sorting
  • data-sort="number" - Numeric sorting
  • data-sort="date" - Date sorting

When active, the <data-table> component sets aria-sort="ascending" or aria-sort="descending" on the <th>, which triggers the directional arrow icon.

Numeric and Alignment Attributes

data-numeric

Use data-numeric on header cells for numeric columns. This right-aligns the header text and applies font-variant-numeric: tabular-nums for proper digit alignment.

data-align

Use data-align for explicit text alignment when the default (start) or data-numeric (end) are not appropriate.

VB Data Attributes

Attribute Values Description
data-numeric Boolean attribute Right-aligns content and uses tabular numerals
data-align start, center, end Explicit text alignment override
data-sort string, number, date Enables sort indicator; value sets sort data type

Standard HTML Attributes

Attribute Values Description
scope col, row, colgroup, rowgroup Specifies which cells the header applies to (required for a11y)
colspan Positive integer Number of columns the header spans
rowspan Positive integer Number of rows the header spans
abbr String Abbreviated version of header content for screen readers

CSS Reference

Accessibility

  • Always use scope: The scope attribute 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 tables with multiple header levels, consider using the headers attribute on <td> elements
  • Implicit roles: <th> has an implicit ARIA role of columnheader (with scope="col") or rowheader (with scope="row")
  • Sort state: When a column is sorted, use aria-sort="ascending" or aria-sort="descending" on the <th>

Related

  • <td> - Data cells (th provides headers for)
  • <tr> - Table rows containing th cells
  • <thead> - Primary location for column headers
  • <tbody> - Contains row headers with scope="row"
  • <table> - The table container
  • <data-table> - Interactive table component that uses sort attributes on <th>