HTML th Tag

HTML <th>Tag: The HTML<th> tag specifies a header cell in an HTML table. Two types of cell in the HTML table.

  • Header Cell: It is used to hold the header information.
  • Standard Cell: It is used to hold the body of data.

The working of both tags (<th> and <td>) are same but the text properties are different. In <th> text is bold and centered, and in <td> text is regular and left align by default.

HTML <th>Tag

This HTML th tag supports both the global and the event attributes.

Syntax: <th> Text </th>

Attributes

There are many attributes supported by HTML4.1 but removed from HTML5 are listed below:

  • abbr: This attribute is used as an abbreviated version of the text content in a header cell.
  • align: Set the alignment of the text content.
  • axis: Categories header cells.
  • bgcolor: Set the background color of a header cell.
  • char: Aligns the content in a header cell to a character.
  • charoff: It is used to sets the number of characters that will be aligned from the character specified by the
  • char attribute. The value of these attributes is in numeric form.
  • colspan: Number of columns a header cell should span.
  • headers: Specifies one or more header cells a cell is related to.
  • height: Set the height of a header cell.
  • nowrap: It specifies that the content inside a header cell should not wrap.
  • rowspan: Set the number of rows a header cell should span.
  • scope: It is used to specify the score of header content.
  • sorted: It is used to sort the direction of a column.
  • valign It is used to set the vertical alignment of text content.
  • width: It is used to set the width of a header cell

Browser compatibility

The HTML<th> tag is supported by different types of browsers.

  • Chrome
  • Firefox
  • Opera
  • Safari
  • Internet Explorer

Example:

<!DOCTYPE html>
<html>
<head>
<style>
thead {color:green;}
tbody {color:blue;}
tfoot {color:red;}

table, th, td {
  border: 1px solid black;
}
</style>
</head>

<body>

<table>
  <thead>
    <tr>
      <th>Programming Languages Text</th>
      <th>Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>HTML</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>C</td>
      <td>$80</td>
    </tr>
  </tbody>
</table>
</body>
</html>

Output:

html th tag

Attributes that are supported by the <th> tag

Attribute Value Description
align right
left
center
justify
char
Aligns the content inside the

element

char character Aligns the content inside the

element to a character

charoff number Sets the number of characters the content inside the

element will be aligned from the character specified by the char attribute

valign top
middle
bottom
baseline
Vertical aligns the content inside the

element

rows number Specifies the visible number of lines in a text area
charoff number Sets the number of characters the content will be aligned from the character specified by the char attribute
colspan number Specifies the number of columns a cell should span
nowrap nowrap Specifies that the content inside a cell should not wrap
rowspan number Sets the number of rows a cell should span
scope col
colgroup
row
rowgroup
Defines a way to associate header cells and data cells in a table
width pixels
%
Specifies the width of a cell

Global and event attributes

This tag supports both the global and the event attributes.