HTML Table

Beginner
⏱️ 13 min read
📚 Updated: Jul 2026
🎯 6 Examples + 6 Try It
table / tr / td

Introduction

HTML tables display tabular data in a structured grid of rows and columns. They are essential for datasets, comparisons, schedules, and any information that fits naturally into a spreadsheet-like layout.

This tutorial covers table structure, semantic sections, styling with CSS, colspan and rowspan, accessibility, common pitfalls, and a complete employee list example.

What You’ll Learn

01

table

Container.

02

tr

Rows.

03

th / td

Cells.

04

thead

Sections.

05

CSS

Style it.

06

a11y

Accessible.

What Is an HTML Table?

An HTML table is a container that holds data in rows and columns. You build it with the <table> element; each row uses <tr>; header cells use <th>; data cells use <td>.

Tables are for data, not page layout. Modern sites use CSS Grid and Flexbox for columns and sidebars.

💡
Beginner Tip

See the table tag reference for every attribute. This tutorial focuses on building readable, accessible tables from scratch.

Basic Structure of a Table

A well-formed table uses these elements:

  • <table> — defines the table container.
  • <thead> — groups header row(s).
  • <tbody> — groups the main data rows.
  • <tfoot> — groups footer rows (totals, optional).
  • <tr> — defines one table row.
  • <th> — header cell (column or row title).
  • <td> — data cell.
html
<table>
  <thead>
    <tr><th>Name</th><th>Age</th></tr>
  </thead>
  <tbody>
    <tr><td>Alice</td><td>28</td></tr>
    <tr><td>Bob</td><td>34</td></tr>
  </tbody>
</table>

Table Elements

  • <table> — main container for all table content.
  • <caption> — optional title shown above the table (great for accessibility).
  • <thead> — column titles; typically one row of th cells.
  • <tbody> — body rows with your data.
  • <tfoot> — summary row(s) such as totals or averages.
  • <tr> — a horizontal row of cells.
  • <th scope="col"> — column header; use scope="row" for row headers.
  • <td> — ordinary data cell.

Table Attributes

HTML4 table attributes still appear in old tutorials but are deprecated in HTML5. Use CSS instead:

  • border — use border: 1px solid #ddd on cells via CSS.
  • cellpadding — use padding on th, td.
  • cellspacing — use border-collapse: collapse and margins/borders.

Valid modern attributes include colspan and rowspan on th and td to merge cells.

Styling Tables

CSS improves readability with borders, spacing, alignment, and alternating row colors:

html
table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}
th {
  background-color: #f4f4f4;
}
tr:nth-child(even) {
  background-color: #f9f9f9;
}

border-collapse: collapse merges adjacent borders into single lines. Zebra striping with nth-child(even) helps users track rows across wide tables.

colspan and rowspan

Merge cells when headers or summaries span multiple columns or rows:

html
<tr>
  <th colspan="2">Schedule</th>
</tr>
<tr>
  <td rowspan="2">Monday</td>
  <td>Math</td>
</tr>

Common Pitfalls

  • Accessibility — use th with scope, add caption, and avoid tables for non-tabular layout.
  • Responsiveness — wide tables overflow on phones; wrap in a scroll container or simplify columns on small screens.
  • Complex tables — nested tables and deep colspan grids confuse users; split data or use simpler layouts when possible.
  • Deprecated attributes — replace border, cellpadding, and cellspacing with CSS.

⚡ Quick Reference

ElementPurpose
<table>Table container
<thead>Header section
<tbody>Body rows
<tfoot>Footer / totals
<tr>Table row
<th>Header cell
<td>Data cell
colspan="2"Span 2 columns
rowspan="2"Span 2 rows

Examples Gallery

Six examples from a minimal grid to a styled employee list. Each includes View Output and Try It Yourself.

Example 1 — Basic Table

html
<table>
  <tr><td>Apple</td><td>Red</td></tr>
  <tr><td>Banana</td><td>Yellow</td></tr>
</table>
Try It Yourself

How It Works

Each tr is a row; each td is a cell. No headers yet—just raw data.

Example 2 — Header Row with th

html
<table>
  <thead>
    <tr><th>Product</th><th>Price</th></tr>
  </thead>
  <tbody>
    <tr><td>Notebook</td><td>$4.99</td></tr>
    <tr><td>Pen</td><td>$1.25</td></tr>
  </tbody>
</table>
Try It Yourself

How It Works

thead wraps the header row; th cells label each column.

Example 3 — thead, tbody, tfoot

html
<table>
  <thead>
    <tr><th>Item</th><th>Qty</th><th>Cost</th></tr>
  </thead>
  <tbody>
    <tr><td>Coffee</td><td>2</td><td>$6.00</td></tr>
    <tr><td>Bagel</td><td>1</td><td>$3.50</td></tr>
  </tbody>
  <tfoot>
    <tr><td colspan="2">Total</td><td>$9.50</td></tr>
  </tfoot>
</table>
Try It Yourself

How It Works

tfoot holds the total row. colspan="2" merges the first two footer cells.

Example 4 — colspan and rowspan

html
<table>
  <tr><th colspan="2">Schedule</th></tr>
  <tr>
    <td rowspan="2">Mon</td>
    <td>Math</td>
  </tr>
  <tr><td>Science</td></tr>
</table>
Try It Yourself

How It Works

The header spans two columns; “Mon” spans two rows beside Math and Science.

Example 5 — CSS Styled Table

html
<style>
  table { width: 100%; border-collapse: collapse; }
  th, td { border: 1px solid #ddd; padding: 8px; }
  th { background: #f4f4f4; }
  tr:nth-child(even) { background: #f9f9f9; }
</style>
<table>
  <thead><tr><th>Name</th><th>Score</th></tr></thead>
  <tbody>
    <tr><td>Alice</td><td>92</td></tr>
    <tr><td>Bob</td><td>85</td></tr>
  </tbody>
</table>
Try It Yourself

How It Works

Alternating row backgrounds (zebra stripes) from the CSS in the Styling section above.

Example 6 — Complete Employee List

Full example from this tutorial—styled table with four columns and three employees:

html
<table>
  <thead>
    <tr><th>ID</th><th>Name</th><th>Position</th><th>Department</th></tr>
  </thead>
  <tbody>
    <tr><td>1</td><td>John Doe</td><td>Software Engineer</td><td>Development</td></tr>
    <tr><td>2</td><td>Jane Smith</td><td>Project Manager</td><td>Operations</td></tr>
  </tbody>
</table>
Try It Yourself

How It Works

Combines semantic structure, CSS styling, and real-world tabular data.

Best Practices

✅ Do

  • Use thead, tbody, and th for structure
  • Add caption to describe the table
  • Style with CSS (border-collapse, padding)
  • Use tables only for tabular data
  • Test with a screen reader or keyboard navigation

❌ Don’t

  • Use tables for page layout
  • Rely on deprecated border / cellpadding
  • Skip header cells on data columns
  • Create deeply nested tables
  • Forget mobile overflow handling

Universal Browser Support

HTML table elements are supported in every browser. CSS styling (border-collapse, nth-child) works in all modern browsers.

Baseline · Since HTML

HTML table elements

HTML table elements are supported in every browser. CSS styling (border-collapse, nth-child) works in all modern browsers.

100% Core tag support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
HTML table elements Universal

Bottom line: Tables are one of the oldest, most reliable HTML features.

Conclusion

HTML tables are fundamental for presenting structured data on the web. Use semantic sections, header cells, and CSS styling to build tables that are clear, accessible, and visually polished.

Next, format text inside cells with HTML Lists to organize content with bullets and numbers, or explore individual tags like th and td.

Key Takeaways

📄 02

th

Headers.

Semantic
🗃 03

thead

Sections.

Structure
🎨 04

CSS

Not border=.

Style
05

scope

a11y.

Access

❓ Frequently Asked Questions

An HTML table displays data in rows and columns using the table element. Each row is a tr; header cells use th; data cells use td. Group rows with thead, tbody, and tfoot.
th defines a header cell—bold by default and semantically a column or row heading. td defines a regular data cell. Screen readers use th to label columns and rows.
No. The border, cellpadding, and cellspacing attributes are deprecated in HTML5. Use CSS: border-collapse, padding, and border on th and td instead.
colspan makes a cell span multiple columns; rowspan spans multiple rows. Use them for merged headers or summary rows, not for page layout.
Use tables for tabular data—spreadsheets, pricing, schedules, comparison charts. Do not use tables for page layout; CSS Grid and Flexbox are the modern approach.
Use th with scope attributes, caption for a title, thead for headers, and keep tables simple. For very large datasets, consider responsive patterns or data grids with ARIA.
Did you know?

Tables were once the primary tool for web page layout in the 1990s and early 2000s. CSS layout replaced that pattern. Today, using a table for non-data layout fails accessibility audits and makes responsive design much harder.

Build a table in the editor

Add rows, headers, and CSS stripes, then preview your employee list live.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful