HTML Tables
Think about a train timetable, a school report card, a price list, or a sports league table. What do they have in common? The information has two directions. You read across to follow one item, and you read down to compare many items. A list cannot do that. A paragraph definitely cannot.
For data that lives in rows and columns, HTML has the table. Tables are one of the most powerful (and most misused) parts of HTML. Used well, they make complex data easy to scan and easy for assistive technology to read. Used badly, they create confusing, broken pages. In this section, you will learn to build tables that are clear, well-structured, and friendly to every reader and every screen size.
What You Will Learnβ
In this section, you will be able to:
- Build a table from
<table>,<tr>,<th>, and<td> - Understand rows and cells, and how they form columns
- Mark up heading cells correctly with
<th>andscope - Add a caption that names the table
- Group content into
<thead>,<tbody>, and<tfoot> - Merge cells with
colspanandrowspan - Style whole columns with
<colgroup>and<col> - Make tables look good with CSS
- Make wide tables work on small screens with responsive techniques
- Make tables accessible to screen reader users
You should know what elements and attributes are, and be comfortable with basic text elements. If lists are new to you, review the Lists section first. It helps to compare the two.
Your First Tableβ
Here is a small table with a header row and two rows of data.
- HTML Code
- Browser Output
<table>
<tr>
<th>Language</th>
<th>Used for</th>
</tr>
<tr>
<td>HTML</td>
<td>Structure</td>
</tr>
<tr>
<td>CSS</td>
<td>Style</td>
</tr>
</table>
| Language | Used for |
|---|---|
| HTML | Structure |
| CSS | Style |
The demo adds borders and a header background with CSS. By itself, a table has no borders at all, only the data laid out in aligned columns.
The Anatomy of a Tableβ
A table is built from four main pieces. Learn these four, and you can build most tables.
| Element | Name | Job |
|---|---|---|
<table> | Table | The container for everything |
<tr> | Table Row | One horizontal row |
<th> | Table Header cell | A cell that labels a column or row. Bold and centered by default |
<td> | Table Data cell | A cell that holds data |
There is no "column" element at the core of a table. Columns appear on their own when rows stack up: the first cell of every row forms column 1, the second cell forms column 2, and so on.
column 1 column 2
βββββββββββββ¬βββββββββββββ
row 1 β <th> β <th> β β header row
βββββββββββββΌβββββββββββββ€
row 2 β <td> β <td> β
βββββββββββββΌβββββββββββββ€
row 3 β <td> β <td> β
βββββββββββββ΄βββββββββββββ
In HTML, you describe a table row by row, from top to bottom, and each row cell by cell, from left to right. Columns come for free.
The Full Set of Table Elementsβ
Beyond the four basics, HTML has elements that give a table more structure and meaning. You will meet each one in its own lesson.
| Element | Purpose | Lesson |
|---|---|---|
<caption> | A title for the table | Caption |
<thead> | Groups the header rows | Thead, Tbody, Tfoot |
<tbody> | Groups the main data rows | Thead, Tbody, Tfoot |
<tfoot> | Groups the footer rows, such as totals | Thead, Tbody, Tfoot |
<colgroup> and <col> | Describe columns, so you can style them | Colgroup and Col |
Here is a fuller table that uses most of them.
- HTML Code
- Browser Output
<table>
<caption>Monthly sign-ups</caption>
<thead>
<tr>
<th scope="col">Month</th>
<th scope="col">Sign-ups</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">January</th>
<td>120</td>
</tr>
<tr>
<th scope="row">February</th>
<td>180</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">Total</th>
<td>300</td>
</tr>
</tfoot>
</table>
| Month | Sign-ups |
|---|---|
| January | 120 |
| February | 180 |
| Total | 300 |
Do not worry about every detail now. Each piece has its own lesson.
When Should You Use a Table?β
Use a table when your content is data with relationships in two directions.
| Good use | Why it works |
|---|---|
| Schedules and timetables | Time runs one way, places or events the other |
| Price and plan comparisons | Features down the side, plans across the top |
| Scores and statistics | Each row is one team or person, each column a measure |
| Specifications compared across products | Same properties for many items |
| Calendars | Days across, weeks down |
A Simple Testβ
Does the information make sense as a grid, where each cell relates to both its row and its column?
If yes, use a table. If not, choose another tool:
| If your content is... | Use instead |
|---|---|
| A single group of items | A list |
| One item's properties (name, size, color) | A description list |
| The visual layout of a page | CSS (Flexbox or Grid) |
| Text with columns | CSS columns |
Tables Are for Data, Not for Layoutβ
In the early years of the web, there was no good way to place things side by side, so developers used tables to build whole page layouts: one cell for the logo, one for the menu, one for the content. It worked, but it caused real problems.
- Screen readers announce "table, 3 columns, 5 rows" and try to read a page as data when it is not.
- Small screens cannot rearrange a table layout, so pages broke on phones.
- Maintenance was painful, because design and content were tangled together.
Today, CSS Flexbox and Grid handle layout far better. Keep tables for data.
If you are tempted to use a table to place two things side by side, use CSS Grid or Flexbox instead. A table should always mean "this is data in a grid."
Your Learning Pathβ
Follow the lessons in order. Each one builds on the one before.
ποΈ Table Basics
Learn the basics of HTML tables. Build your first table with table, tr, th, and td, understand how rows and columns work, add borders with CSS, avoid obsolete attributes, and fix common beginner mistakes with clear examples on CodeHarborHub.
ποΈ Rows and Cells
Learn how HTML table rows (tr) and cells (td and th) work. Understand the table grid, what can go inside a cell, how to align text and numbers, control wrapping and width, handle empty cells, and avoid common row and cell mistakes with live examples on CodeHarborHub.
ποΈ Table Headings
Learn how to mark up HTML table headings with the th element and the scope attribute. Understand column headers, row headers, the empty corner cell, abbreviations, styling, and how headings help screen reader users understand every cell on CodeHarborHub.
ποΈ Caption
Learn how to give HTML tables a title with the caption element. Understand where caption goes, how it differs from headings and figcaption, how to style and position it with caption-side, number tables with CSS counters, and write captions that help screen reader users on CodeHarborHub.
ποΈ Thead, Tbody, Tfoot
Learn how to organize HTML tables with thead, tbody, and tfoot. Understand what each section means, the correct order, how to use several tbody groups, style headers and footers, build totals rows, keep headers visible while scrolling, and avoid common mistakes on CodeHarborHub.
ποΈ Colspan and Rowspan
Learn how to merge cells in HTML tables with colspan and rowspan. Understand how to count cells correctly, build grouped headers, schedules, and totals rows, avoid overlap errors, keep merged tables accessible, and decide when to split a table instead on CodeHarborHub.
ποΈ Colgroup and Col
Learn how to style and size whole table columns with the HTML colgroup and col elements. Understand placement, the span attribute, which CSS properties work on columns, setting column widths, highlighting columns, and how colgroup relates to accessibility on CodeHarborHub.
ποΈ Styling Tables
Learn how to style HTML tables with CSS. Add borders with border-collapse, spacing and padding, header styles, zebra stripes, hover effects, alignment, rounded corners, captions, and a complete modern table stylesheet, with accessibility and contrast tips on CodeHarborHub.
ποΈ Responsive Tables
Learn how to make HTML tables work on phones and small screens. Use scrolling containers, sticky columns, fluid widths, stacked card layouts, and priority columns, understand the accessibility trade-offs of each approach, and choose the right technique for your data on CodeHarborHub.
ποΈ Table Accessibility
Learn how to build accessible HTML tables. Understand how screen readers navigate tables, use captions, th and scope, the headers and id attributes for complex tables, sortable headers with aria-sort, scrollable regions, layout table pitfalls, and how to test tables on CodeHarborHub.
This section adds two lessons that were not in the original plan: Colgroup and Col (colgroup-and-col.mdx) and Styling Tables (table-styling.mdx). Column groups are part of the table model that every complete table course should cover, and tables look unfinished without CSS, so each gets its own page.
What Makes a Good Table?β
A quick checklist you will understand fully by the end of this section.
- It holds real data, not page layout.
- It has a caption that says what the table is.
- Its headers use
<th>, withscopeso every cell knows its labels. - It uses
<thead>,<tbody>, and<tfoot>when the table has meaningful sections. - It is simple. Merged cells are used sparingly.
- It is styled with CSS, not with old HTML attributes like
borderandbgcolor. - It works on small screens, without hiding data.
- It is accessible, so people using screen readers can understand it.
Common Beginner Mistakesβ
| Mistake | What goes wrong | Where you will fix it |
|---|---|---|
| Using tables for page layout | Poor accessibility and broken mobile pages | This page |
Using <td> for headers and bolding them by hand | Screen readers do not know they are headers | Table Headings |
| No caption | Users do not know what the table is about | Caption |
| Missing or extra cells in a row | The table looks broken and misaligned | Table Rows and Cells |
Overusing colspan and rowspan | Complex tables that are hard to navigate | Colspan and Rowspan |
Using old attributes like border and cellpadding | Obsolete HTML | Styling Tables |
| A wide table that breaks the page on phones | Sideways scrolling and cut-off data | Responsive Tables |
| No accessible structure | Screen reader users hear a pile of numbers | Table Accessibility |
Tables for a Global Audienceβ
Data travels across borders, but how we write it does not. Keep these habits in mind.
- State units. "Weight (kg)" in the header is better than repeating "kg" in every cell.
- Be careful with number formats. Some regions write 1,234.56, others 1.234,56. Use clear labels, and consider the
langattribute. - Be careful with dates. "03/04/2026" means March 4 in one country and April 3 in another. Write "4 March 2026," or use
<time datetime="2026-03-04">. - Right-to-left languages such as Arabic and Hebrew flip the column order when the page
diris set tortl. Use logical CSS properties and test. - Do not rely on color alone to show up or down trends. Add a symbol or word.
Quick Recapβ
Test yourself. Click a question to reveal the answer.
1. Which four elements make up the core of a table?
<table> (the container), <tr> (a row), <th> (a header cell), and <td> (a data cell).
2. How are columns created in HTML?
They are not declared directly. Cells stacked at the same position in each row form a column.
3. When is a table the right choice?
When the content is data with relationships in both directions: each cell relates to its row and its column. Schedules, comparisons, and statistics are good examples.
4. Why should you not use tables for page layout?
Screen readers treat them as data, they break on small screens, and they mix design with content. Use CSS Grid or Flexbox for layout.
5. Name three elements that add structure beyond the basics.
<caption>, <thead>, <tbody>, <tfoot>, <colgroup>, and <col>. Any three are correct.
Key Takeawaysβ
- A table presents data in rows and columns, built with
<table>,<tr>,<th>, and<td>. - You write rows and cells. Columns appear automatically.
- Add meaning with
<caption>,<thead>,<tbody>,<tfoot>, andscope. - Use tables for data, never for layout. Use lists or description lists when the data is not a grid.
- Style with CSS, make tables work on small screens, and make them accessible to everyone.
- Write units, dates, and numbers clearly, so your data makes sense around the world.
Ready to begin? Start with Table Basics.