Tables are the best way to show data on a web page: anything that naturally fits into rows and columns, such as prices, timetables, results or specifications. In this guide, we'll build tables from the ground up, make them accessible, style them with modern CSS and make them work on small screens.
Tip: to create table markup quickly, try our HTML Table Generator.
When to use a table
Use a table when your content has a genuine two-way relationship: each piece of information belongs to a row and a column. A good test is whether the columns would have headings, like "Name", "Price" or "Date".
In the early days of the web, tables were also used to lay out whole pages. Don't do this today. Screen readers (software that reads pages aloud) announce tables as data, with row and column positions, which is confusing for a page layout. Tables also don't adapt well to small screens. For layout, use CSS Grid and flexbox instead.
The basic structure
A table is built from a handful of elements:
| Element | What it does |
|---|---|
<table> | Wraps the whole table |
<caption> | Gives the table a title (it must be the first thing inside <table>) |
<thead> | Groups the header rows |
<tbody> | Groups the main data rows |
<tfoot> | Groups summary rows, such as totals |
<tr> | A table row |
<th> | A header cell, labelling a column or row |
<td> | A data cell |
Here's a simple table using them:
<table>
<caption>Team members</caption>
<thead>
<tr>
<th>Name</th>
<th>Role</th>
<th>Location</th>
</tr>
</thead>
<tbody>
<tr>
<td>Ada Lovelace</td>
<td>Developer</td>
<td>London</td>
</tr>
<tr>
<td>Grace Hopper</td>
<td>Engineer</td>
<td>New York</td>
</tr>
<tr>
<td>Alan Turing</td>
<td>Analyst</td>
<td>Manchester</td>
</tr>
</tbody>
</table>
Without any CSS, browsers display it like this:
| Name | Role | Location |
|---|---|---|
| Ada Lovelace | Developer | London |
| Grace Hopper | Engineer | New York |
| Alan Turing | Analyst | Manchester |
It's functional but plain: header cells are bold, but there are no borders, little spacing and nothing to help your eye follow a row. We'll style it shortly.
Headers, captions and accessibility
A well-built table lets screen reader users explore it cell by cell, hearing the matching headers as they go, such as "Role, Developer". Three things make that work.
Use <th> for every header
Any cell that labels other cells should be a <th>, not a <td> that's just been made bold. Headers can label rows as well as columns. For example, in a price comparison, each plan's name labels its row:
<tr>
<th scope="row">Pro plan</th>
<td>$12</td>
<td>50 GB</td>
</tr>
Add scope to clarify headers
The scope attribute says which way a header points: scope="col" for a column header, scope="row" for a row header. Browsers can often work this out for simple tables, but adding it removes any doubt, and it's essential when a table has both row and column headers.
Give the table a caption
A <caption> works as the table's title. Screen readers announce it when someone reaches the table, so they know what it contains before diving in. If you'd rather show a heading above the table instead, you can link the two with aria-labelledby.
Spanning cells with colspan and rowspan
Sometimes a cell needs to stretch across more than one column or row:
colspan="2"makes a cell two columns wide.rowspan="2"makes a cell two rows tall.
<table>
<caption>Contact list</caption>
<thead>
<tr>
<th colspan="2" scope="colgroup">Name</th>
<th rowspan="2" scope="col">Age</th>
<th rowspan="2" scope="col">City</th>
</tr>
<tr>
<th scope="col">First</th>
<th scope="col">Last</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>30</td>
<td>New York</td>
</tr>
<tr>
<td>Jane</td>
<td>Smith</td>
<td>28</td>
<td>Los Angeles</td>
</tr>
</tbody>
</table>
| Name | Age | City | |
|---|---|---|---|
| First | Last | ||
| John | Doe | 30 | New York |
| Jane | Smith | 28 | Los Angeles |
The spanning cells are shaded darker so you can see them. "Name" covers the First and Last columns, and "Age" and "City" each fill two header rows. Notice that the second row only has two cells: the rowspan cells from the row above already fill the other two positions.
Tip: merged cells make tables harder to follow, especially with a screen reader. If you find yourself nesting lots of spans, consider splitting the data into two simpler tables.
Styling tables with CSS
Here's a styled version of a price table, with a caption, row headers, right-aligned numbers, a total in the footer, striped rows and a highlight on hover:
| Plan | Price | Storage | Users |
|---|---|---|---|
| Starter | $5 | 10 GB | 1 |
| Standard | $12 | 50 GB | 5 |
| Pro | $29 | 200 GB | 20 |
| Team | $79 | 1,000 GB | 100 |
| All plans | $125 | 1,260 GB | 126 |
table {
width: 100%;
border-collapse: collapse; /* merge neighbouring cell borders into one */
}
caption {
padding-bottom: 8px;
font-weight: 700;
text-align: left;
}
th,
td {
padding: 10px 14px;
border-bottom: 1px solid #e5e7eb;
text-align: left;
}
thead th {
border-bottom: 2px solid #1d4ed8;
color: #1e3a8a;
}
/* Zebra stripes */
tbody tr:nth-child(even) {
background: #f8fafc;
}
/* Highlight the row under the pointer */
tbody tr:hover {
background: #eff6ff;
}
/* Line up numbers neatly */
.num {
text-align: right;
font-variant-numeric: tabular-nums;
}
tfoot th,
tfoot td {
border-top: 2px solid #1f2937;
font-weight: 700;
}
border-collapse: collapsemerges the borders between neighbouring cells, so you get single lines instead of doubled ones.- Right-aligning numbers lines up the units, tens and hundreds, making columns of figures much easier to compare.
font-variant-numeric: tabular-numsmakes every digit the same width, so numbers line up perfectly from row to row.- Subtle zebra stripes help eyes track across a wide row. Keep them light, so text keeps strong contrast.
Tip: by default, a table sizes its columns to fit their contents. Add table-layout: fixed to the table, and set widths on the first row or with <col> elements, to control column widths yourself. It also helps large tables display faster.
Scrolling and sticky headers
Long or wide tables can sit inside a scrolling container. Add position: sticky to the header cells, and the column headings stay visible as you scroll. Scroll inside the table below:
| Month | North | South | East | West | Central |
|---|---|---|---|---|---|
| Jan | $42,410 | $25,838 | $62,590 | $31,192 | $20,120 |
| Feb | $63,662 | $49,919 | $19,327 | $78,649 | $58,383 |
| Mar | $34,946 | $25,368 | $39,126 | $94,926 | $45,919 |
| Apr | $46,298 | $33,417 | $49,742 | $59,188 | $89,445 |
| May | $97,497 | $76,354 | $34,353 | $72,386 | $23,937 |
| Jun | $82,960 | $50,107 | $49,686 | $51,968 | $77,299 |
| Jul | $64,533 | $88,395 | $67,562 | $32,338 | $51,365 |
| Aug | $17,183 | $17,573 | $92,387 | $78,647 | $94,582 |
| Sep | $55,248 | $98,300 | $20,522 | $37,750 | $92,551 |
| Oct | $47,288 | $57,546 | $87,428 | $93,671 | $37,431 |
| Nov | $24,959 | $19,825 | $41,384 | $86,730 | $42,225 |
| Dec | $54,281 | $49,570 | $15,143 | $57,814 | $22,392 |
<div class="table-scroll" role="region" aria-labelledby="sales-caption" tabindex="0">
<table>
<caption id="sales-caption">Monthly sales by region</caption>
…
</table>
</div>
.table-scroll {
max-height: 240px;
overflow: auto; /* scroll in both directions when needed */
}
.table-scroll thead th {
position: sticky;
top: 0; /* stick to the top of the scrolling box */
z-index: 1; /* stay above the rows as they scroll past */
}
The attributes on the wrapper make the scrolling box accessible:
tabindex="0"lets keyboard users focus the box and scroll it with the arrow keys.role="region"witharia-labelledbygives the box a name ("Monthly sales by region"), so screen reader users know what they've landed on.
Responsive tables
Tables can be awkward on phones, because they're often wider than the screen. There are two main approaches.
1. Let the table scroll sideways
Wrapping the table in a scrolling container, as above, is the simplest and most reliable option. The table keeps its structure, and people can swipe to see more columns. It suits tables where comparing across rows matters.
2. Stack rows as cards
For simpler tables, each row can become a card on narrow screens, with the column name shown beside each value. The demo below uses a container query, so it reacts to the width of its box rather than the screen. Drag the box's bottom-right corner to make it narrower (on a phone, it's already stacked):
| Plan | Price | Storage | Support |
|---|---|---|---|
| Starter | $5 | 10 GB | |
| Standard | $12 | 50 GB | Email and chat |
| Pro | $29 | 200 GB | Priority phone |
Each data cell carries its column name in a data-label attribute, which CSS displays using ::before:
<td data-label="Plan">Pro</td>
.stack-wrapper {
container-type: inline-size; /* let children respond to this box's width */
}
@container (max-width: 480px) {
/* Hide the header row visually, but keep it for screen readers */
.stack thead {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
.stack,
.stack tbody,
.stack tr {
display: block;
}
.stack tbody tr {
margin-bottom: 12px;
border: 1px solid #d1d5db;
border-radius: 8px;
}
.stack tbody :is(th, td) {
display: flex;
justify-content: space-between;
}
.stack td::before {
content: attr(data-label);
font-weight: 600;
}
}
Accessibility warning: changing the display of table elements can make some browsers stop treating them as a table, so screen readers lose the row and column information. The original version of this post also hid the header row with display: none, which removes the headers for screen readers entirely. Hide it visually instead, as above. If you use stacking, test it with a screen reader, or add ARIA roles (role="table", "row", "columnheader", "cell") to restore the table structure. When in doubt, horizontal scrolling is the safer choice.
A third option, hiding less important columns on small screens, should be used with care: people on phones may need that information too. If you do hide columns, offer another way to see them, such as a link to the full table.
Quick reference
Checklist for good tables
- Use tables for data only, never for page layout.
- Add a
<caption>describing the table. - Use
<th>for every header, withscope="col"orscope="row". - Group rows with
<thead>,<tbody>and<tfoot>. - Keep merged cells to a minimum.
- Right-align numbers, and use
tabular-nums. - On small screens, let wide tables scroll inside an accessible, focusable container.
HTML references
- HTML <table> tag
- HTML <caption> tag
- HTML <thead> tag
- HTML <tbody> tag
- HTML <tfoot> tag
- HTML <tr> tag
- HTML <th> tag
- HTML <td> tag
Wrapping up
Good tables start with good HTML: a caption, proper <th> headers with scope, and sensible grouping into head, body and foot. From there, a few lines of CSS (collapsed borders, comfortable padding, aligned numbers and light stripes) make data easy to read, and a scrolling wrapper or a careful stacked layout keeps it usable on any screen.