<th>
Establishes introductory header content cell in a table
Definition and Usage
The <th> element establishes a header cell in
an HTML table.
An HTML table has two kinds of cells:
-
Header cells - contains header information
(created with the
<th>element) -
Data cells - contains data (created with the
<td>element)
The text in <th> elements are
bold and centered by default.
The text in <td> elements are regular and
left-aligned by default.
scope attribute to
specify whether a header cell is a header for a column, row, or
group of columns or rows.
<th> element with the
scope attribute helps screen readers understand the
table structure better.
Browser support and standards
Browser rendering and modern HTML conformance are different questions. Check the current element reference for the browsers and features your project needs; the older source version table is not a current compatibility guarantee.
Read the MDN element referenceAttributes
| Attribute | Value | Description |
|---|---|---|
| scope | col row colgroup rowgroup |
Specifies whether a header cell is a header for a column, row, or group of columns or rows |
| colspan | number | Specifies the number of columns a header cell should span |
| rowspan | number | Specifies the number of rows a header cell should span |
| headers | header_id | Specifies one or more header cells a cell is related to |
| abbr | text | Specifies an abbreviated version of the content in a header cell |
Deprecated Attributes
The following attributes are deprecated in HTML5 and should not be used. Use CSS instead:
| Attribute | Value | Description |
|---|---|---|
| Deprecated align | left right center justify char |
Deprecated in HTML5 specifications. Use CSS instead. Specifies the horizontal alignment |
| Deprecated bgcolor | rgb(x,x,x) #xxxxxx colorname |
Deprecated in HTML5 specifications. Use CSS instead. Specifies the background color |
| Deprecated height | pixels % |
Deprecated in HTML5 specifications. Use CSS instead. Specifies the height |
| Deprecated width | pixels % |
Deprecated in HTML5 specifications. Use CSS instead. Specifies the width |
| Deprecated valign | top middle bottom baseline |
Deprecated in HTML5 specifications. Use CSS instead. Specifies the vertical alignment |
Examples
Basic Table with Header Cells
Create a simple table with header cells:
Example
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>John Doe</td>
<td>30</td>
<td>USA</td>
</tr>
<tr>
<td>Jane Smith</td>
<td>25</td>
<td>Canada</td>
</tr>
</table>
Using the Scope Attribute
Use the scope attribute for better accessibility:
Example
<table>
<thead>
<tr>
<th scope="col">Product</th>
<th scope="col">Price</th>
<th scope="col">Quantity</th>
</tr>
</thead>
<tbody>
<tr>
<td>Laptop</td>
<td>$999</td>
<td>5</td>
</tr>
<tr>
<td>Mouse</td>
<td>$29</td>
<td>15</td>
</tr>
</tbody>
</table>
Header Cell with Colspan
Span header cells across multiple columns:
Example
<table>
<tr>
<th colspan="2">Personal Information</th>
</tr>
<tr>
<th scope="col">Name</th>
<th scope="col">Email</th>
</tr>
<tr>
<td>John Doe</td>
<td>john@example.com</td>
</tr>
</table>
Using the Abbr Attribute
Provide abbreviated versions for screen readers:
Example
<table>
<tr>
<th scope="col" abbr="Prod">Product Name</th>
<th scope="col" abbr="Desc">Description</th>
<th scope="col" abbr="Qty">Quantity Available</th>
</tr>
<tr>
<td>Wireless Keyboard</td>
<td>Ergonomic wireless keyboard with backlight</td>
<td>25</td>
</tr>
</table>
Row Headers with Scope
Use header cells for rows:
Example
<table>
<tr>
<th scope="row">Monday</th>
<td>Closed</td>
</tr>
<tr>
<th scope="row">Tuesday</th>
<td>9:00 AM - 5:00 PM</td>
</tr>
<tr>
<th scope="row">Wednesday</th>
<td>9:00 AM - 5:00 PM</td>
</tr>
</table>
Complex Table with Headers
Create a complex table with both column and row headers:
Example
<table>
<thead>
<tr>
<th></th>
<th scope="col">Q1</th>
<th scope="col">Q2</th>
<th scope="col">Q3</th>
<th scope="col">Q4</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Sales</th>
<td>$50,000</td>
<td>$65,000</td>
<td>$72,000</td>
<td>$85,000</td>
</tr>
<tr>
<th scope="row">Expenses</th>
<td>$35,000</td>
<td>$40,000</td>
<td>$38,000</td>
<td>$42,000</td>
</tr>
</tbody>
</table>
Try it Yourself
Interactive Example
Here's a live example of a table with header cells:
| Product | Price | Stock |
|---|---|---|
| Laptop | $999 | 12 |
| Mouse | $29 | 45 |
| Keyboard | $79 | 23 |
th vs td - Understanding the Difference
| Feature | <th> | <td> |
|---|---|---|
| Purpose | Header cell containing header information | Data cell containing actual data |
| Default Styling | Bold and centered | Regular and left-aligned |
| Scope Attribute | Can use scope="col" or scope="row" | Not applicable |
| Accessibility | Helps screen readers identify headers | Regular data content |
| Typical Use | Column headers, row headers, table titles | Data values in table body |
Accessibility with Scope and Headers Attributes
The scope and headers attributes are
crucial for accessibility:
- scope="col" - Indicates the header applies to a column
- scope="row" - Indicates the header applies to a row
- scope="colgroup" - Indicates the header applies to a group of columns
- scope="rowgroup" - Indicates the header applies to a group of rows
- headers - Associates data cells with specific header cells by ID
scope attribute on <th> elements
in data tables. This helps screen reader users understand the
table structure and navigate more efficiently.
Best Practices
-
Always use
<th>for header cells and<td>for data cells -
Include the
scopeattribute on all<th>elements for better accessibility -
Use
scope="col"for column headers andscope="row"for row headers -
Place column headers inside
<thead>for better semantic structure -
Use the
abbrattribute to provide abbreviated versions for long headers -
Avoid using deprecated attributes like
align,bgcolor, orvalign- use CSS instead -
For complex tables, consider using the
headersattribute to explicitly associate data cells with header cells - Keep header text concise and descriptive
Default CSS Settings
Most browsers will display the <th> element
with the following default values:
Default CSS
Unavailable or blocked: markup context “Matching lesson markup is not supplied; this fragment uses a generic HTML scaffold.”. Provide inline styles and scripts, embedded images or the missing HTML context. External requests and browser dialogs stay blocked.
th {
display: table-cell;
vertical-align: inherit;
font-weight: bold;
text-align: center;
}