How to create table header in HTML

Table header in an HTML table is to provide context and structure to the data contained within the table. The elements used to create the table header help identify the content of each column or row in the table, making it easier for the user to understand and interpret the data. The table header also serves as a visual cue to distinguish the table headings from the rest of the table data. Additionally, the table header can be used to sort and manipulate the table data, depending on the functionality provided by the web application or website. The HTML table header is different from the HTML page header, which contains information about the document as a whole. The HTML table header can be styled using CSS, and can also be used in combination with a table caption for additional context.

Create HTML table header

The HTML table header is typically used to provide descriptive headers for columns or rows of data. For example, in a table of employee information, the table header might include columns for the employee name, title, department, and salary. The <th> element is used to define each header cell in the table, and the content of the cell is typically text.

Following is an example of an HTML table with a header row:

<table> <tr> <th>Name</th> <th>Title</th> <th>Department</th> <th>Salary</th> </tr> <tr> <td>Cris John</td> <td>Manager</td> <td>Marketing</td> <td>$100,000</td> </tr> <tr> <td>Catherine Smith</td> <td>Associate</td> <td>IT</td> <td>$60,000</td> </tr> </table>
run this source code Browser View
Name Title Department Salary
Cris John Manager Marketing $100,000
Catherine Smith Associate IT $60,000

In the above example, the first row of the table contains four header cells defined by the <th> element. These cells identify the content of each column in the table. The remaining rows of the table contain data cells defined by the <td> element.

The HTML table header can also be used to provide a header column for row-based data. For example, in a table of sales data by month, the table header might include a row of month names to identify the data in each row.

Following is an example of an HTML table with a header column:

<table> <tr> <th>Month</th> <td>January</td> <td>$10,000</td> </tr> <tr> <th></th> <td>February</td> <td>$15,000</td> </tr> <tr> <th></th> <td>March</td> <td>$12,000</td> </tr> </table>
run this source code Browser View
Month January $10,000
February $15,000
March $12,000

In the above example, the first column of the table contains three header cells defined by the <th> element. These cells identify the content of each row in the table. The remaining cells of the table contain data cells defined by the <td> element. Note that the second and third rows have an empty <th> element in the first column, which creates a visual cue to align the data cells with the correct month.

Common attributes used with the <th> element

There are several common attributes used with the <th> element in an HTML table header. Following are a few of the most common ones:

Scope

This attribute specifies the scope of the header cell, which can be either "row" or "column". This attribute is used to associate the header cell with the appropriate data cells in the table. For example, if the header cell identifies a column of data, the scope attribute would be set to "col". If it identifies a row of data, the scope attribute would be set to "row".

Colspan

This attribute specifies the number of columns that the header cell should span. This is useful when the header cell applies to multiple columns of data.

Rowspan

This attribute specifies the number of rows that the header cell should span. This is useful when the header cell applies to multiple rows of data.

ID and Class

These attributes are used to identify and style the header cell using CSS. The id attribute can be used to uniquely identify the header cell, while the class attribute can be used to apply a specific style to a group of header cells.

Following is an example of how these attributes might be used in an HTML table header:

<table> <thead> <tr> <th id="name" scope="col">Name</th> <th id="age" scope="col">Age</th> <th id="address" scope="col" colspan="2">Address</th> </tr> </thead> <tbody> <tr> <th scope="row">John Doe</th> <td>35</td> <td>123 Main St.</td> <td>Anytown, USA</td> </tr> <tr> <th scope="row">Jane Smith</th> <td>27</td> <td colspan="2">456 Elm St., Suite 2</td> </tr> </tbody> </table>
run this source code Browser View
Name Age Address
John Doe 35 123 Main St. Anytown, USA
Jane Smith 27 456 Elm St., Suite 2

In the above example, the first row of the table contains three header cells defined by the <th> element, with different attributes applied to each one. The second and third rows of the table contain data cells defined by the <td> element. Note how the scope, colspan, and rowspan attributes are used to associate the header cells with the appropriate data cells in the table.

Difference between a table header and a table caption

In HTML, a table header (<thead>) is used to group together the header content (usually <th> elements ) of a table. On the other hand, a table caption (<caption>) is used to provide a title or a brief explanation about the content of the entire table.

The main differences between a table header and a table caption are:

  1. Placement: The table header is placed at the top of the table, and it usually contains the column headings for the data in the table. The table caption is placed at the top of the table, but outside of the <thead> element, and it usually provides a general description or summary of the table.
  2. Markup: The table header is marked up using the <thead> element, while the table caption is marked up using the <caption> element.

Purpose: The table header is used to group together the header content of a table, and to provide a structure for screen readers and other assistive technologies. The table caption is used to provide additional context or information about the entire table, and it is typically not necessary for the structure of the table.

Following is an example of a table with both a header and a caption:

<table> <caption>Monthly Sales Figures</caption> <thead> <tr> <th>Month</th> <th>Sales</th> </tr> </thead> <tbody> <tr> <td>January</td> <td>$10,000</td> </tr> <tr> <td>February</td> <td>$12,000</td> </tr> </tbody> </table>
run this source code Browser View
Monthly Sales Figures
Month Sales
January $10,000
February $12,000

In the above example, the <thead> element contains the header row of the table, with two <th> elements for "Month" and "Sales". The element provides a title for the table, which is "Monthly Sales Figures".

Make an HTML table header sticky or fixed

Making an HTML table header sticky or fixed means that the header row stays at the top of the screen when the user scrolls down through the table data. This is useful when you have a large table with many rows of data, and you want to keep the column headers visible for reference as the user scrolls through the data.

Using CSS position: sticky

One way to make a table header sticky is to use CSS position: sticky property. This property allows an element to remain positioned relative to its parent container while the user scrolls. To use this method, you need to apply the sticky position to the header row (th) element.

<style> table { width: 100%; border-collapse: collapse; } th { position: sticky; top: 0; background-color: #fff; } td, th { border: 1px solid #ddd; padding: 8px; text-align: left; } </style>
<table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> <th>Header 4</th> </tr> </thead> <tbody> <tr> <td>Row 1, Column 1</td> <td>Row 1, Column 2</td> <td>Row 1, Column 3</td> <td>Row 1, Column 4</td> </tr> <tr> <td>Row 2, Column 1</td> <td>Row 2, Column 2</td> <td>Row 2, Column 3</td> <td>Row 2, Column 4</td> </tr> <!-- more rows here --> </tbody> </table>
run this source code Browser View
Header 1 Header 2 Header 3 Header 4
Row 1, Column 1 Row 1, Column 2 Row 1, Column 3 Row 1, Column 4
Row 2, Column 1 Row 2, Column 2 Row 2, Column 3 Row 2, Column 4

In the above example, the th elements have a position: sticky property with a top value of 0, which means they will stick to the top of the parent container (the table) when the user scrolls. The background-color property is set to white to differentiate the header row from the rest of the table.

Conclusion:

The table header serves as a visual cue to distinguish the table headings from the rest of the table data. Additionally, the table header can be used to sort and manipulate the table data, depending on the functionality provided by the web application or website.