How to create table header in HTML?
The <th> element is used to define table header cells in HTML. It is typically placed within the <tr> element, which represents a table row.
The <th> element is similar to the <td> element used for regular table cells, but it is typically styled differently to visually distinguish the table header.
- The <table> element represents the table itself.
- Inside the table, we have a <tr> element, which represents a table row.
- Within the table row, we have multiple <th> elements, each representing a table header cell.
- In the above example, we have five header cells labeled "Header 1" through "Header 5".
Using CSS to style table headers#
You can use CSS to style the table headers. CSS provides various properties to customize the appearance of table headers, such as background color, font size, alignment, and borders.
- The <style> tag is used to define CSS rules within the HTML document.
- The CSS selector th targets all <th> elements in the document.
- In this example, we set a background color of #f2f2f2, a font size of 18px, center alignment, and a bottom border of 2px solid #ccc
Using a table header group#
The <thead> element can be used to group table header content together. It provides a semantic way to distinguish the header section of a table, making it easier to style and manage.
- The <thead> element is placed within the <table> element.
- Inside the <thead> element, we have the table header row <tr> and its associated header cells <th>.
- The <thead> element groups the header content together and separates it from the table body <tbody>.
Using a table caption#
You can add a caption to your table using the <caption> element. The caption provides a brief description or title for the table and is typically displayed above or below the table.
- The <caption> element is used to define the table caption.
- Inside the
element, you can add the desired caption text, such as "Table Header Example".