Skip to main content

HTML Lists and Tables

HTML Tables

Written by Updated HTML Tables exercises

An HTML table shows data in rows and columns: prices, timetables, results, comparisons. table holds it all, tr makes each row, and each cell is a th for a heading or a td for data. Use tables for data, never for page layout.

Each tag has its own lesson, for example the table tag and the td tag. Borders and styling are covered in Table Styling & Borders.

Basic Table Structure

The table is built row by row: <tr> for each row, and inside it <th> for header cells and <td> for data cells. There are no column tags; columns appear because every row has the same number of cells.

Basic Table Example

html

<table border="1">
  <tr>
    <th>Name</th>
    <th>Age</th>
    <th>City</th>
  </tr>
  <tr>
    <td>Alissha</td>
    <td>28</td>
    <td>Delhi</td>
  </tr>
  <tr>
    <td>Swapnil</td>
    <td>30</td>
    <td>Mumbai</td>
  </tr>
</table>

Table Header, Body & Footer

Bigger tables are divided into three parts: <thead> for the header rows, <tbody> for the data, and <tfoot> for totals or notes.

Table Example

html

<table border="1">
  <thead>
    <tr>
      <th>Product</th>
      <th>Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Laptop</td>
      <td>$800</td>
    </tr>
    <tr>
      <td>Tablet</td>
      <td>$400</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Total</td>
      <td>$1200</td>
    </tr>
  </tfoot>
</table>

These groups let you style the header separately, keep it at the top when printing, and help scripts and screen readers understand the table.

Adding Captions

A <caption> gives the table a title. It goes straight after the opening <table> tag, and screen readers announce it as the table's name.

Caption Example

html

<table border="1">
  <caption>Monthly Sales Report</caption>
  ...
</table>

Merging Cells (Rowspan & Colspan)

  • colspan="2" makes a cell two columns wide.
  • rowspan="2" makes a cell two rows tall.

Merged Cell Example

html

<table border="1">
  <tr>
    <th rowspan="2">Name</th>
    <th colspan="2">Marks</th>
  </tr>
  <tr>
    <th>Math</th>
    <th>Science</th>
  </tr>
  <tr>
    <td>Alissha</td>
    <td>95</td>
    <td>89</td>
  </tr>
</table>

When a table looks broken after merging, count the cells in each row, including the spans: every row must add up to the same number of columns.

Styling Tables with CSS

The old border="1" attribute still works, but CSS gives far more control:

Table Styling Example

html

<style>
table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  border: 1px solid #29AB87;
  padding: 10px;
  text-align: left;
}
th {
  background-color: #f3f3f3;
}
tr:nth-child(even) {
  background-color: #fafafa;
}
</style>
<table border="1">
  <tr>
    <th rowspan="2">Name</th>
    <th colspan="2">Marks</th>
  </tr>
  <tr>
    <th>Math</th>
    <th>Science</th>
  </tr>
  <tr>
    <td>Alissha</td>
    <td>95</td>
    <td>89</td>
  </tr>
</table>

Responsive Tables

A table cannot wrap its columns, so a wide table overflows on a phone. Wrapping it in a box with overflow-x: auto lets it scroll sideways inside that box, without breaking the rest of the page:

html

<div style="overflow-x:auto;">
  <table border="1" style="width: 800px;">
    <tr><th>Name</th><th>City</th><th>Email</th></tr>
    <tr><td>Alissha</td><td>Delhi</td><td>alissha@mail.com</td></tr>
    <tr><td>Swapnil</td><td>Mumbai</td><td>swapnil@mail.com</td></tr>
  </table>
</div>

Table Accessibility Tips

Accessibility Example

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Accessible HTML Table Example</title>
  <style>
    table {
      width: 100%;
      border-collapse: collapse;
      margin-top: 15px;
    }
    caption {
      caption-side: top;
      font-weight: bold;
      margin-bottom: 10px;
      text-align: left;
      color: #29AB87;
    }
    th, td {
      border: 1px solid #ccc;
      padding: 10px;
      text-align: left;
    }
    th {
      background-color: #f5f5f5;
    }
    tr:nth-child(even) {
      background-color: #fafafa;
    }
    tr:hover {
      background-color: #f0f9f6;
    }
  </style>
</head>
<body>

  <h2>Student Information Table</h2>

  <table>
    <caption>List of Students with Age and City</caption>
    <thead>
      <tr>
        <th scope="col">Name</th>
        <th scope="col">Age</th>
        <th scope="col">City</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Alissha</td>
        <td>28</td>
        <td>Delhi</td>
      </tr>
      <tr>
        <td>Swapnil</td>
        <td>30</td>
        <td>Mumbai</td>
      </tr>
      <tr>
        <td>Rahul</td>
        <td>26</td>
        <td>Pune</td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <td colspan="3">Total Students: 3</td>
      </tr>
    </tfoot>
  </table>

</body>
</html>

This table puts it all together: a caption, a header with scope attributes, a body and a footer, and CSS for borders, stripes and a hover effect.

Advanced Table Attributes (Legacy)

Old attributes like border, cellpadding, cellspacing, align and bgcolor still appear in tutorials and old code. Use CSS instead: border, padding, border-spacing, text-align and background-color.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

HTML Tables exercises

Build a pricing table

Build a table with three column headings: Plan, Price, Storage. Below them add two rows: Starter, $9, 10GB and Pro, $19, 100GB. Put the headings in <thead> and the two rows in <tbody>.

Result

Check your answer to see it here.