th HTML

HTML <th> Tag

A cell in an HTML table can be of two types:

  • Header cell – The header information is placed inside a header cell. It is created using the <th> element.
  • Data Cells – The main data is placed inside a data cell. It is created using the <td> element.

To define the header cells in an HTML table, the HTML <th> tag is used as the child element of <tr> (table row) tag. By default, the content of the <th> tag is displayed in bold and centered. A table in HTML has auto-adjustable size as per the content size.

Syntax:

<th>.......</th>

Example:

<!DOCTYPE html>
<html>
<body>
<h3>Students Table</h3>
<table style="width:50%" border="2">
  <tr>
    <th>NAME</th>
    <th>AGE</th> 
    <th>CITY</th>
  </tr>
  <tr>
    <td>Tom</td>
    <td>10</td>
    <td>London</td>
  </tr>
  <tr>
    <td>Jerry</td>
    <td>8</td>
    <td>London</td>
  </tr>
  <tr>
    <td>Bruno</td>
    <td>12</td>
    <td>Wells</td>
  </tr>
</table>
</body>
</html>

Explanation:

In the above example, a table is created with three header cells namely, “NAME,” “AGE”, and “CITY”. Here, the width of the table is also defined to be 50%. A border is added to the table of size 2 pixels using the border attribute of the HTML table.

Tag specific Attributes:

Attribute Value Uses HTML 5
abbr text Used to define the abbreviated version of the cell content. Not supported in HTML5.
align left

right

center

justify

char

Used to align a cell content. Not supported in HTML5.
axis category_name Used to categorize cells. Not supported in HTML5.
bgcolor rgb(x,x,x)

#xxxxxx

colorname

Used to define the background color of a cell. Not supported in HTML5.
char character Used to align a cell content to a character. Not supported in HTML5.
charoff number Used to define the number of characters the content will be aligned from the character specified by the char attribute. Not supported in HTML5.
colspan number Used to indicate the number of columns a cell should span.
headers header_id Used to indicate one or more header cells a cell is related to.
height pixels

Used to define the height of a cell.

Not supported in HTML5.
nowrap nowrap Used to instruct that the content inside a cell should not wrap. Not supported in HTML5.
rowspan number Used to define the number of rows a cell should span.
scope col

colgroup

row

rowgroup

Used to define a way to associate header cells and data cells in a table. Not supported in HTML5.
valign top

middle

bottom

baseline

Used to vertically align the content in a cell. Not supported in HTML5.
width pixels

Used to define the width of a cell.

Not supported in HTML5.

Global Attributes:

The HTML Global attributes are supported by the HTML <th> tag.

Event Attributes:

The HTML Event attributes are supported by the HTML <th> tag.

Supporting Browser:

Chrome, IE, Firefox, Opera, and Safari.