HTML th tag

Example

A simple HTML table with three rows, two header cells and four data cells:

<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
<tr>
<td>February</td>
<td>Rs.80</td>
</tr>
</table>

Definition and Usage

The <th> tag defines 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.

Browser Support

Element
<th> Yes Yes Yes Yes Yes

Attributes

Attribute Value Description
abbr text Specifies an abbreviated version of the content in a header cell
colspan number Specifies the number of columns a header cell should span
headers header_id Specifies one or more header cells a cell is related to
rowspan number Specifies the number of rows a header cell should span
scope col
colgroup
row
rowgroup
Specifies whether a header cell is a header for a column, row, or group of columns or rows

Global Attributes

The <th> tag also supports the Global Attributes in HTML.

Event Attributes

The <th> tag also supports the Event Attributes in HTML.

More Examples

Example

How to align content inside <th> (with CSS):

<table style="width:100%">
<tr>
<th style="text-align:left">Month</th>
<th style="text-align:left">Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
<tr>
<td>February</td>
<td>Rs.80</td>
</tr>
</table>

Example

How to add background-color to table header cell (with CSS):

<table>
<tr>
<th style="background-color:#FF0000">Month</th>
<th style="background-color:#00FF00">Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
 </table>

Example

How to set the height of a table header cell (with CSS):

<table>
<tr>
<th style="height:100px">Month</th>
<th style="height:100px">Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
</table>

Example

How to specify no word-wrapping in table header cell (with CSS):

<table>
<tr>
<th>Month</th>
<th style="white-space:nowrap">My Savings for a new car</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
</table>

Example

How to vertical align content inside <th> (with CSS):

<table style="width:50%;">
<tr style="height:100px">
<th style="vertical-align:bottom">Month</th>
<th style="vertical-align:bottom">Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
</table>

Example

How to set the width of a table header cell (with CSS):

<table style="width:100%">
<tr>
<th style="width:70%">Month</th>
<th style="width:30%">Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
</table>

Example

How to create table headers:

<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
</tr>
<tr>
<td>SamS</td>
<td>sam.s@lovelife.com</td>
<td>123-45-678</td>
</tr>
</table>

Example

How to create a table with a caption:

<table>
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>Rs.100</td>
</tr>
<tr>
<td>February</td>
<td>Rs.80</td>
</tr>
</table>

Example

How to define table cells that span more than one row or one column:

<table>
<tr>
<th>Name</th>
<th>Email</th>
<th colspan="2">Phone</th>
</tr>
<tr>
<td>SamS</td>
<td>sam.s@lovelife.com</td>
<td>123-45-678</td>
<td>212-00-546</td>
</tr>
</table>

Default CSS Settings

Most browsers will display the <th> element with the following default values:

th {
display: table-cell;
vertical-align: inherit;
font-weight: bold;
text-align: center;
}

Comments