Skip to main content

HTML Table Headers

Tables can have headers for each column or row, or for several columns or rows at once. Header cells are made with <th> elements; each <th> is one table cell.

Example

A table with a header row.

Prompt
A table whose first row is header cells (th): Firstname, Lastname, Age, followed by two data rows (Jill Smith 50, Eve Jackson 94).
Output: complete HTML page

Vertical Table Headers

To use the first column as headers, make the first cell of each row a <th> element.

Example

A table with headers running down the left side.

Prompt
A table that uses the first cell in each row as a header (th) so the headers run down the left side: rows Firstname (Jill, Eve), Lastname (Smith, Jackson), Age (94, 50).
Output: complete HTML page

Align Table Headers

By default, header text is bold and centered. To left-align it, use the CSS text-align property on th.

Example

Headers left-aligned with CSS.

Prompt
A table with header cells left-aligned using the CSS text-align property set to left on th, with Firstname/Lastname/Age headers and two data rows.
Output: complete HTML page

Header for Multiple Columns

A header can span two or more columns. Use the colspan attribute on the <th> element. You will learn more about colspan and rowspan in the Table colspan & rowspan chapter.

Example

A header spanning two columns.

Prompt
A table with a header that spans two columns using colspan=2 on a Name th, plus an Age th, then two data rows (Jill Smith 50, Eve Jackson 94).
Output: complete HTML page

Table Caption

You can add a caption that acts as a heading for the whole table with the <caption> tag.

Note: The <caption> tag should come right after the <table> tag.

Example

A table with a caption.

Prompt
A 100%-width table with a caption "Monthly savings" added right after the table tag, header row Month/Savings, and two rows (January $100, February $50).
Output: complete HTML page