Contents
HTML th: Main Tips
- Table header cells are defined using HTML
th
tags. - Their text content is bold and centered by default.
- A
<th>
element is usually a child of a <tr> element. - Most of the tag-specific attributes are deprecated in HTML5. It's best to use CSS properties for styling.
Defining Header Cells
HTML tables have two types of cells: header and standard. The content of a table cell can span multiple columns or rows.
The HTML th
tags define a header cell in a table:
<table>
<tr>
<th>Fruits</th>
<th>Prices</th>
</tr>
<tr>
<td>Apple</td>
<td>$1.26</td>
</tr>
<tr>
<td>Banana</td>
<td>$0.60</td>
</tr>
</table>
To define the standard cells, use <td> tags. Their content is displayed as regular text.
Common th Tag Attributes
abbr
defines a short abbreviated description of the cell's content:
<table>
<tr>
<th abbr="fruits">List of Fruits</th>
<th abbr="prices">Price per Unit of Fruit</th>
</tr>
<tr>
<td>Apple</td>
<td>€0.60</td>
</tr>
<tr>
<td>Banana</td>
<td>€0.32</td>
</tr>
</table>
colspan
sets the number of columns a <th>
element should span:
<table>
<tr>
<th colspan="2">List of Fruit Prices</th>
</tr>
<tr>
<td>Apple</td>
<td>€0.60</td>
</tr>
<tr>
<td>Banana</td>
<td>€0.32</td>
</tr>
</table>
headers
defines an ID of a header cell, which a particular header cell is related to:
<table>
<tr>
<th id="name" colspan="2">List of Fruit Prices</th>
</tr>
<tr>
<th headers="name">Fruit</th>
<th headers="name">Price</th>
</tr>
</table>
rowspan
sets the number of rows a <th>
element should span:
<table>
<tr>
<th>Fruits</th>
<th>Prices</th>
<th rowspan="3">Month of June 2017</th>
</tr>
<tr>
<td>Apple</td>
<td>€0.6</td>
</tr>
<tr>
<td>Banana</td>
<td>€0.32</td>
</tr>
</table>
scope
defines whether a header cell is a header for a column, row, or a group of columns or rows:
<table>
<tr>
<th scope="col">Fruits</th>
<th scope="col">Prices</th>
</tr>
<tr>
<td>Apple</td>
<td>€0.6</td>
</tr>
<tr>
<td>Banana</td>
<td>€0.32</td>
</tr>
</table>
Obsolete Attributes
There's also a lot of attributes that were tag-specific to HTML th
, but have been removed in HTML5.
align
defined horizontal alignment of the th
cell's content:
<table width="100%">
<tr>
<th align="left">List of Fruits</th>
<th align="right">Price per Unit of Fruit</th>
</tr>
<tr>
<td>Apple</td>
<td>€0.60</td>
</tr>
<tr>
<td>Banana</td>
<td>€0.32</td>
</tr>
</table>
Note: instead of align, use CSS text-align property.
axis
defined a category for grouping header cells:
<table>
<tr>
<th axis="name">Fruits</th>
<th axis="price">Prices</th>
</tr>
<tr>
<td>Apple</td>
<td>€0.60</td>
</tr>
<tr>
<td>Banana</td>
<td>€0.32</td>
</tr>
</table>
bgcolor
set the background color for a header cell:
<table>
<tr>
<th bgcolor="yellow">Fruits</th>
<th bgcolor="#00FF00">Prices</th>
</tr>
<tr>
<td>Apple</td>
<td>€0.60</td>
</tr>
<tr>
<td>Banana</td>
<td>€0.32</td>
</tr>
</table>
Note: instead of bgcolor, use CSS background-color property.
char
set the alignment of th
cell's content according to the character specified:
<table>
<tr>
<th>Fruits</th>
<th align="char" char=".">Prices €0.00</th>
</tr>
<tr>
<td>Apple</td>
<td>€0.60</td>
</tr>
<tr>
<td>Banana</td>
<td>€0.32</td>
</tr>
</table>
charoff
set the number of characters for aligning the content after the character specified by the char
attribute:
<table>
<tr>
<th>Fruits</th>
<th align="char" char="." charoff="2">Prices €0.00</th>
</tr>
<tr>
<td>Apple</td>
<td>€0.60</td>
</tr>
<tr>
<td>Banana</td>
<td>€0.32</td>
</tr>
</table>
height
set th
cell's height:
<table>
<tr>
<th height="50px" colspan="2">List of Fruit Prices</th>
</tr>
<tr>
<td>Apple</td>
<td>€0.60</td>
</tr>
<tr>
<td>Banana</td>
<td>€0.32</td>
</tr>
</table>
Note: instead of height, use CSS height property.
nowrap
defined that the content inside a header cell should not be wrapped:
<table>
<tr>
<th nowrap colspan="2">Prices per Unit of Fruit</th>
</tr>
<tr>
<td>Apple</td>
<td>€0.60</td>
</tr>
<tr>
<td>Banana</td>
<td>€0.32</td>
</tr>
</table>
Note: instead of nowrap, use CSS white-space property.
valign
defined vertical alignment of the th
cell's content:
<table>
<tr>
<th>Fruits</th>
<th>Prices</th>
<th rowspan="3" valign="top">Month of June 2017</th>
</tr>
<tr>
<td>Apple</td>
<td>€0.60</td>
</tr>
<tr>
<td>Banana</td>
<td>€0.32</td>
</tr>
</table>
Note: instead of valign, use CSS vertical-align property.
width
set th
cell's width:
<table>
<tr>
<th width="50%">Fruits</th>
<th width="100px">Prices</th>
</tr>
<tr>
<td>Apple</td>
<td>€0.60</td>
</tr>
<tr>
<td>Banana</td>
<td>€0.32</td>
</tr>
</table>
Note: instead of width, use CSS width property.