← Web basics: HTML, CSS and JavaScript

Tables

Rows and columns for data, with proper header cells.

Start this lesson

A table shows data in rows and columns, like a timetable or a price list. <table> wraps everything. Each row is a <tr> (table row). Inside a row, header cells are <th> and ordinary data cells are <td>. A <caption> right after <table> gives the table a title. Header cells matter: screen readers use them to say "Price: $3" instead of just "$3".

<table>
  <caption>Juice prices</caption>
  <tr>
    <th>Juice</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>Orange</td>
    <td>$3</td>
  </tr>
  <tr>
    <td>Watermelon</td>
    <td>$4</td>
  </tr>
</table>

Three rows: the first holds the two column headers, the other two hold data. Every row has the same number of cells, so the columns line up.

Bigger tables often group rows: <thead> around the header row and <tbody> around the data rows.

Use tables only for data that really has rows and columns. Long ago people used tables to lay out whole pages; today that job belongs to CSS.