HTML Tables — Structure, Spanning & Styling
Tables organise data into rows and columns — course schedules, pricing grids, comparison charts, and more. In this chapter you'll master every table tag, learn how to merge cells with colspan and rowspan, group rows semantically, and make tables fully responsive with CSS.
<table>, <tr>, <th>, <td><thead>, <tbody>, <tfoot><caption>colspan
and rows with rowspanWhat is an HTML Table?
An HTML table displays data in a grid of rows and
columns. Every table is built from three core elements: <table> (the container), <tr> (table row), and either <th> (table header cell) or <td> (table data cell). Here is a fully labelled example of a real
table rendered live:
| <th> Course Name | <th> Duration | <th> Level | <th> Fee |
|---|---|---|---|
| Web Development With AI | 40 hrs | Beginner–Advanced | ₹8,999 |
| Python Masterclass | 30 hrs | Beginner–Intermediate | ₹6,999 |
| Data Analysis | 25 hrs | Intermediate | ₹7,499 |
| <tfoot> — Total courses available at K2infocom | 15+ | ||
All HTML Table Tags Explained
Here is a complete reference of every element used to build an HTML table:
<th> or <td> cells. Each
<tr> represents one row of data.
scope attribute (col or row) improves accessibility
for screen readers.colspan
and rowspan for spanning.<thead> repeats on every page.<tbody> elements in one table to visually group
related rows of data.<tbody> in the HTML source.alt, it describes what the table contains.Basic Table Syntax
Here is the minimal, correct structure for an HTML table with <thead>, <tbody>, and <tfoot>:
<table>
<!-- Optional title for the table -->
<caption>K2infocom Course Overview</caption>
<!-- Header rows -->
<thead>
<tr>
<th scope="col">Course</th>
<th scope="col">Duration</th>
<th scope="col">Fee</th>
</tr>
</thead>
<!-- Data rows -->
<tbody>
<tr>
<td>Web Development With AI</td>
<td>40 hrs</td>
<td>₹8,999</td>
</tr>
<tr>
<td>Python Masterclass</td>
<td>30 hrs</td>
<td>₹6,999</td>
</tr>
</tbody>
<!-- Footer / summary row -->
<tfoot>
<tr>
<td colspan="2">Total Courses Available</td>
<td>15+</td>
</tr>
</tfoot>
</table>
Merging Cells — colspan & rowspan
Two attributes let you merge cells across multiple columns or rows, creating complex layouts:
<td> cells from that row to keep the grid valid.<td> cells from
the rows below to keep the grid valid.| colspan="3" — spans 3 columns | ||
| Col 1 | Col 2 | Col 3 |
| Data A | Data B | Data C |
| rowspan="3" spans 3 rows |
Row 1 Data |
| Row 2 Data | |
| Row 3 Data |
<table>
<thead>
<tr>
<!-- Spans across 2 columns -->
<th colspan="2">Student Info</th>
<th>Score</th>
</tr>
<tr>
<th>Name</th>
<th>City</th>
<th>Marks</th>
</tr>
</thead>
<tbody>
<tr>
<!-- rowspan spans this cell across 2 rows -->
<td rowspan="2">Rahul Shah</td>
<td>Vadodara</td>
<td>92</td>
</tr>
<tr>
<!-- No first cell needed — covered by rowspan above -->
<td>Surat</td>
<td>88</td>
</tr>
</tbody>
</table>
colspan="3", remove 2 cells from that row. When using rowspan="2", remove 1 cell from the row below in the same column. The total
number of cells in every row must always equal the number of columns — otherwise the table renders broken.
Table Attribute Reference
Here is a complete reference of the most important HTML table attributes:
| Attribute | Applied To | Purpose |
|---|---|---|
| colspan | <td>, <th>
|
Merges the cell across the specified number of columns horizontally |
| rowspan | <td>, <th>
|
Merges the cell across the specified number of rows vertically |
| scope | <th> |
Declares whether the header applies to a col or a row — critical for screen reader accessibility |
| headers | <td> |
Associates a data cell with one or more header cells by their id — for complex table accessibility |
| caption-side | CSS on <caption> |
CSS property — top (default) or bottom — positions the caption above or below the table |
Styling Tables with CSS
Plain HTML tables look unstyled by default. Here is the CSS used to create clean, professional-looking tables — and the result rendered live below:
/* Responsive wrapper — allows horizontal scroll on mobile */
.table-wrap {
width: 100%;
overflow-x: auto;
border-radius: 12px;
border: 1px solid #e5e7eb;
}
/* Base table reset */
table {
width: 100%;
border-collapse: collapse;
font-size: 15px;
min-width: 480px;
}
/* Caption — visible title */
caption {
padding: 12px 16px;
font-weight: 700;
text-align: left;
background: #f9fafb;
border-bottom: 1px solid #e5e7eb;
}
/* Header cells */
thead th {
background: #f3f4f6;
padding: 12px 16px;
font-weight: 700;
text-align: left;
border-bottom: 2px solid #e44d26;
white-space: nowrap;
}
/* Data cells */
tbody td {
padding: 12px 16px;
border-bottom: 1px solid #e5e7eb;
vertical-align: middle;
}
/* Striped rows — alternating background */
tbody tr:nth-child(even) {
background: #f9fafb;
}
/* Hover highlight */
tbody tr:hover {
background: rgba(228, 77, 38, 0.04);
}
/* Footer row */
tfoot td {
padding: 10px 16px;
font-weight: 700;
background: #f3f4f6;
border-top: 2px solid #e44d26;
}
Here is that CSS applied to a real table — rendered live:
| Course | Batch Start | Duration | Mode | Seats | Status |
|---|---|---|---|---|---|
| Web Dev With AI | 1 Jun 2025 | 40 hrs | Online + Offline | 30 | Open |
| Python Masterclass | 15 Jun 2025 | 30 hrs | Online | 40 | Open |
| Data Analysis | 1 Jul 2025 | 25 hrs | Online | 35 | Full |
| MERN Stack | 10 Jul 2025 | 50 hrs | Offline | 25 | Open |
| Total seats across all open batches | 95 | — | |||
Making Tables Responsive
Wide tables overflow on small screens. The standard solution is to wrap the table in a
<div> with overflow-x: auto —
this adds a horizontal scroll bar on mobile without breaking the table structure.
<!-- Wrap your table in a scrollable div -->
<div class="table-wrap" role="region" aria-label="Course schedule table">
<table>
...your table content...
</table>
</div>
.table-wrap {
width: 100%;
overflow-x: auto;
-webkit-overflow-scrolling: touch; /* smooth scroll on iOS */
border-radius: 12px;
border: 1px solid var(--border);
}
table {
min-width: 480px; /* prevents columns from collapsing too narrow */
width: 100%;
border-collapse: collapse;
}
role="region" and aria-label="Table description" to the wrapper <div>. This tells screen readers that the scrollable region contains
important content, making the horizontal scroll accessible to keyboard and screen reader users.
Common Mistakes — Do's & Don'ts
These are the most frequent table mistakes beginners make:
<!-- Use scope for accessibility -->
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Score</th>
</tr>
</thead>
<!-- colspan: remove matching cells -->
<tr>
<td colspan="2">Total</td>
<td>100</td>
</tr>
<!-- Tables for layout — never do this -->
<table>
<tr>
<td>Sidebar</td>
<td>Main Content</td>
</tr>
</table>
<!-- colspan without removing cells (broken) -->
<tr>
<td colspan="2">Total</td>
<td>Extra</td>
<td>100</td>
</tr>
Try It Yourself
Edit the table code below and click Open in Editor to see your table live in full screen.
colspan="2" on the first header to colspan="3" — notice how it spans further. Then try adding a rowspan="2" on a <td> to see
vertical merging in action.
Quick Check — Chapter 9
Expand each question to check your understanding before moving on to the multiple choice quiz below.
<th> is a table header cell — it is
semantically marked as a label for a column or row. Browsers render it bold and
centre-aligned by default, and screen readers announce it as a header. <td> is a table data cell — it holds the actual
content of the table. It is rendered left-aligned with normal font weight. Always use <th> for column and row headings (with scope="col" or scope="row") and
<td> for all other data.
<thead> groups the header rows, <tbody> groups the data rows, and <tfoot> groups the footer rows (totals, summaries). They are
not strictly required — a table can work with just <tr> and <td> — but
using them is strongly recommended for accessibility, CSS styling flexibility, and
because <thead> repeats on every printed page.
colspan="N" on a <td> or
<th> makes that cell span N columns
horizontally. To use it correctly, you must remove the equivalent number of
cells from that row — otherwise the table has more cells than columns and renders broken. For
example, colspan="3" in a 4-column table means that row only needs 2
more cells (1 spanning + 2 regular = 4 total column width). Always count that every row's total column
span equals the number of columns.
<table> inside a <div> with CSS overflow-x: auto. This keeps the table at its natural width but adds a
horizontal scrollbar on narrow screens, allowing users to scroll to see all columns without the table
being squished or broken. Set a min-width on the table itself to
prevent columns from collapsing too narrow. Also add role="region"
and aria-label on the wrapper div so screen reader users know the
scrollable region contains data.
scope attribute tells screen readers which cells a
header applies to. scope="col" means the header describes
all cells in its column below it. scope="row" means the header
describes all cells in its row to the right. Without scope, screen
readers may struggle to associate data cells with their correct headers — especially in tables with
merged cells or complex structures. Adding scope to every <th> is a simple one-attribute fix that significantly improves
table accessibility.
<div>
makes a wide table scrollable on mobile?- HTML tables use
<table>,<tr>,<th>, and<td>as their core elements. Use tables only for tabular data — never for page layout. <th>is a header cell (bold, centred, semantic).<td>is a data cell. Always addscope="col"orscope="row"to<th>for accessibility.- Group rows semantically:
<thead>for headers,<tbody>for data,<tfoot>for summaries. Add<caption>for a visible table title. colspan="N"merges N columns horizontally.rowspan="N"merges N rows vertically. Always remove the equivalent cells to keep the grid valid.- Style tables with
border-collapse: collapse, zebra-striped rows (:nth-child(even)), and hover highlight for clean professional tables. - Make tables responsive by wrapping in a
<div>withoverflow-x: autoand setting amin-widthon the table itself.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions, and stay updated with new tutorials.