HTML - <thead> Tag



HTML <thead> tag is used to group the header content of HTML table. It is used in conjunction with <tbody> and <tfoot> elements to specify each part of a table.

The <thead> tag ahould be used as a child of a <table> element, preceding any <tbody>, <tfoot>, and <tr> elements, and following any <caption> and <colgroup> elements. Browsers can use these elements to enable scrolling of the table body independently of the header and footer.

Syntax

<thead>
   ...
</thead>

Attribute

HTML tfoot tag supports Global and Event attributes of HTML. Bellow mentioned attributes are deprecated so use CSS properties rather using these attributes.

Attribute Value Description
align left
right
center
justify
Specifies the alignment of text content(Deprecated).
bgcolor color Specifies the background color of each column cell(Deprecated).
char character Specifies the alignment of the content to a character of each column cell(Deprecated).
charoff number Specifies the number of characters to offset the column cell content from the alignment character specified by the char attribute(Deprecated).
valign baseline
bottom
middle
top
Specifies the vertical alignment of each column cell(Deprecated).

Examples of HTML thead Tag

Bellow examples will illustrate the usage of thead tag. Where, when and how to use thead tag to create header in HTML.

Creating Header in Table

Let’s look at the following example, where we are going to use the <thead> tag.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1.5px solid #DE3163;
      }
   </style>
<body>
   <table>
      <thead>
         <tr>
            <th>Subjects</th>
            <th>Marks</th>
         </tr>
      </thead>
      <tbody>
         <tr>
            <td>1A</td>
            <td>72</td>
         </tr>
         <tr>
            <td>1B</td>
            <td>74</td>
         </tr>
         <tr>
            <td>2B</td>
            <td>73</td>
         </tr>
      </tbody>
      <tfoot>
         <tr>
            <td>Total</td>
            <td>219</td>
         </tr>
      </tfoot>
   </table>
</body>
</html>

Styling Table

Considering another scenario where we are going to style the <thead>, <tbody>, and <tfoot> using CSS.

<!DOCTYPE html>
<html>
   <style>
      thead {
         color: #6C3483;
      }
      tbody {
         color: #196F3D;
      }
      tfoot {
         color: #DE3163;
      }
      table,
      th,
      td {
         border: 1.5px solid #5DADE2;
      }
   </style>
<body>
   <table>
      <thead>
         <tr>
            <th>Items</th>
            <th>Price</th>
         </tr>
      </thead>
      <tbody>
         <tr>
            <td>5Star</td>
            <td>$20</td>
         </tr>
         <tr>
            <td>KitKat</td>
            <td>$25</td>
         </tr>
         <tr>
            <td>MilkyBar</td>
            <td>$10</td>
         </tr>
      </tbody>
      <tfoot>
         <tr>
            <td>Total Bill</td>
            <td>$55</td>
         </tr>
      </tfoot>
   </table>
</body>
</html>

Align Header Content

Following is an example where we are going to align the content inside the <thead> tag to the right using CSS.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1.5px solid #DE3163;
      }
   </style>
<body>
   <table style="width:100%">
      <thead style="text-align:right">
         <tr>
            <th>Employees</th>
            <th>Age</th>
         </tr>
      </thead>
      <tbody>
         <tr>
            <td>Ram</td>
            <td>23</td>
         </tr>
         <tr>
            <td>Raju</td>
            <td>22</td>
         </tr>
         <tr>
            <td>Maya</td>
            <td>24</td>
         </tr>
      </tbody>
   </table>
</body>
</html>

Vertical Alignment of Header Content

In the following example, we are going to use the CSS vertical align property to align the content inside the <thead> tag.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1.5px solid #A569BD;
      }
   </style>
<body>
   <table style="width:50%">
      <thead style="vertical-align:middle">
         <tr style="height:50px">
            <th>Cars</th>
            <th>Countries</th>
         </tr>
      </thead>
      <tbody>
         <tr>
            <td>Maserati</td>
            <td>Italy</td>
         </tr>
         <tr>
            <td>Porsche</td>
            <td>Germany</td>
         </tr>
         <tr>
            <td>Dodge</td>
            <td>US</td>
         </tr>
      </tbody>
   </table>
</body>
</html>

Supported Browsers

Tag Chrome Edge Firefox Safari Opera
thead Yes Yes Yes Yes Yes
html_tags_reference.htm
Advertisements