W3docs

HTML <th> Tag

The HTML <th> tag defines the header cells in the table which are displayed as bold, center-aligned text. Try the HTML <th> tag example yourself.

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

The <th> tag can contain text, images, forms, links, or any other HTML element that can be used in the body of an 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, you must use <th> elements instead of <td> elements for that row. By default, the content of the <th> tag is bold and centered. To change its appearance, you can use CSS styles. Table headings can also be easily styled independently from the rest of the table contents.

Note that all rows in a table should have the same number of cells. If a row has fewer cells, the browser renders the missing cells implicitly. These implicit cells inherit the table's border styles. If you need to indicate that other cells contain no data, create empty cells where necessary. If implicit cells appear consecutively, they may render as a single merged cell.

Syntax

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

HTML <th> Tag

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

Example of the HTML <th> tag:

Month and Date — Example of the HTML <th> Tag — W3Docs

<!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", which we define using <th> tags. We place both tags inside a <tr> element.

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:

Example with a colspan attribute — HTML <th> Tag — W3Docs

<!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

Note: Attributes such as align, bgcolor, valign, width, height, and others are deprecated in HTML5. Use CSS for styling instead.

AttributeValueDescription
abbrtextDefines 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.
alignleft right centerAligns the content in a header cell. Not supported in HTML 5.
axiscategory_nameCategorizes cells having similar content. Not supported in HTML 5.
backgroundbackgroundSets the background in a cell. Not supported in HTML 5.
bgcolorrgb(x,x,x) #xxxxxx colornameDefines the background color of a cell. Not supported in HTML 5.
bordercolorbordercolorSets the color of a border. Not supported in HTML 5.
charcharacterAligns the content in a header cell to a character. It is used only if the attribute is align="char". Not supported in HTML 5.
charoffnumberSets 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.
colspannumberDefines the number of columns a cell should span. The value of the attribute should be a positive integer. Default value is 1.
headersheader_idSpecifies 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% pixelsSets the height of a cell. Not supported in HTML 5.
nowrapnowrapSpecifies that the content inside a cell should not wrap. Not supported in HTML 5.
rowspannumberSpecifies 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.
scopecol colgroup row rowgroup autoDefines the cells that the header element (defined in <th>) relates to. Essential for table accessibility.
sortedreversed number reversed number number reversedDefines the sort direction of a column. Not supported in HTML 5.
valigntop middle bottom baselineSpecifies vertical alignment of the content inside a cell. Not supported in HTML 5.
width% pixelsSets 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 an HTML <th> tag

th {
  background-color: #f2f2f2;
  color: #333;
  padding: 8px;
  border: 1px solid #ccc;
}

Practice

Practice

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