<thead>
Overview
The <thead>
element is used to define a set of rows that function as column headers in an HTML table. These headers provide essential context for the data displayed in the rest of the table. Moreover, the element participates in the table model, which outlines its structural role and rendering rules within the table.
Examples and Usage
To better understand the role of <thead>
, let's explore a demonstrative example. We'll examine how <thead>
contributes to defining the structure of an HTML table, both in terms of markup and presentation.
Before diving into the HTML, it's worth mentioning that our table will have two columns: Expense Type and Amount. These columns will be defined within the <thead>
element.
<table>
<thead>
<tr>
<th>Expense Type</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Rent</td>
<td>$1,400</td>
</tr>
<tr>
<td>Utilities</td>
<td>$300</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td>$1,700</td>
</tr>
</tfoot>
</table>
In this example, the <thead>
groups the header rows for "Expense Type" and "Amount," setting the context for the data rows that follow in the <tbody>
and the summary in the <tfoot>
.
Per HTML specifications, the element should appear after any <caption>
or <colgroup>
elements and before <tbody>
, <tfoot>
, or standalone <tr>
elements.
To enhance readability, we'll apply some foundational CSS:
table {
border-collapse: collapse;
border: 2px solid #ccc;
font-size: 18px;
}
The CSS snippet above collapses adjacent cell borders, sets a border for the table, and defines a base font size.
Next, we have rules that target all table cells, setting a light gray border, padding, and text alignment.
th, td {
border: 1px solid #ddd;
padding: 4px 8px;
text-align: left;
}
Finally, we'll target the header cells defined within <thead>
and set a a light gray background color to slighyl distinguish them.
thead > tr > th {
background-color: #f9f9f9;
}
For more complex tables, additional CSS will likely be necessary, based on specific requirements and appropriate design decisions. Furthermore, these tables may also benefit from using ARIA roles for accessibility, adding extra HTML attributes, and incorporating other table-related elements to optimize both accessibility and readability.
Attribute Breakdown
The <thead>
element historically permitted various attributes for controlling alignment and background color. However, most of these attributes are now considered deprecated in favor of CSS. The element also includes all global HTML attributes.
Accessibility Aspects
The <thead>
element has an implicit ARIA role of rowgroup
. Detailed information on this can be found in the official specification.
Associated Elements
<table>
<tbody>
<td>
<tfoot>
<th>
<tr>
<caption>
<col>
<colgroup>
Additional Notes
- Deprecated attributes like
align
,bgcolor
,char
,charoff
, andvalign
were used for alignment and background color within the associated sections of a table. Modern practices replace these with CSS properties such astext-align
,background-color
, andvertical-align
.
Browser Compatibility
For a detailed breakdown of specific browser nuances and older version support refer to the first link in the Useful Resources below.
Browser | Chrome | Edge | Safari | Firefox | Opera | Internet Explorer |
---|---|---|---|---|---|---|
Support | Yes | Yes* | Yes | Yes | Yes* | Yes* |