HTML - <th> Tag



In an HTML table, the <th> tag designates the header cell. It needs to be used as a child element of <tr>, which is itself stated in the <table> tag. The <td> tag is utilised to define a standard data cell. Although the browser presents the header cell as bold and centred by default, you can modify this behaviour using CSS properties.

The <th> tag can contain any HTML element that can be used in the HTML document's body including text, images, forms, links, and more. Depending on the size of its contents, the table's size is automatically modified.

Syntax

Following is the syntax for HTML <th> −

<th>
   text
</th>

Example

Let's consider the following example, where we are going to create the table using the two header cells.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 2px solid #D2B4DE;
      }
   </style>
<body>
   <table>
      <tr>
         <th>Name</th>
         <th>Age</th>
      </tr>
      <tr>
         <td>Maya</td>
         <td>23</td>
      </tr>
      <tr>
         <td>Ravi</td>
         <td>22</td>
      </tr>
      <tr>
         <td>Ram</td>
         <td>21</td>
      </tr>
   </table>
</body>
</html>

On running the above code, the output window will pop up, displaying the table applied with two header cells on the webpage.

Example

Considering another scenario where we are going to apply thw background-color to the header cells.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 2px solid #5B2C6F;
      }
   </style>
<body>
   <table>
      <tr>
         <th style="background-color:#D6EAF8 ">Students</th>
         <th style="background-color:#D5F5E3">Marks</th>
      </tr>
      <tr>
         <td>Akhil</td>
         <td>95</td>
      </tr>
      <tr>
         <td>Ravi</td>
         <td>96</td>
      </tr>
      <tr>
         <td>Raju</td>
         <td>99</td>
      </tr>
   </table>
</body>
</html>

When we execute the above code, it will generate an output consisting of the table along with a background color added to the header cells displayed on the webpage.

Example

In the following example, we are going to align the <th> to the right using the CSS.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1.5px solid #DE3163;
      }
   </style>
<body>
   <table style="width:100%">
      <tr>
         <th style="text-align:right">Car</th>
         <th style="text-align:right">Model</th>
      </tr>
      <tr>
         <td>RS7</td>
         <td>2023</td>
      </tr>
      <tr>
         <td>AUDI</td>
         <td>2017</td>
      </tr>
      <tr>
         <td>BENZ</td>
         <td>2014</td>
      </tr>
   </table>
</body>
</html>

On running the above code, the output window will pop up, displaying the data that comes under the <th> tag displayed to the right inside the table.

Example

Following is an example where we are going to set the width of the <th> tag using CSS.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 2px solid #BB8FCE;
      }
   </style>
<body>
   <table style="width:50%">
      <tr>
         <th style="width:25%">Name</th>
         <th style="width:25%">Countrty</th>
      </tr>
      <tr>
         <td>Ram</td>
         <td>India</td>
      </tr>
      <tr>
         <td>Maya</td>
         <td>Greece</td>
      </tr>
      <tr>
         <td>Mamboo</td>
         <td>Nigeria</td>
      </tr>
   </table>
</body>
</html>

When we execute the above code, it will generate an output consisting of the table mentioned with a width using CSS applied to the <th> displayed on the webpage.

html_tags_reference.htm
Advertisements