html
  1. html-th-tag

HTML <th> Tag

  • The HTML <th> tag (short for table header cell) represents a header cell in a table.
  • It is used to define the header of a table or a group of rows in a table.

Syntax

<table>
  <thead>
    <tr>
      <th>[Header Content]</th>
      <th>[Header Content]</th>
      <th>[Header Content]</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>[Cell Content]</td>
      <td>[Cell Content]</td>
      <td>[Cell Content]</td>
    </tr>
    <tr>
      <td>[Cell Content]</td>
      <td>[Cell Content]</td>
      <td>[Cell Content]</td>
    </tr>
  </tbody>
</table>

Example

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>Gender</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>John Doe</td>
      <td>32</td>
      <td>Male</td>
    </tr>
    <tr>
      <td>Jane Smith</td>
      <td>27</td>
      <td>Female</td>
    </tr>
  </tbody>
</table>
Try Playground

Explanation

The <th> tag is used as a child element of <thead>, which is a section that groups the header content in a table. The <th> tag is used to define the headers for each column in a table.

Unlike the <th> tag, which is used to define data cells in a table, the <th> tag is used to define header cells in a table. The HTML table element is used to create a table and it contains two main parts, the <thead> and <tbody>. The <thead> element contains the header row(s) of the table, and the <tbody> element contains the data rows of the table.

Use

The <th> tag is used to define table headers. It is useful for making data in tables more understandable and organized. It provides visual clues as to what data is contained within each column.

Important Points

  • The <th> tag can be used both in the <thead> and <tbody> section of the table.
  • By default, table header cells are bold and centered.
  • The colspan and rowspan attributes can be used to merge multiple cells horizontally or vertically.
  • The scope attribute can be used to associate a header cell with a group of data cells.

Summary

The <th> tag is used to define table header cells in HTML tables. It provides visual cues for understanding tabular data and can be used both in the <thead> and <tbody> sections of the table. By default, table header cells are bold and centered. The colspan, rowspan, and scope attributes can be used to merge cells or associate header cells with groups of data cells.

Published on: