Skip to main content

How to Make a Table in HTML

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

An HTML table is a <table> made of rows, <tr>, and each row is made of cells: <th> for a header cell and <td> for a data cell. The browser lines the cells up into columns for you.

Syntax​

Syntax
<table>
<tr>
<th><heading></th>
</tr>
<tr>
<td><data></td>
</tr>
</table>
PartWhat it does
<table>The table.
<tr>A table row. Every cell goes inside a row.
<th>A header cell, for the heading of a column.
<td>A data cell.
<heading>, <data>The text in each cell.

Each row should have the same number of cells, one for each column. MDN's table page covers the other table elements.

The Example​

The example is a table of two public holidays, with the columns Date and Holiday.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a table of three rows: a header row with the th cells Date and Holiday, and the rows 1 January, New Year's Day and 25 December, Christmas Day in td cells.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Holidays</title>
</head>
<body>
<table>
<tr>
<th>Date</th>
<th>Holiday</th>
</tr>
<tr>
<td>1 January</td>
<td>New Year's Day</td>
</tr>
<tr>
<td>25 December</td>
<td>Christmas Day</td>
</tr>
</table>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: a table without borders, with the bold, centered headings Date and Holiday, and the rows 1 January, New Year's Day and 25 December, Christmas Day.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The cells lined up in two columns, each as wide as its longest text.
  • The th cells are bold and centered, and the td cells are normal text on the left. Those are the browser's defaults: Chromium gave th a font weight of 700 and centered text.
  • The table has no lines. A table only gets borders from CSS.
  • The browser added a tbody element around the rows, although the file has none: in the page Chromium built, all three rows were inside a tbody.

Common Mistakes​

MistakeWhat happensDo this instead
Writing text in a row without a cellThe text jumps out of the table: Two holidays, written between two rows, appeared above the tablePut all text in a th or a td
Giving one row more cells than the othersThe extra cell starts a new column with no heading: a third cell, Public, in the 1 January row sat in a column of its own, with nothing above itGive each row one cell per column, or join cells with colspan

Troubleshooting​

SymptomLikely causeFix
Some text shows above the tableIt isn't inside a cellMove it into a td
The table has no linesTables have no borders by defaultAdd borders with CSS

FAQ​

How do I add borders?​

With CSS, for example in a style element: th, td { border: 1px solid #999999; } gave every cell a 1-pixel grey border.

How do I make a cell span two columns?​

Give it colspan="2". In a test row with one such cell, the cell stretched across both columns, as wide as the two cells above it together.

What is tbody for?​

It groups the rows that make up a table's body. You can leave it out, as the example does, and the browser adds it for you.

Conclusion​

Rows of cells, th for headings and td for data, are all an HTML table needs. Keep every piece of text inside a cell, give each row the same number of columns, and add lines with CSS.

Additional Resources​