The <th> tag specifies a header cell in an HTML table. It must be used as a child element of <tr>, which in its turn is declared in the <table> tag. To define a standard data cell, the <td> tag is used.

The <th> tag can contain text, image, form, links or any other HTML element, that can be used in the body of the HTML document. The size of the table is automatically adjusted based on the size of its content.

In HTML tables, data is arranged vertically in columns. If you want to display the first row of the table as labels or headings, for the content in the table’s first row you must use <th> elements instead of <td> elements. By default, the content of the <th> tag is bold and centered. To change its look, you can use CSS styles. Table headings can also be easily styled independently from the rest of the table contents due to this element.

Note that all the rows in the table have an equal number of cells, which is equivalent to the number of cells in the longest row. If there are fewer cells in a row, then the browser will automatically fill the row, placing empty cells at the end of it.

If you need to emphasize that there is no data in other cells, then create cells without content where necessary.

The cells added by browser have no borders, and if they go after each other, they will be shown as one integrated cell.

Syntax

The <th> tag comes in pairs. The content is written between the opening (<th>) and closing (</th>) tags.

<table> 
  <tr> 
    <th>...</th> 
  </tr> 
</table>

Example of the HTML <th> tag:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      table {
        width: 80%;
        margin: 30px auto;
        border-collapse: collapse;
      }
      tr:first-child {
        background-color: #1c87c9;
        color: #fff;
      }
      th,
      td {
        padding: 10px;
        border: 1px solid #666;
      }
    </style>
  </head>
  <body>
    <table>
      <tr>
        <th>Month</th>
        <th>Date</th>
      </tr>
      <tr>
        <td>June</td>
        <td>18.07.2018</td>
      </tr>
    </table>
  </body>
</html>

Result

th example

In this example, our headings are "Month" and "Date", that we define using <th> tags for each of them. Both tags we place in <tr>.

The colspan attribute is generally used with the <th> tag to let the content span over multiple columns. Let’s see this in action.

Example of the HTML <th> tag with the colspan attribute:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      table {
        width: 80%;
        margin: 30px auto;
        border-collapse: collapse;
      }
      tr {
        background-color: #1c87c9;
        color: #ffffff;
      }
      th,
      td {
        padding: 10px;
        border: 1px solid #666;
      }
    </style>
  </head>
  <body>
    <table>
      <tr>
        <th colspan="2">Month and Date</th>
      </tr>
      <tr>
        <td>Jun</td>
        <td>18.07.2014</td>
      </tr>
    </table>
  </body>
</html>

In this example, the value of the colspan attribute is "2". It means that a header cell should span two columns.

Attributes

Attribute Value Description
abbr text Defines an abbreviated version of the content in a header cell, or an alternative text. Some user-agents, such as speech readers, may present this description before the content itself.
Not supported in HTML 5.
align left
right
center
Aligns the content in a header cell.
Not supported in HTML 5.
axis category_name Categorizes cells having similar content.
Not supported in HTML 5.
background background Sets the background in a cell.
Not supported in HTML 5.
bgcolor rgb(x,x,x)
#xxxxxx
colorname
Defines the background color of a cell.
Not supported in HTML 5.
bordercolor bordercolor Sets the color of a border.
Not supported in HTML 5.
char character Aligns the content in a header cell to a character. It is used only if the attribute is align="char".
Not supported in HTML 5.
charoff number Sets the number of characters the content will be aligned from the character specified by the char attribute. Is used only if attribute align="char".
Not supported in HTML 5.
colspan number Defines the number of columns a cell should span. The value of the attribute should be a positive integer. Default value is 1.
headers header_id Specifies a space-separated list of header cells which contains information about this cell. The value needs to correspond to the id of the header cell (set by using the id attribute).
height %
pixels
Sets the height of a cell.
Not supported in HTML 5.
nowrap nowrap Specifies that the content inside a cell should not wrap.
Not supported in HTML 5.
rowspan number Specifies the number of rows a cell should span. The value of the attribute should be a positive integer. The default value is 1. It is not recommended to use values higher than 65534, as they will be clipped down to 65534.
scope col
colgroup
row
rowgroup
auto
Defines the cells that the header element (defined in <th>) relates to.
Not supported in HTML 5.
sorted reversed
number
reversed number
number reversed
Defines the sort direction of a column.
Not supported in HTML 5.
valign top
middle
bottom
baseline
Specifies vertical alignment of the content inside a cell.
Not supported in HTML 5.
width %
pixels
Sets the width of a cell.
Not supported in HTML 5.

The <th> tag also supports the Global Attributes and the Event Attributes.

How to style <th> tag?

Common properties to alter the visual weight/emphasis/size of text in <th> tag:

  • CSS font-style property sets the style of the font. normal | italic | oblique | initial | inherit.
  • CSS font-family property specifies a prioritized list of one or more font family names and/or generic family names for the selected element.
  • CSS font-size property sets the size of the font.
  • CSS font-weight property defines whether the font should be bold or thick.
  • CSS text-transform property controls text case and capitalization.
  • CSS text-decoration property specifies the decoration added to text, and is a shorthand property for text-decoration-line, text-decoration-color, text-decoration-style.

Coloring text in <th> tag:

  • CSS color property describes the color of the text content and text decorations.
  • CSS background-color property sets the background color of an element.

Text layout styles for <th> tag:

  • CSS text-indent property specifies the indentation of the first line in a text block.
  • CSS text-overflow property specifies how overflowed content that is not displayed should be signalled to the user.
  • CSS white-space property specifies how white-space inside an element is handled.
  • CSS word-break property specifies where the lines should be broken.

Other properties worth looking at for <th> tag:

Browser support

chrome edge firefox safari opera

Practice Your Knowledge

What is the purpose of the HTML <th> tag?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.