Skip to main content

HTML Tables

HTML tables let you arrange data into rows and columns.

Example

A table of companies, contacts, and countries.

Prompt
A table of companies with header cells Company, Contact, Country and six data rows: Alfreds Futterkiste/Maria Anders/Germany, Centro comercial Moctezuma/Francisco Chang/Mexico, Ernst Handel/Roland Mendel/Austria, Island Trading/Helen Bennett/UK, Laughing Bacchus Winecellars/Yoshi Tannamuri/Canada, Magazzini Alimentari Riuniti/Giovanni Rovelli/Italy.
Output: complete HTML page

Define an HTML Table

A table is made of table cells arranged in rows and columns, all inside a <table> element.

Example

A simple table with a header row and two data rows.

Prompt
A simple HTML table with a header row (Company, Contact, Country) and two data rows: Alfreds Futterkiste/Maria Anders/Germany and Centro comercial Moctezuma/Francisco Chang/Mexico.
Output: complete HTML page

Table Cells

Each table cell is defined by a <td> and a </td> tag. "td" stands for table data, and everything between the tags is the cell's content.

Note: A cell can hold all kinds of HTML: text, images, lists, links, even other tables.

Example

A row of three data cells.

Prompt
A table with a single row of three data cells using td: Emil, Tobias, Linus.
Output: complete HTML page

Table Rows

Each table row starts with a <tr> tag and ends with </tr>. "tr" stands for table row. You can have as many rows as you like; just keep the number of cells the same in each row.

Note: Sometimes a row can have fewer or more cells than another; you will learn about that in a later chapter.

Example

A table with two rows.

Prompt
A table with two rows of three data cells each: Emil/Tobias/Linus and 16/14/10.
Output: complete HTML page

Table Headers

When you want a cell to be a header, use the <th> tag instead of <td>. "th" stands for table header. By default, header text is bold and centered, but you can change that with CSS.

Example

A table whose first row is made of header cells.

Prompt
A table whose first row uses th header cells (Person 1, Person 2, Person 3) followed by two data rows: Emil/Tobias/Linus and 16/14/10.
Output: complete HTML page

HTML Table Tags

The table-related tags are: <table> (a table), <th> (a header cell), <tr> (a row), <td> (a cell), <caption> (a table caption), <colgroup> (a group of columns for formatting), <col> (column properties within a colgroup), <thead> (groups the header content), <tbody> (groups the body content), and <tfoot> (groups the footer content).