What are HTML tables?

HTML tables are used to display tabular data โ€” information organized in rows and columns. Examples include pricing plans, schedules, sports scores, student grades, financial reports, and product comparisons.

A table has:

  • Rows โ€” horizontal lines of data (tr = table row)
  • Columns โ€” vertical lines of data
  • Cells โ€” individual boxes that hold data (td = table data, th = table header)
Important:
Tables are ONLY for tabular data. Never use tables for page layout (like arranging a header, sidebar, and content). Use CSS Grid or Flexbox instead.

Basic table structure

Every table starts with the <table> tag. Inside, you'll use:

  • <tr> โ€” table row (groups cells horizontally)
  • <th> โ€” table header cell (bold, centered by default)
  • <td> โ€” table data cell (regular cell)
basic-table.html A simple 3x3 table

This creates a table with 3 rows and 3 columns. By default, tables have no borders. You'll add borders with CSS later.

Table sections: thead, tbody, tfoot

For larger tables, group rows into logical sections:

  • <thead> โ€” header rows (titles/column names). Usually just one row, at the top.
  • <tbody> โ€” body rows (the main data). Can have many rows.
  • <tfoot> โ€” footer rows (summaries, totals, notes). Usually at the bottom.
semantic-table.html Table with thead, tbody, tfoot

The <caption> tag provides a title for the table. It's always the first child of <table>.

Try it yourself: Pricing table

Edit the code below to create a pricing comparison table with thead, tbody, and tfoot. Try adding more features or plans!

โœ๏ธ Try It Yourself โ€” Pricing Table
Preview

Spanning cells: colspan and rowspan

Sometimes a cell needs to span multiple columns or rows:

  • colspan="2" โ€” makes a cell occupy 2 columns
  • rowspan="2" โ€” makes a cell occupy 2 rows
colspan-rowspan.html Cells spanning multiple columns and rows

In the example above, "Weekly Schedule" spans 3 columns, "Assembly" spans 2 columns, and "Lunch" spans 2 rows.

Try it yourself: Schedule with colspan and rowspan

Build a class schedule where some classes span multiple periods or days.

โœ๏ธ Try It Yourself โ€” Schedule Table
Preview

Colgroup: style entire columns

The <colgroup> tag groups columns so you can style them all at once using <col> tags.

colgroup.html Colgroup for column styling

Each <col> represents one column. With the span attribute, one <col> can style multiple adjacent columns.

Accessibility: the scope attribute

To make tables accessible to screen readers, use the scope attribute on header cells:

  • scope="col" โ€” this header applies to the column below it
  • scope="row" โ€” this header applies to the row to its right
accessible-table.html Table with scope attributes

Screen readers use scope to announce which header a cell belongs to, making the table much easier to navigate for users with visual impairments.

When NOT to use tables

Tables are ONLY for tabular data. DO NOT use them for:

  • Page layout โ€” Use CSS Grid or Flexbox instead. Screen readers will incorrectly announce layout tables as data tables.
  • Positioning elements โ€” Tables are semantic for data, not for moving divs around the page.
  • Creating sidebars or columns โ€” That's a layout job, use CSS.
Rule:
If your data would make sense in a spreadsheet (rows ร— columns), use a table. If it's just for positioning, use CSS.
Challenge: Student Grades Table

Task: Build a table showing Fall 2024 grades with the following requirements:

  • Caption: "Fall 2024 Grades"
  • Thead with columns: Student, Math, Science, English, Average
  • Tbody with 4 students and their grades (use realistic numbers 0-100)
  • Tfoot with "Class Average" label spanning 1 column, then the class averages for each subject (colspan to label the row)
  • Add simple inline CSS: borders, padding, column colors with colgroup
  • Use scope attributes on all headers

Build it in one of the editors above or in your own file!

Hint: Your table structure should be:

<table>
  <caption>...</caption>
  <colgroup> (optional, for styling)
  <thead>
    <tr> <th scope="col">... </th> ร— 5
  </thead>
  <tbody>
    <tr> ร— 4 (students)
  </tbody>
  <tfoot>
    <tr> <th scope="row" colspan="1">Class Average</th> <td> ร— 4
  </tfoot>
</table>

For class averages: Math avg = (M1+M2+M3+M4)/4, etc. Student averages = (Math+Science+English)/3.

Key takeaways

  • Use tables for tabular data only โ€” rows ร— columns with headers and data. Never for layout.
  • <table>, <tr>, <th>, <td> are the core elements.
  • <thead>, <tbody>, <tfoot> organize table sections semantically.
  • <caption> gives the table a title.
  • colspan and rowspan let cells span multiple columns or rows.
  • scope="col" and scope="row" improve accessibility for screen readers.
  • Style tables with CSS (borders, padding, colors). Use <colgroup> to style entire columns.

Test your knowledge

Question 1 of 5
Which tag defines a table header cell?
Question 2 of 5
What does colspan="2" do?
Question 3 of 5
Which element groups the header rows of a table?
Question 4 of 5
Should tables be used for page layout?
Question 5 of 5
What tag provides a title or caption for a table?
0
/5
Great work! You understand HTML tables.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just learned to build data tables.
The best is yet to come.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’