HTML - <thead> Tag



In an HTML table, header content is grouped using the <thead> tag. The <thead> element is used in conjunction with the <tbody> and <tfoot> elements to provide the header, body, and footer of a table. By default, the <thead>, <tbody>, and <tfoot> elements have no impact on how the table is organised. However, you can style these elements with CSS.

These elements can be used by browsers to enable table body scrolling separate from header and footer scrolling. These features can also make it possible for the table header and footer to be printed at the top and bottom of each page when publishing a large table that spans multiple pages.

Syntax

Following is the syntax for HTML <thead> tag −

<thead>...</thead>

Example

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>

On running the above code, the output window will pop up, consisting of the table displayed on the webpage.

Example

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>

When we execute the above code, it will generate an output consisting of the table with CSS applied to the <thead>, <tbody>, and <tfoot> displayed on the webpage.

Example

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>

On running the above code, the output window will pop up, displaying the table on the webpage along with the content inside the <thead> tag displayed to the right.

Example

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>

When we execute the above code, it will generate an output consisting of the table displayed on the webpage along with content inside the <thead> tag that is aligned to the middle using the CSS vertical align property.

html_tags_reference.htm
Advertisements