The <th> tag stands for table heading. It is used to define a single data cell in the HTML table. The text in a cell defined by the <th> tag is bold and center-aligned.
The
<table>tag is used to create a table. The<tr>tag is used to create a row in the header, body, or footer.
The <th> tag is used in the <tr> tag to define a single cell that will contain a heading. The <th> tag is mostly used in cells in the first row or first column.
<th> Heading1 </th>
The above snippet of code describes how to write the <th> tag in HTML. We begin by opening the tags using the <> symbol. We then close the tags using the </> symbol. Once again, we will place the name of our tag in-between <> and after the / symbol.
We can use attributes with the <th> tag. The most commonly used attribute with the <th> tag is scope.
The scope attribute defines if the heading in the cell is for a row or a column.
If the heading is for a row, the scope is defined as:
<th scope="row">Heading 1</th>
If the heading is for a column, the scope is defined as:
<th scope="col">Heading 1</th>
More attributes used with the <th> tag can be found here.
The above example depicts the usage of the <th> tag.
In the above snippet, the header and the body are defined using the <thead> and <tbody> tags.
There is a single row in the header, and each cell of this row is a heading cell with scope="col". The scope is set to column because this row acts as a heading for all the rows below it.
In the body, there are two rows. Each row is defined using the <tr> tag.
Each row is divided into four cells. First, a heading cell is defined by the <th> tag with scope="row". The scope is set to row because it acts as a heading for all the values towards the right of it.
Then there are three standard data cells defined by the <td> tag. The entire row is divided according to the number of <td> or <th> cells used inside the <tr> tag.
Free Resources