HTML Tables <table>

HTML Living Standard

A data table expresses relationships across rows and columns. HTML can label those relationships so readers can tell what each value means.

On this page

Table rows, cells, and caption

A table contains rows written with tr. Use td for a data cell and th for a header cell. Put the visible caption first inside the table. Thead groups header rows and tbody groups body rows. The browser aligns cells into columns even without added CSS.

How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.

<table>
  <caption>Product catalog</caption>
  <thead>
    <tr><th>Item</th><th>Price (MMK)</th></tr>
  </thead>
  <tbody>
    <tr><td>Pen</td><td>1500</td></tr>
    <tr><td>Notebook</td><td>2500</td></tr>
  </tbody>
</table>

What to expect You see a caption and two columns. Each product’s price appears on the same row as its name.

Table Footer <tfoot>

After the data rows in <tbody>, put summary rows such as a total or average inside <tfoot>. Each footer row still uses <tr> with cells inside it. Enter the summary value yourself in this example. If a price changes, update the total too.

How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.

<table>
  <caption>Order summary</caption>
  <thead>
    <tr><th scope="col">Item</th><th scope="col">Price (MMK)</th></tr>
  </thead>
  <tbody>
    <tr><td>Pen</td><td>1500</td></tr>
    <tr><td>Notebook</td><td>2500</td></tr>
  </tbody>
  <tfoot>
    <tr><th scope="row">Total</th><td>4000</td></tr>
  </tfoot>
</table>

What to expect The Total row appears below the product rows and shows 4000 MMK.

Connect headers to cells with scope

In a simple data table, scope="col" marks a header for its column and scope="row" marks a header for its row. Assistive technology can use the relationships to announce meaningful context as someone moves through the table.

How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.

<table>
  <caption>Product prices</caption>
  <tr><th scope="col">Product</th><th scope="col">Price (MMK)</th></tr>
  <tr><th scope="row">Pen</th><td>1500</td></tr>
  <tr><th scope="row">Notebook</th><td>2500</td></tr>
</table>

What to expect The cell containing 1500 has Pen as its row header and Price (MMK) as its column header.

Table Columns with <colgroup> and <col>

Use <colgroup> to group columns. Place it after <caption> and before <thead>. Inside it, each <col> in this example represents one column, from left to right. A class on the second <col> lets CSS color the background of the price column. Keep the actual headings and values in <th> and <td> as usual.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Product prices</title>
  <style>
    .price-column { background-color: #dbeafe; }
  </style>
</head>
<body>
<table>
  <caption>Product prices</caption>
  <colgroup>
    <col>
    <col class="price-column">
  </colgroup>
  <thead>
    <tr><th scope="col">Item</th><th scope="col">Price (MMK)</th></tr>
  </thead>
  <tbody>
    <tr><td>Pen</td><td>1500</td></tr>
    <tr><td>Notebook</td><td>2500</td></tr>
  </tbody>
</table>
</body>
</html>

What to expect The price column has a light blue background. Move the class to the first <col> to color the item column instead.

Official references