Tag <thead> In HTML

The Tag thead is discussed in this article using examples, in an effort to fulfill educational objectives.

The <thead> tag in HTML is used to define the header section of a table. The header section typically contains one or more rows of table header cells (defined using the <th> tag) that describe the data in the columns below.

The <thead> tag is often used in combination with the <tbody> and <tfoot> tags to divide the content of the table into sections with different purposes. The <thead> section is usually placed immediately after the <table> tag and before any <tbody> or <tfoot> sections.

Html Tag Thead stands for The Table Head element – An example of an HTML table with a <thead>, <tbody>, and <tfoot> tag:

Example: 

<!DOCTYPE html> <html> <body> <table> <thead> <tr> <th>Product ID</th> <th>Product Name</th> <th>Unit Price</th> <th>Quantity</th> <th>Total Price</th> </tr> </thead> <tbody> <tr> <td>A001</td> <td>Apple</td> <td>$1.50</td> <td>3</td> <td>$4.50</td> </tr> <tr> <td>B002</td> <td>Banana</td> <td>$2.00</td> <td>3</td> <td>$6.00</td> </tr> <tr> <td>O003</td> <td>Orange</td> <td>$3.00</td> <td>3</td> <td>$9.00</td> </tr> </tbody> <tfoot> <tr> <td>Total</td> <td>$19.50</td> </tr> </tfoot> </table> </body> </html>

Here’s an example of styling the <thead>, <tbody>, and <tfoot> elements of an HTML table with CSS:

Example: 

<!DOCTYPE html> <html> <head> <title>Styled Table</title> <style> thead { background-color: #fcb2b2; } th, td { border: 1px solid rgb(179, 133, 133); padding: 8px; text-align: left; } tfoot { background-color: maroon; font-weight: bold; color:white; } tbody { color: gray; } </style> </head> <body> <table> <thead> <tr> <th>Event Name</th> <th>Date</th> <th>Location</th> </tr> </thead> <tbody> <tr> <td>Concert</td> <td>Jan 20, 2022</td> <td>New York</td> </tr> <tr> <td>Festival</td> <td>Feb 10, 2022</td> <td>Los Angeles</td> </tr> <tr> <td>Exhibition</td> <td>Mar 15, 2022</td> <td>Chicago</td> </tr> </tbody> <tfoot> <tr> <td>Upcoming events</td> </tr> </tfoot> </table> </body> </html>

Here is an example of the align content using CSS to the <thead>:

Example: 

<!DOCTYPE html> <html> <head> <title>Styled Table</title> <style> thead { background-color: #fcb2b2; text-align: center; } th, td { border: 1px solid rgb(179, 133, 133); padding: 8px; text-align: right; } tfoot { background-color: maroon; font-weight: bold; color:white; } tbody { color: gray; } </style> </head> <body> <table> <thead> <tr> <th>Event Name</th> <th>Date</th> <th>Location</th> </tr> </thead> <tbody> <tr> <td>Concert</td> <td>Jan 20, 2022</td> <td>New York</td> </tr> <tr> <td>Festival</td> <td>Feb 10, 2022</td> <td>Los Angeles</td> </tr> </tbody> <tfoot> <tr> <td>Upcoming events</td> </tr> </tfoot> </table> </body> </html>

You can use the width property in CSS to set the width of a table header cell <thead> tag. Here is an example:

Example: 

<!DOCTYPE html> <html> <head> <style> thead { width: 50px; } th, td { border: 1px solid; padding: 8px; text-align: left; } </style> </head> <body> <table> <thead> <tr> <th>Company</th> <th>Created</th> <th>Location</th> </tr> </thead> <tbody> <tr> <td>Goolge</td> <td>September 4, 1998</td> <td>Menlo Park, California, United States</td> </tr> <tr> <td>Tesla</td> <td>July 01, 2003</td> <td>San Carlos, California, United States</td> </tr> </tbody> </table> </body> </html>

Here is another example of the <thead> tag with CSS:

Example: 

<!DOCTYPE html> <html> <head> <style> table { width: 100%; border-collapse: separate; border-spacing: 0px 10px; } thead { background-color: #333; color: #fff; } th, td { padding: 20px; text-align: center; border: 2px solid #444; } th { font-size: 1.2em; border-bottom: none; } td { font-size: 1em; border-top: none; } </style> </head> <body> <table> <thead> <tr> <th>Name</th> <th>Age</th> <th>City</th> </tr> </thead> <tbody> <tr> <td>Lionel Andrés Messi</td> <td>35</td> <td>Rosario, Argentina</td> </tr> <tr> <td>Cristiano Ronaldo</td> <td>37</td> <td>Hospital Dr. Nélio Mendonça, Funchal, Portugal</td> </tr> <tr> <td>Gerard Piqué</td> <td>35</td> <td>Barcelona, Spain</td> </tr> </tbody> </table> </body> </html>
Advice: Typically, the <thead>, <tbody>, and <tfoot> tags will not impact the table’s layout. In order to style these elements, you will need to use CSS.


Tag <thead> Uses

To arrange header information in HTML tables, use the Tag Thead <thead>.

To indicate a table’s head (body and footer), add the Tag <thead> with the Tag <tbody> and Tag <tfoot> elements.

By using these elements, browsers can navigate the table body on their own from the header and footer. Furthermore, if a large table occupies multiple pages, the table header and footer can be displayed at the beginning and end of each.

IMPORTANT: The <thead> element should contain one or multiple <tr> tags within it.

The Tag <thead> should be positioned accordingly: under a <table> element, followed by <caption>, <colgroup> elements prior to <tbody> , <tfoot> and <tr>.

Common Uses

Here are some common uses of the <thead> tag:

  • The <thead> tag is used to define the headers of a table. The header cells provide a brief description of the data that is contained in each column.
  • The <thead> tag is often used in conjunction with JavaScript libraries to provide dynamic sorting of table data. When a user clicks on a header cell, the JavaScript code can sort the data based on the contents of that column.
  • By using the <thead> tag to define the headers of a table, developers can improve the accessibility of the table for users who rely on assistive technologies such as screen readers. The header cells are announced separately from the data cells, making it easier for users to understand the structure of the table.
  • The <thead> tag can be used to create a more structured and organized table. By visually separating the header cells from the rest of the table, developers can make it easier for users to understand the relationships between the data.
  • The <thead> tag can be styled using CSS to change the appearance of the table headers. This can help to make the table more visually appealing and easier to read.

Attributes

Global

The Tag <thead> also accepts the Global Attributes in HTML.

Event

The Tag Thead <thead> includes the Event Attributes in HTML.


Browser Compatibility

Element
<thead>YesYesYesYesYes

Predefined CSS

The majority of browsers present the <thead> tag with the values listed below:

thead {
display: table-header-group;
vertical-align: middle;
border-color: inherit;
}

Tag <thead> Benefits

The <thead> tag in HTML provides several benefits, including:

  • By using the <thead> tag to define the header section of a table, developers can create more structured and organized tables. The header cells are visually separated from the rest of the table, making it easier for users to understand the relationships between the data.
  • The <thead> tag can improve the accessibility of tables for users who rely on assistive technologies such as screen readers. By identifying the header cells using the <th> tag within the <thead> section, screen readers can announce the headers separately from the data cells, making it easier for users to understand the structure of the table.
  • Tables can be divided into different sections using the <thead>, <tbody>, and <tfoot> tags. This allows developers to provide additional information or structure to the table without cluttering the main data cells. For example, the <tfoot> tag can be used to display summary information such as totals or averages.
  • The <thead> tag can be styled using CSS to change the appearance of the table headers. For example, developers can change the font size, color, or background color of the headers to make them stand out more.
Do leave your reaction below in order to be in touch with the latest technical information on this site.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *