Structuring Data: HTML Tables

Learn to organize tabular data with rows and columns using the essential HTML table tags, from basic structure to advanced accessibility.

Lesson ProgressStep 1 of 9
📊
0 EXP

Welcome! Let's learn how to organize data on a webpage by building an HTML table from scratch.

/* Ready to build a table? */

Basic Table Structure

An HTML table is built with a specific hierarchy of tags.

  • <table>: The main container that wraps the entire table.
  • <tr> (Table Row): Defines a single horizontal row.
  • <td> (Table Data): Defines a single cell within a row.
<table>
  <tr>
    <td>Cell 1.1</td>
    <td>Cell 1.2</td>
  </tr>
  <tr>
    <td>Cell 2.1</td>
    <td>Cell 2.2</td>
  </tr>
</table>

System Check

Which tag is used to create a single row in a table?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

📊
Table Builder

Construct a well-formed HTML table with rows and cells.

🏗️
Table Structurer

Correctly use <thead>, <tbody>, and <th> for semantics.

✍️
Cell Master

Prove your mastery of cell spanning with colspan/rowspan.

Mission: Build a Semantic Table

Create a 2x2 table with a semantic header (<thead>, <th>) and body (<tbody>, <td>). Add one row to the body. Our AI will guide you.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Order the Tags

Drag the elements into the correct nesting order to build a valid table, from the outermost container to the innermost cell.

<tr>
<table>
<td>Cell</td>
<tbody>

Challenge: Span the Cells

Fill in the missing attributes to make the cells span multiple columns or rows.

<th="2">Spans 2 Columns</th>
<td="3">Spans 3 Rows</td>

Consult A.D.A.

Community Holo-Net

Beyond the Grid: Mastering HTML Tables

In the early days of the web, developers notoriously used <table> tags to create entire page layouts. This was a hack. Today, we have CSS Flexbox and Grid, which are infinitely better for layout. The HTML <table> element has a clear and vital purpose: **to display tabular data**. Think spreadsheets, price comparisons, or schedules.

Using tables correctly isn't just about rows and cells; it's about **semantics** and **accessibility**. A well-structured table is one that a screen reader can parse logically, and that a search engine can understand.

1. The Semantic Structure: <thead>, <tbody>, <tfoot>

While you can just throw <tr> tags directly into a <table>, you'll create a much more robust and meaningful structure by grouping your rows into a header, body, and footer.

  • <thead>: Wraps the header row(s) of your table (where you put your <th>tags).
  • <tbody>: Wraps the main data rows of your table. You can technically have multiple `<tbody>` elements to group data.
  • <tfoot>: Wraps the footer row(s), often used for summaries or totals.

Why bother? These tags provide crucial hooks for **CSS styling** (e.g., making the header sticky) and **accessibility**. They also allow browsers to correctly print long tables, repeating the <thead> and<tfoot> on each page.

2. Accessibility is Non-Negotiable: <caption> and `scope`

A visual user can quickly scan a table, but a screen reader user needs more context.

  • <caption>: This tag should be the *first child* of your <table>. It acts as the table's title, describing its content (e.g., "Monthly Sales Figures 2025"). This is far more semantic than using a <p> or<h3> tag above the table.
  • scope Attribute: This is the most important accessibility feature. It tells assistive technology what a header is *for*.
    • <th scope="col">: Declares this header is for the **column** of data below it.
    • <th scope="row">: Declares this header is for the **row** of data to its right (common in the first column).

3. Merging Cells: `colspan` and `rowspan`

Sometimes, data isn't a perfect grid. You might need a header that spans two columns, or a cell that spans multiple rows.

  • colspan="2": An attribute you add to a <td> or <td>. This makes the cell stretch **horizontally** to occupy the space of two columns. You must then remove the cell it "covered up" from that same row.
  • rowspan="3": Makes the cell stretch **vertically** to occupy the space of three rows. You must then remove the corresponding cells from the *next two rows* below it.

Colspan Example

<tr>
  <th colspan="2">Full Name</th> 
</tr>
<tr>
  <td>John</td>
  <td>Doe</td>
</tr>

The "Full Name" header spans two columns, above "John" and "Doe".

Rowspan Example

<tr>
  <td rowspan="2">ID-123</td>
  <td>Item A</td>
</tr>
<tr>
  <td>Item B</td>
</tr>

"ID-123" spans two rows, next to both "Item A" and "Item B".

4. The Modern Challenge: Responsive Tables

Tables are wide. Mobile screens are narrow. This is a problem. A common, simple solution is to wrap your table in a <div> and allow it to overflow horizontally.

<div style="overflow-x: auto; width: 100%;">
  <table>
    </table>
</div>

This isn't always the best user experience. A more advanced (but complex) method involves using CSS to "stack" the table cells vertically on mobile, using `data-` attributes to re-insert the headers.

Key Takeaway: Use tables for **tabular data only**. Build them semantically with <thead>, <tbody>, <caption>, and scope. This creates a foundation that is accessible, stylable, and professional.

HTML Table Glossary

<table>
The wrapper element for all HTML table content.
<tr>
Table Row. Defines a single horizontal row of cells in a table.
<td>
Table Data. The standard data cell of a table.
<th>
Table Header. A special cell for a column or row header. Text is typically bold and centered by default.
<thead>
Table Head. A semantic wrapper for the header row(s) (the <tr>containing <th> tags).
<tbody>
Table Body. A semantic wrapper for the main content rows of the table.
<tfoot>
Table Foot. A semantic wrapper for the footer row(s) of the table, used for sums or totals.
<caption>
Provides a title or description for the table. It should be the first child of the <table> element.
colspan (Attribute)
Used on a <td> or <th> to make that cell span horizontally across multiple columns.
rowspan (Attribute)
Used on a <td> or <th> to make that cell span vertically across multiple rows.
scope (Attribute)
An accessibility attribute for <th> tags. Set to scope="col" or scope="row" to define what the header is associated with.
border-collapse (CSS)
A CSS property set on the <table>. border-collapse: collapse; merges adjacent cell borders into a single, clean line.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching HTML and building robust and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest HTML5 specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!