HTML Tables

HTML Tables

Master the art of creating structured, accessible data tables. Learn basic table structure, advanced features, and best practices for organizing tabular data.

How do you create a table in HTML?

Use <table> with rows (<tr>) containing header cells (<th>) and data cells (<td>). Group rows with <thead>, <tbody> and <tfoot>, and add a <caption> to describe the table. Merge cells with colspan and rowspan. Tables are for tabular data, not page layout.

<table>
  <caption>Monthly sales</caption>
  <thead>
    <tr><th scope="col">Month</th><th scope="col">Sales</th></tr>
  </thead>
  <tbody>
    <tr><th scope="row">January</th><td>$1,200</td></tr>
    <tr><td colspan="2">No data for February</td></tr>
  </tbody>
</table>

What are HTML Tables?

HTML tables are used to display data in rows and columns. They provide a structured way to present tabular information like spreadsheets, schedules, price lists, and comparison charts.

Table Elements:

  • • <table> - Container element
  • • <thead> - Table header section
  • • <tbody> - Table body content
  • • <tr> - Table rows
  • • <th> - Header cells
  • • <td> - Data cells

Key Features:

  • • Semantic structure
  • • Accessibility support
  • • Cell merging capabilities
  • • Responsive design options
  • • Interactive functionality
  • • Screen reader friendly

Try It Yourself

Create a basic HTML table with headers and data:

Code Editor

<table>
  <caption>Course progress</caption>
  <thead>
    <tr><th scope="col">Course</th><th scope="col">Lessons</th><th scope="col">Done</th></tr>
  </thead>
  <tbody>
    <tr><th scope="row">HTML</th><td>69</td><td>40%</td></tr>
    <tr><th scope="row">CSS</th><td>66</td><td>15%</td></tr>
    <tr><td colspan="3">JavaScript coming next</td></tr>
  </tbody>
</table>
<style>
  table { border-collapse: collapse; }
  th, td { border: 1px solid #ccc; padding: 6px 12px; text-align: left; }
  caption { font-weight: bold; margin-bottom: 6px; }
</style>

Preview

What to Notice:

  • • Table has a clear structure with header and body
  • • Caption provides context for the table content
  • • Styling makes the table visually appealing
  • • Hover effects improve user interaction

Table Structure

Basic Table Elements

Essential Structure:

<table> <caption>Table Description</caption> <thead> <tr> <th>Header 1</th> <th>Header 2</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> </tr> </tbody> </table>
<table>:

Main container for all table content

<caption>:

Provides a title or description

<thead>:

Groups header content

Header Cells (<th>)

  • • Define column or row headers
  • • Bold and centered by default
  • • Improve accessibility
  • • Support scope attributes
  • • Essential for screen readers

Data Cells (<td>)

  • • Contain actual table data
  • • Left-aligned by default
  • • Can span multiple columns/rows
  • • Support various content types
  • • Inherit table styling

Important Table Attributes

Cell Spanning

Colspan (Horizontal):

<!-- Merge cells across columns --> <td colspan="3">Spans 3 columns</td> <!-- Header spanning multiple columns --> <th colspan="2">Q1 Results</th>

Rowspan (Vertical):

<!-- Merge cells across rows --> <td rowspan="2">Spans 2 rows</td> <!-- Vertical header --> <th rowspan="3">Category</th>

Accessibility Attributes

Scope Attribute:

<!-- Column headers --> <th scope="col">Name</th> <th scope="col">Age</th> <!-- Row headers --> <th scope="row">January</th>

Headers Attribute:

<!-- Complex relationships --> <th id="name">Name</th> <th id="age">Age</th> <td headers="name">John</td> <td headers="age">25</td>
scope="col"
Column header
scope="row"
Row header
scope="colgroup"
Column group
scope="rowgroup"
Row group

Common Table Use Cases

Data Presentation:

  • • Employee information lists
  • • Product catalogs and prices
  • • Financial reports and statements
  • • Statistical data and surveys
  • • Comparison charts

Schedules & Planning:

  • • Class schedules and timetables
  • • Event calendars
  • • Project timelines
  • • Meeting room bookings
  • • Shift schedules

Technical Data:

  • • System specifications
  • • Test results and metrics
  • • Configuration settings
  • • Performance benchmarks
  • • API documentation tables

Interactive Features:

  • • Sortable data tables
  • • Filterable lists
  • • Editable spreadsheets
  • • Data visualization tables
  • • Dashboard displays

Table Best Practices

Do's:

  • • Use semantic table elements
  • • Include table captions
  • • Provide scope attributes
  • • Make tables responsive
  • • Test with screen readers
  • • Use consistent alignment

Don'ts:

  • • Don't use tables for layout
  • • Avoid empty header cells
  • • Don't make tables too wide
  • • Avoid poor color contrast
  • • Don't forget mobile users
  • • Avoid overly complex structures

Important Notes:

  • • Tables are for tabular data only
  • • Always include meaningful headers
  • • Consider mobile experience early
  • • Test accessibility with screen readers
  • • Use captions for context
  • • Keep structure simple when possible

Quick Reference

Essential Elements:

<table> <!-- Table container --> <caption> <!-- Table title --> <thead> <!-- Header section --> <tbody> <!-- Body section --> <tfoot> <!-- Footer section --> <tr> <!-- Table row --> <th> <!-- Header cell --> <td> <!-- Data cell -->

Key Attributes:

colspan="3" <!-- Span 3 columns --> rowspan="2" <!-- Span 2 rows --> scope="col" <!-- Column header --> scope="row" <!-- Row header --> headers="id" <!-- Associate cells --> id="unique" <!-- Unique identifier -->

HTML Tables — step by step

1 / 4
1

Build the table structure

A table uses table, tr (row), th (header cell), and td (data cell). Wrap header rows in thead and data rows in tbody for semantics and styling hooks.

1 of 4
⚠️Tables for Layout Are Outdated
Before CSS Grid, developers used tables for page layout. This is now considered bad practice. Tables are semantically wrong for layout, break responsiveness on mobile, and create accessibility nightmares for screen readers.

🎯 Quick Check

Which HTML element defines a table header cell?

HTML Tables: summary

Key points

  • scope="col" or scope="row" on <th> tells screen readers which cells a header applies to.
  • <caption> is the table's accessible name and visible title.
  • Style tables with CSS (border-collapse, padding, zebra stripes) rather than old attributes like border and cellpadding.

Frequently asked questions

›How do I merge cells in an HTML table?

Use colspan="2" to span two columns or rowspan="2" to span two rows on the <td> or <th>.

›Should I use tables for layout?

No. Layout tables confuse screen readers and are hard to make responsive. Use CSS Grid or Flexbox.