🔥 $100K Hit! Where Will Bitcoin Go Next? Find Out Live!

Code has been added to clipboard!

HTML th Tag

Reading time 3 min
Published Jun 29, 2017
Updated Oct 15, 2019

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:

Example
<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.

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

Common th Tag Attributes

abbr defines a short abbreviated description of the cell's content:

Example
<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:

Example
<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:

Example
<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:

Example
<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:

Example
<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:

Example
<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:

Example
<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:

Example
<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:

Example
<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:

Example
<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:

Example
<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:

Example
<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:

Example
<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:

Example
<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.

Browser support

Browser image
Chrome
1+
Browser image
Edge
All
Browser image
Firefox
1+
Browser image
IE
All
Browser image
Opera
All
Browser image
Safari
All

Mobile browser support

Browser image
Chrome
All
Browser image
Firefox
4+
Browser image
Opera
All
Browser image
Safari
All