HTML

How to Create Tables in HTML with Complete Examples

How to build tables in HTML to show data in rows and columns: table, tr, th, td, thead, tbody and caption, with complete examples.

RazakTerra
Reading time
1 min read
Comments
Discussion
How to Create Tables in HTML with Complete Examples
On this page
  1. Basic Table Structure
  2. Adding Thead, Tbody, and Tfoot
  3. Merging Cells with Colspan and Rowspan
  4. Making Tables More Accessible
  5. Quick Tips

Basic Table Structure

A simple table needs four main tags:

Playground
<table>
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Alex</td>
    <td>25</td>
  </tr>
  <tr>
    <td>Sarah</td>
    <td>28</td>
  </tr>
</table>
Result
Cmd/Ctrl + Enter runs it · Esc leaves the editor
  • <table> → the container for the whole table
  • <tr> → table row
  • <th> → table header cell (usually bold and centered)
  • <td> → table data cell

Adding Thead, Tbody, and Tfoot

For bigger tables, it’s better to group the sections:

Playground
<table>
  <thead>
    <tr>
      <th>Product</th>
      <th>Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Keyboard</td>
      <td>$29</td>
    </tr>
    <tr>
      <td>Mouse</td>
      <td>$15</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Total</td>
      <td>$44</td>
    </tr>
  </tfoot>
</table>
Result
Cmd/Ctrl + Enter runs it · Esc leaves the editor
  • <thead> → header section
  • <tbody> → main content
  • <tfoot> → footer section (great for totals)

Merging Cells with Colspan and Rowspan

Colspan merges columns:

HTML
<td colspan="2">This cell spans two columns</td>

Rowspan merges rows:

HTML
<td rowspan="2">This cell spans two rows</td>

Example:

Playground
<table>
  <tr>
    <th>Name</th>
    <th colspan="2">Contact</th>
  </tr>
  <tr>
    <td>Alex</td>
    <td>Email</td>
    <td>Phone</td>
  </tr>
</table>
Result
Cmd/Ctrl + Enter runs it · Esc leaves the editor

Making Tables More Accessible

To make tables easier to understand (especially for screen readers), you can add:

Playground
<table>
  <caption>
    Monthly Sales Report
  </caption>
  <thead>
    <tr>
      <th scope="col">Month</th>
      <th scope="col">Sales</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>January</td>
      <td>$3000</td>
    </tr>
  </tbody>
</table>
Result
Cmd/Ctrl + Enter runs it · Esc leaves the editor
  • <caption> → gives the table a title
  • scope=“col” or scope=“row” → helps screen readers understand headers

Quick Tips

  • Use tables only for real tabular data, not for page layout.
  • Always add headers with <th> so the table is clearer.
  • Use <thead>, <tbody>, and <tfoot> for better structure.
  • Add a <caption> when the table needs a title.
  • Don’t forget scope for better accessibility.

With these tags, you can build clean and well-structured tables that are easy to read and friendly for everyone.

Written by

Razak

Writes about the web, slowly, and builds small tools on the side.

Discussion

Comments

Loading comments…

Keep reading