HTML table headers are used to identify and describe the information displayed in table rows and columns. The <th> element defines a header cell in an HTML table. By default, browsers usually display header text in bold and center it within the cell.
Table headers help users understand the meaning of the data in a table. They are commonly used in student records, employee details, product lists, price comparisons, and financial reports.
In this tutorial, you will learn how to create HTML table headers, use the scope attribute, style header cells with CSS, and improve table accessibility with practical examples.
What Is an HTML Table Header?
An HTML table header is a cell that describes the data in a particular row or column. You create a header cell using the <th> element inside a table row.
For example, a student table can use Name, Age, and Course as its column headers.
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Course</th>
</tr>
<tr>
<td>Rahul</td>
<td>21</td>
<td>HTML</td>
</tr>
<tr>
<td>Priya</td>
<td>22</td>
<td>CSS</td>
</tr>
</table>
In this example, the <th> elements identify the three columns. The <td> elements contain the actual student information.
HTML Table Header Syntax
The basic syntax for creating a table header is shown below.
<table>
<tr>
<th>Header Text</th>
</tr>
</table>
The <table> element creates the table, <tr> defines the row, and <th> creates the header cell.
You can add multiple <th> elements to create headings for different columns.
HTML Table Header Example
The following example demonstrates how to create a complete HTML table with column headers.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Table Headers</title>
</head>
<body>
<h2>Employee Details</h2>
<table>
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Department</th>
<th>Salary</th>
</tr>
<tr>
<td>101</td>
<td>Rahul</td>
<td>IT</td>
<td>₹25,000</td>
</tr>
<tr>
<td>102</td>
<td>Priya</td>
<td>HR</td>
<td>₹28,000</td>
</tr>
</table>
</body>
</html>
In this example, the table header contains Employee ID, Name, Department, and Salary. Each heading describes the information displayed below it.
HTML Table Headers vs Data Cells
HTML provides two important elements for creating table cells: <th> and <td>.
| Feature | <th> | <td> |
|---|---|---|
| Purpose | Defines a header cell | Defines a data cell |
| Typical content | Column or row heading | Actual table information |
| Default appearance | Usually bold and centered | Usually normal weight and left-aligned |
| Accessibility | Identifies header relationships | Provides the corresponding data |
| Location | Inside a table row | Inside a table row |
Use <th> when a cell describes the meaning of other cells. Use <td> when a cell contains ordinary data.
HTML Table Column Headers
Column headers describe the information in each column. They are usually placed in the first row of a table.
<table>
<tr>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
</tr>
<tr>
<td>Keyboard</td>
<td>₹800</td>
<td>2</td>
</tr>
<tr>
<td>Mouse</td>
<td>₹400</td>
<td>3</td>
</tr>
</table>
Here, Product, Price, and Quantity describe the information in their respective columns.
For better accessibility, you can use the scope="col" attribute to identify column headers.
<tr>
<th scope="col">Product</th>
<th scope="col">Price</th>
<th scope="col">Quantity</th>
</tr>
This helps assistive technologies understand which columns the headings describe.
HTML Table Row Headers
A row header describes the information in a particular row. You can create one using <th> inside the relevant table row.
<table>
<tr>
<th scope="row">HTML</th>
<td>Markup Language</td>
</tr>
<tr>
<th scope="row">CSS</th>
<td>Style Sheets</td>
</tr>
<tr>
<th scope="row">JavaScript</th>
<td>Programming Language</td>
</tr>
</table>
In this example, HTML, CSS, and JavaScript are row headers. Each heading identifies the subject described by the adjacent cell.
The scope="row" attribute explicitly identifies each header as applying to its row.
HTML Table Headers with the Scope Attribute
The scope attribute specifies whether a header applies to a row, a column, a row group, or a column group.
| Attribute Value | Description |
|---|---|
scope="col" | Identifies a column header |
scope="row" | Identifies a row header |
scope="colgroup" | Identifies a header for a group of columns |
scope="rowgroup" | Identifies a header for a group of rows |
Example:
<table>
<tr>
<th scope="col">Name</th>
<th scope="col">Marks</th>
</tr>
<tr>
<th scope="row">Rahul</th>
<td>85</td>
</tr>
<tr>
<th scope="row">Priya</th>
<td>92</td>
</tr>
</table>
Here, Name and Marks are column headers, while Rahul and Priya identify individual rows.
The scope attribute is particularly useful for improving the accessibility of tables.
HTML Table Headers with CSS
You can use CSS to change the appearance of table headers. For example, you can add a background color, change the text color, adjust padding, and align the text.
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 12px;
text-align: left;
}
th {
background-color: #0878d1;
color: white;
}
</style>
The th selector targets all table header cells. The blue background and white text help distinguish headings from regular data cells.
You can customize the colors to match your website’s design.
HTML Table Headers with Text Alignment
By default, browsers usually center table header text. You can change the alignment using the CSS text-align property.
<style>
th {
text-align: left;
}
</style>
The text-align: left declaration aligns header text to the left side of each cell.
You can also use center or right depending on the layout. For tables containing prices or numerical values, consistent alignment can make comparisons easier.
HTML Table Headers with Padding and Borders
Padding adds space inside a header cell, while borders create visible boundaries between cells.
<style>
th {
border: 1px solid #333;
padding: 14px;
background-color: #e8f2ff;
}
</style>
In this example, every header cell has a border, additional internal spacing, and a light blue background.
You can also use border-collapse: collapse on the table to combine adjacent borders into a single border.
HTML Table Headers with Colspan
The colspan attribute allows a header cell to span multiple columns. It is useful when you want to group related columns under one heading.
<table>
<tr>
<th colspan="2">Student Information</th>
</tr>
<tr>
<th scope="col">Name</th>
<th scope="col">Course</th>
</tr>
<tr>
<td>Rahul</td>
<td>HTML</td>
</tr>
</table>
The Student Information header spans two columns because its colspan value is 2.
This technique is useful for creating grouped headings in reports and data tables.
HTML Table Headers with the Thead Element
The <thead> element groups the header rows of a table. It helps separate headings from the main data and footer sections.
<table>
<thead>
<tr>
<th scope="col">Product</th>
<th scope="col">Price</th>
<th scope="col">Stock</th>
</tr>
</thead>
<tbody>
<tr>
<td>Laptop</td>
<td>₹50,000</td>
<td>Available</td>
</tr>
<tr>
<td>Monitor</td>
<td>₹12,000</td>
<td>Available</td>
</tr>
</tbody>
</table>
The <thead> element contains the header row, while <tbody> contains the product data.
You can style the table header separately by using the CSS selector thead th.
Common HTML Table Header Mistakes
Avoid these common mistakes when working with HTML table headers:
- Using
<td>instead of<th>for cells that identify columns or rows. - Forgetting to use meaningful headings that describe the data.
- Using CSS alone to make ordinary data cells look like headers without providing the correct HTML semantics.
- Adding too many merged header cells, making relationships difficult to understand.
- Forgetting to consider accessibility when creating tables with multiple header levels.
- Using inconsistent heading labels that make table information confusing.
Using semantic header elements and clear labels helps users understand your table more easily.
HTML Table Header Best Practices
Follow these practices when creating HTML table headers:
- Use
<th>for cells that describe related rows or columns. - Add
scope="col"orscope="row"when appropriate. - Use meaningful, concise header text.
- Use CSS to customize header colors, spacing, borders, and alignment.
- Use
<thead>to group table header rows when it improves the table structure. - Use a
<caption>when the table needs a descriptive title. - For complex tables with multiple levels of headings, use appropriate header associations such as
idandheaderswhen necessary. - Make sure header text remains readable on mobile devices and meets color-contrast requirements.