Tag th In HTML

The purpose of this post is to explore Tag th <th> with examples so that it may be of assistance to you.

The <th> tag in HTML is used to define a header cell in a table. It is used in combination with the <tr> and <td> tags to structure tabular data and provide additional context and information to users

Tag th stands for table head – The following is an basic HTML table with rows and table head cells:

Example: 

<!DOCTYPE html> <html> <body> <h1>Product List</h1> <table> <thead> <tr> <th>Product ID</th> <th>Product Name</th> <th>Price</th> </tr> </thead> <tbody> <tr> <td>A001</td> <td>Apple</td> <td>$1.50</td> </tr> <tr> <td>B002</td> <td>Banana</td> <td>$2.00</td> </tr> <tr> <td>O003</td> <td>Orange</td> <td>$3.00</td> </tr> </tbody> <tfoot> <tr> <td>Total</td> <td>$6.50</td> </tr> </tfoot> </table> </body> </html>

An example of a table with two rows and two head cells, with the content inside the head cells aligned to the center with CSS:

Example: 

<!DOCTYPE html> <html> <head> <style> table { width: 80%; border-collapse: collapse; margin: 20px auto; } thead, tfoot { background-color: maroon; } th { border: 1px solid #ddd; padding: 8px; text-align: center; } </style> </head> <body> <h1>Product List</h1> <table> <thead> <tr> <th>Product ID</th> <th>Product Name</th> <th>Price</th> </tr> </thead> <tbody> <tr> <td>A001</td> <td>Apple</td> <td>$1.50</td> </tr> <tr> <td>B002</td> <td>Banana</td> <td>$2.00</td> </tr> <tr> <td>O003</td> <td>Orange</td> <td>$3.00</td> </tr> </tbody> <tfoot> <tr> <td>Total</td> <td>$6.50</td> </tr> </tfoot> </table> </body> </html>

Here is an example to creation of table headers in HTML table:

Example: 

<!DOCTYPE html> <html> <body> <table> <tr> <th>Country</th> <th>Capital</th> </tr> <tr> <td>USA</td> <td>Washington D.C.</td> </tr> <tr> <td>Canada</td> <td>Ottawa</td> </tr> <tr> <td>India</td> <td>New Delhi</td> </tr> <tr> <td>China</td> <td>Beijing</td> </tr> </table> </body> </html>

Here is an example of how to create a table with a caption in HTML:

Example: 

<!DOCTYPE html> <html> <body> <table> <caption>Employee List</caption> <tr> <th>Name</th> <th>Age</th> <th>Position</th> </tr> <tr> <td>John Smith</td> <td>35</td> <td>Manager</td> </tr> <tr> <td>Jane Doe</td> <td>28</td> <td>Developer</td> </tr> <tr> <td>Bob Johnson</td> <td>40</td> <td>Sales</td> </tr> </table> </body> </html>

Here is an example of how the <th> tag can be used to create a unique table cell in HTML:

Example: 

<!DOCTYPE html> <html> <body> <table> <tr> <th>Product</th> <th>Price</th> <th>Availability</th> </tr> <tr> <td>Apple</td> <td>$1.50</td> <td> <div class="unique-cell"> <p>In stock</p> <button>Add to cart</button> </div> </td> </tr> <tr> <td>Banana</td> <td>$0.75</td> <td> <div class="unique-cell"> <p>Out of stock</p> <button disabled>Add to cart</button> </div> </td> </tr> </table> </body> </html>

You can use the vertical align property in CSS to align the content inside a <th> ement vertically. Here is an example:

Example: 

<!DOCTYPE html> <html> <head> <style> table { width: 80%; border-collapse: collapse; margin: 20px auto; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { vertical-align: middle; } </style> </head> <body> <table> <thead> <tr> <th>Product ID</th> <th>Product Name</th> <th>Price</th> </tr> </thead> <tbody> <tr> <td>A001</td> <td>Apple</td> <td>$1.50</td> </tr> <tr> <td>B002</td> <td>Banana</td> <td>$2.00</td> </tr> <tr> <td>O003</td> <td>Orange</td> <td>$3.00</td> </tr> </tbody> </table> </body> </html>

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

Example: 

<!DOCTYPE html> <html> <head> <style> table { border-collapse: collapse; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th:first-child { width: 50px; } </style> </head> <body> <table> <thead> <tr> <th>Product ID</th> <th>Product Name</th> <th>Price</th> </tr> </thead> <tbody> <tr> <td>01</td> <td>Iphone</td> <td>$1400</td> </tr> </tbody> </table> </body> </html>

Table cells can span several rows or columns using the <rowspan> and <colspan> attributes on the <td> and <th> elements.

Below table has cells that span rows and columns:

Example: 

<!DOCTYPE html> <html> <head> <style> table { width: 80%; border-collapse: collapse; margin: 20px auto; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } </style> </head> <body> <table> <thead> <tr> <th rowspan="2">Product ID</th> <th rowspan="2">Product Name</th> <th colspan="2">Price</th> </tr> <tr> <th>Unit Price</th> <th>Total Price</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Chair</td> <td>$10</td> <td>$15.50</td> </tr> <tr> <td>2</td> <td>Table</td> <td>$15</td> <td>$22</td> </tr> <tr> <td>3</td> <td>Fan</td> <td>$8.00</td> <td>$13.20</td> </tr> </tbody> </table> </body> </html>


Tag th Usage

The Tag th <th> indicates a regular header cell in an HTML table.

There are two types of cells in an HTML table:

  • The header cells – hold the header information (made by the <th> tag)
  • The data cell – holds data (made with the <td> element)

Whenever we discuss of Tag <th>, the text in <th> tags is automatically bold and centered. Text within <th> elements is standard and aligned to the left as default.

Common Uses

Here are some of the main uses of the <th> tag:

  • The <th> tag is commonly used to define the header cells in a table. By using <th> cells, developers can provide additional context and information about the data in the table, such as column or row titles, data units, or data types. This can make the table easier to understand and interpret for users.
  • The use of <th> cells in tables can help to improve the accessibility of web pages for users who rely on assistive technologies, such as screen readers. By using <th> cells to structure table data, developers can provide additional information about the table structure and data, which can help to improve the user experience for all users.
  • The <th> tag can be styled differently from the <td> tag, which is used for regular table cells. This can help to distinguish the header cells from the data cells in the table, making the table easier to read and understand.
  • Many table sorting plugins and libraries use the <th> tag to identify the header cells that can be used for sorting. By adding sorting functionality to tables, users can more easily manipulate and analyze the data presented in the table.

Attributes

Global

The Tag th <th> are compatible with the Global Attributes in HTML.

Event

The Tag <th> also accepts the Event Attributes in HTML.


Attributes List

AttributeValueOverview
abbrtextIndicates an abbreviated form of the text that appears in a header cell.
colspannumberDetermines how many columns a cell should have
headersheader_idIndicates which header cells a cell belongs to
rowspannumberDetermine how many rows should be displayed in a cell
scopecol
colgroup
row
rowgroup
Describes if a header cell is a header for a column, row, or collection of columns or rows.

Browser Compatibility

Element
<th>YesYesYesYesYes

Predefined CSS

The following Tag <th> values are used by major browsers for displaying the <th> element:

th {
display: table-cell;
vertical-align: inherit;
font-weight: bold;
text-align: center;
}

HTML Tag th Benefits

The <th> tag in HTML has several benefits, including:

  • By using <th> cells to define the header cells in a table, developers can improve the readability of the table for users. The use of header cells can help to provide context and information about the data in the table, making it easier to understand and interpret.
  • The use of <th> cells in tables can help to improve the accessibility of web pages for users who rely on assistive technologies, such as screen readers. By providing additional information about the structure and data of the table, users with disabilities can more easily understand and navigate the content.
  • The <th> tag is often used to identify header cells that can be used for sorting. This can be useful for users who need to analyze and manipulate large amounts of data in the table.
  • The <th> tag can be styled differently from the <td> tag, which is used for regular table cells. This can help to distinguish the header cells from the data cells in the table, making the table easier to read and understand.
  • The use of <th> cells in tables can improve the semantic structure of the HTML document. By using the appropriate HTML tags for different types of content, developers can create documents that are more easily understood by both humans and machines.
Do subscribe to our Newsletter 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 *