Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 122 ยท Tables

Tables

Build your first HTML tables with table, tr, td and th. Learn how rows and cells form a grid, when a table is the right choice, and why tables are never for layout.

Some information is naturally a grid. A train timetable has stations down one side and departure times across the top. A league table has teams in rows and points in columns. When readers need to look across and down to find an answer, you are looking at tabular data, and HTML has a whole family of elements for it.

In this lesson you will build tables from four elements: <table>, <tr>, <td> and <th>. The rest of this chapter builds on these basics with captions, spanning cells, styling and accessibility.

The building blocks

A table is built row by row. You never describe columns directly; they appear because every row has cells in the same order.

Element Name What it does
<table> table Wraps the whole table.
<tr> table row Holds one horizontal row of cells.
<td> table data An ordinary cell containing data.
<th> table header A cell that labels a row or a column.

Here is the smallest useful table: a header row and two rows of data.

A first table

Read the code from top to bottom and you can see the grid: three <tr> elements make three rows, and each row contains two cells, so the table has two columns. Browsers make <th> cells bold and centered by default, but that is only decoration. What matters is that a <th> tells browsers and screen readers "this cell is a label".

Tables have no borders by default. The plain look above is normal. You will add borders, spacing and colors with CSS in Styling Tables with CSS. The examples in this chapter include a small <style> block so the grid is easy to see.

Making the grid visible

A few lines of CSS turn the bare grid into something readable. Don't worry about every property yet; the important part is that the HTML stays exactly the same.

A train timetable

Try adding a fourth station. Copy one whole <tr> block, paste it below the last one and change the values. Then try adding a fourth train: you will need to add one cell to every row, including the header row.

Row headers as well as column headers

A <th> does not have to sit in the first row. In the timetable, the station names label each row, so they are headers too. Making them <th> cells gives each row a clear label:

HTML
<tr>
  <th>Harbor Street</th>
  <td>07:10</td>
  <td>08:40</td>
  <td>10:10</td>
</tr>

A person scanning for "When does train 403 reach Old Mill?" uses both labels: the column header (Train 403) and the row header (Old Mill). Screen readers can do the same, announcing the right headers as the user moves from cell to cell. The next lesson, Table Headers, Sections and Captions, shows how the scope attribute makes that relationship explicit.

What can go inside a cell

A <td> or <th> can hold much more than a word or number. Links, images, lists, line breaks and even buttons are all fine inside a cell:

Rich cell content

Keep cells simple where you can, though. A table is easiest to read when each cell answers one small question.

Rules the browser expects

HTML tables follow a strict structure. Breaking it rarely causes an error message, but the browser quietly "repairs" your code, and the result may not be what you meant.

  • Cells live inside rows. A <td> or <th> must be a child of a <tr>.
  • Rows live inside the table (or inside a row group such as <tbody>, which you will meet in the next lesson).
  • Text never goes directly inside <table> or <tr>. Stray text gets moved out and shown above the table, a confusing bug to track down.
  • Give every row the same number of cells. If one row is short, the browser simply leaves a gap at the end. To make a cell stretch across several columns, use colspan instead (see Spanning Rows and Columns).

A hidden element appears. If you inspect any of the tables above in your browser's developer tools, you will find a <tbody> element that you never wrote. When a <tr> sits directly inside <table>, the HTML parser adds a <tbody> around it automatically. That's why a CSS selector like table > tr matches nothing.

Tables are for data, never for layout

Long ago, before CSS could arrange a page, designers built entire websites out of tables: one cell for the logo, one for the menu, one for the content, often with tables nested inside tables. You may still meet that code in old email templates.

Don't copy it. Using tables for layout causes real problems:

  • Screen readers announce tables as tables. A user hears "table with 3 columns and 5 rows" for what is really a page header, then has to navigate cell by cell through content that was never tabular.
  • Layout tables can't adapt. A row of cells stays a row on a narrow phone screen, forcing sideways scrolling.
  • The markup is heavy and fragile. Changing the design means rewriting the HTML instead of a few lines of CSS.

Ask yourself one question: would this content still make sense as a spreadsheet? Prices of three plans compared feature by feature: yes, use a table. A photo next to a paragraph: no, that's layout. For page structure, use the elements from Semantic Elements and arrange them with CSS, as you will learn in Page Layout.

Don't reach for old attributes either. Attributes like border, cellpadding, cellspacing, bgcolor and align on tables are obsolete. Browsers still understand some of them for backward compatibility, but CSS does every one of these jobs better.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which element creates a new row in a table?

    <tr> stands for table row. <td> and <th> are cells that go inside a row, and there is no <row> element in HTML.
  2. 2. How do you add a new column to an existing table?

    Tables are written row by row, so a new column appears when every row gets one more cell in the same position.
  3. 3. Why should the station names in a timetable be <th> cells rather than <td> cells?

    <th> marks a cell as a header for its row or column. Its bold look is only a default style; the meaning is what matters.
  4. 4. Which of these is a good use of a table?

    The phone comparison is real tabular data that you read across and down. The other two are layout, which belongs to CSS.

Summary

  • <table> wraps a table, <tr> makes a row, <td> makes a data cell and <th> makes a header cell.
  • Tables are written row by row; columns appear because rows line up.
  • <th> cells can label columns (top row) or rows (first column).
  • Cells may contain links, lists and other content, but text must never sit directly in <table> or <tr>.
  • Browsers add a <tbody> automatically when you leave it out.
  • Use tables only for tabular data, never for page layout, and style them with CSS rather than obsolete attributes.

Next lesson: Table Headers, Sections and Captions.