Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The <th> tag in HTML is used to define a table header cell. It is typically used in tables to mark a cell that will hold a heading for a particular column or row. The content inside a <th> tag is usually bold and centered by default to distinguish it from regular table data cells. This tag plays an important role in making tables more readable and organized.
In this article, we’ll discuss the syntax, attributes, examples, and browser compatibility of the <th> tag.
Definition and Usage
The `<th>` tag in HTML stands for "table header." It is used to define header cells in an HTML table. Header cells are special because they provide labels or titles for the columns or rows in a table, making it easier to understand the data. Unlike regular table cells (defined by the `<td>` tag), the content inside a `<th>` tag is bold & centered by default, which helps it stand out.
The `<th>` tag is always placed inside a `<tr>` (table row) tag. It works alongside the `<td>` tag, which defines standard table cells. While `<td>` is used for regular data, `<th>` is used for headers. This distinction is important because it helps browsers & screen readers interpret the table structure correctly, improving accessibility.
Here, the "Personal Information" header spans two columns, combining "Name" & "Age" under one header.
If you use the `<th>` tag correctly, it not only improves the visual presentation of your tables but also enhances accessibility for users relying on screen readers.
Syntax
The basic syntax of the <th> tag is:
<th>Header Content</th>
The <th> tag is always used within the <table> tag. It is generally nested inside a <tr> (table row) tag. Here’s a minimal example of using the <th> tag:
<table>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</table>
Explanation:
<table> defines the table structure.
<tr> creates a row in the table.
<th> defines header cells for that row.
Attributes of the <th> Tag
The <th> tag supports several attributes to enhance its functionality. Let’s have a look at the most commonly used ones:
1. colspan
The colspan attribute specifies how many columns the header cell should span.
The "Name" header spans two rows, while "Marks" header spans two columns, effectively creating a nested header structure.
Supported Browsers
The <th> tag is supported by all major browsers, including:
he updated table:
Browser
Support
Chrome
Yes
Firefox
Yes
Edge
Yes
Safari
Yes
Opera
Yes
Since the <th> tag is part of the core HTML specification, it is compatible with most modern and older browsers.
Frequently Asked Questions
What is the purpose of the <th> tag?
The <th> tag is used to create header cells in a table. These cells help organize data by labeling columns or rows, making the table content easier to understand.
Can I use the <th> tag without a <table>?
No, the <th> tag must be used inside a <table> tag. It is specifically designed for creating table headers.
What is the difference between <th> and <td>?
<th> is used for table headers, and its content is bold and centered by default.
<td> is used for table data cells, and its content is left-aligned by default.
Conclusion
The <th> tag is an essential part of HTML tables, helping to define clear and structured header cells. In this article, we covered the syntax, attributes, practical examples, and browser compatibility of the <th> tag. Whether you're building simple tables or complex data grids, understanding the <th> tag is key to creating accessible and well-organized tables.