<th>
A table header cell: the name of a column or row.
- Display
- table-cell
- Closing tag
- Required
- Permitted content
- Flow content without headings, sectioning elements,
<header>, and<footer>
Example
#<style>
table { border-collapse: collapse; }
th, td { border: 1px solid #8886; padding: 4px 10px; }
</style>
<table>
<tr>
<th scope="col">Course</th>
<th scope="col">Lessons</th>
<th scope="col">Test score</th>
</tr>
<tr>
<th scope="row">HTML and CSS</th>
<td>24</td>
<td>92%</td>
</tr>
<tr>
<th scope="row">JavaScript</th>
<td>32</td>
<td>78%</td>
</tr>
</table>Definition and usage
#The <th> element (from table header) is a header cell, that is, a description of what the data in a column or row means. By default the browser makes its content bold and centers it horizontally.
The scope attribute says what the header applies to: a column (col) or a row (row). Thanks to it, when a screen reader reads a cell with the value 92%, it also gives the headers “Test score” and “HTML and CSS”, not just the number.
In simple tables scope is enough. In complex ones, where a cell belongs to several headers at once, you link it to them with the headers attribute on <td>, pointing to the ids of the relevant <th> elements.
Syntax
#<th scope="col">Column name</th>Attributes
#col | row | colgroup | rowgroup
Specifies which cells the header applies to:colthe column below it,rowthe row next to it, andcolgroupandrowgroupa whole group of columns or rows. Without this attribute the browser tries to guess, which doesn’t always work.positive number (default 1)
The number of columns the cell spans. It’s useful for a header that covers several columns at once.non-negative number (default 1)
The number of rows the cell spans. A row header stretched across several rows groups related data well.abbr
text
A shorter version of the header’s content. A screen reader may use it when it repeats the header for subsequent cells, for exampleabbr="Price"for a header “Monthly price, gross”.headers
list of ids
The ids of other<th>headers that this header falls under, separated by spaces. Needed only in complex tables with headers on several levels.
The element also supports all global attributes, such as class, id, style and hidden.
Attributes in practice
#scope
col | row | colgroup | rowgroupSpecifies which cells the header applies to: col the column below it, row the row next to it, and colgroup and rowgroup a whole group of columns or rows. Without this attribute the browser tries to guess, which doesn’t always work.
<style>
table { border-collapse: collapse; }
th, td { border: 1px solid #8886; padding: 4px 10px; text-align: left; }
</style>
<table>
<tr><th scope="row">Email</th><td>anna@example.com</td></tr>
<tr><th scope="row">City</th><td>Krakow</td></tr>
<tr><th scope="row">Level</th><td>7</td></tr>
</table>colspan
positive number (default 1)The number of columns the cell spans. It’s useful for a header that covers several columns at once.
<style>
table { border-collapse: collapse; }
th, td { border: 1px solid #8886; padding: 4px 10px; }
</style>
<table>
<tr>
<th rowspan="2" scope="col">Student</th>
<th colspan="2" scope="col">Quiz score</th>
</tr>
<tr>
<th scope="col">HTML</th>
<th scope="col">CSS</th>
</tr>
<tr><td>Ola</td><td>9/10</td><td>8/10</td></tr>
<tr><td>Kuba</td><td>7/10</td><td>9/10</td></tr>
</table>rowspan
non-negative number (default 1)The number of rows the cell spans. A row header stretched across several rows groups related data well.
<style>
table { border-collapse: collapse; }
th, td { border: 1px solid #8886; padding: 4px 10px; text-align: left; }
</style>
<table>
<tr><th rowspan="2" scope="row">Module 1</th><td>Tags</td></tr>
<tr><td>Attributes</td></tr>
<tr><th rowspan="2" scope="row">Module 2</th><td>Lists</td></tr>
<tr><td>Tables</td></tr>
</table>Default CSS
#Most browsers display this element with these default styles:
th {
display: table-cell;
padding: 1px;
font-weight: bold;
text-align: center;
vertical-align: inherit;
}Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Don’t replace
<th>with a bold<td>. It will look similar, but assistive technologies won’t recognize the header. - Row headers, for example the course names in the first column, are also
<th>elements, just withscope="row". - You can change the default centering in CSS:
th { text-align: start; }.
Related entries
#Practise it in lessons
#Spotted a mistake or missing an example? Write to us.