Merge HTML Table Cells: colspan and rowspan Guide
November 11, 2026 · 9 min read
Merge HTML Table Cells with colspan and rowspan
To merge HTML table cells, add colspan to a cell to stretch it across columns, or rowspan to stretch it down rows. Then delete the cells it now covers. Every row must still add up to the same number of column slots, and that one rule keeps your merged tables aligned.
How Do You Merge Cells in an HTML Table?
You don't join two cells together. You make one cell bigger and remove the cells it swallows. Start with a plain 2 by 2 table:
<table>
<tr><td>Name</td><td>Role</td></tr>
<tr><td>Ada</td><td>Lead</td></tr>
</table>
That renders as four boxes in two rows. To turn the top row into one wide title cell, give the first cell colspan="2" and delete the second one.
<table>
<tr><td colspan="2">Team</td></tr>
<tr><td>Ada</td><td>Lead</td></tr>
</table>
Now "Team" fills the full width of the first row, and the second row still shows two cells underneath it. Both attributes work on <td> and <th>, and both default to 1, which means no merge at all. In JavaScript, the same values live on the cell's colSpan and rowSpan properties.
What Does the colspan Attribute Do?
colspan tells a cell how many columns wide it is. A cell with colspan="3" occupies three column slots in its row, so that row needs two fewer cells than its neighbours. The classic use is a totals row on an invoice:
<table>
<tr><th>Item</th><th>Qty</th><th>Price</th></tr>
<tr><td>Paper</td><td>2</td><td>8.00</td></tr>
<tr><td>Toner</td><td>1</td><td>45.00</td></tr>
<tr><td colspan="2">Total</td><td>53.00</td></tr>
</table>
The last row renders "Total" across the Item and Qty columns, with 53.00 sitting under Price. That's an HTML table with merged cells in its simplest useful form. Browsers draw no cell lines by default, so add an HTML table border to see each span clearly.
The value must be a whole number from 1 to 1000. The HTML Living Standard tells browsers to treat anything above 1000 as 1000, and to treat 0 or anything that isn't a valid number as 1. So colspan="0" doesn't mean "span everything". It quietly does nothing.
Merging Rows Vertically with rowspan
rowspan makes a cell taller. The cell keeps its slot in the rows below, so each of those rows gets one fewer cell. A team rota shows the pattern:
<table>
<tr><th>Team</th><th>Person</th><th>Shift</th></tr>
<tr><td rowspan="3">Support</td><td>Ana</td><td>Early</td></tr>
<tr><td>Ben</td><td>Late</td></tr>
<tr><td>Chloe</td><td>Night</td></tr>
</table>
"Support" renders once, as a tall cell running down three rows. The Ben and Chloe rows only list two cells each because the first slot is already taken.
A few limits from the spec and MDN's td reference:
- The maximum is 65534. Larger values are clipped to it.
rowspan="0"stretches the cell to the end of its row group, meaning its<thead>,<tbody>or<tfoot>. MDN's compatibility data lists support in Chrome, Edge and Firefox but not in Safari, so we write the real number instead.- A cell can't belong to two row groups. A rowspan never carries from one
<tbody>into the next.
You can also put both attributes on one cell. A cell with colspan="2" and rowspan="2" fills a 2 by 2 block. Its own row loses one other cell, and the row below loses two.
<table>
<tr><td colspan="2" rowspan="2">Logo</td><td>Home</td></tr>
<tr><td>Docs</td></tr>
<tr><td>Blog</td><td>Shop</td><td>Help</td></tr>
</table>
"Logo" renders as one large cell filling the top left 2 by 2 block. Home and Docs stack beside it, and the third row returns to three ordinary cells.
Count Grid Slots Before You Merge HTML Table Cells
Most broken merged tables come from a row with the wrong slot total. The browser builds the table as a grid of slots, places each cell in the next free slot, and skips slots that a rowspan from above already fills. You can run the same check on paper:
- Decide how many columns the table has. Call it N.
- For each row, add up the
colspanof every cell in it. - Add 1 for each cell from a row above whose
rowspanreaches into this row. - Every row should total N. Fix any row that doesn't.
Here's a table that fails the check:
<table>
<tr><td>A</td><td rowspan="2">B</td><td>C</td></tr>
<tr><td>D</td><td>E</td><td>F</td></tr>
</table>
The first row totals 3. The second row has three cells plus B carried down, so it totals 4. The browser puts D in column 1, skips column 2 for B, then places E in column 3 and F in a brand new column 4. You get a ghost column with an empty gap where the first row ends.
To fix it, remove one cell from the second row, so <tr><td>D</td><td>E</td></tr> brings it back to 3.
The other failure is overlap. In this table, C starts in column 1 and asks for two columns. But B already claims column 2 of that row.
<table>
<tr><td>A</td><td rowspan="2">B</td></tr>
<tr><td colspan="2">C</td></tr>
</table>
This is what the spec calls a table model error, and documents must not contain one. Browsers still draw something, but the two cells collide, so don't rely on the result. Change C to colspan="1" or shorten B to rowspan="1".
Try it below. The table is raw HTML inside Markdown, and the preview draws it with the spans applied.
The demo leaves out the quotes around attribute values. That's valid HTML for simple values like 2, though we keep the quotes in real pages.
Spanning Header Cells for Two-Level Column Headers
Merged headers are where spans earn their keep. A group header with colspan sits above its sub-columns, and a row label header uses rowspan to stand beside both header rows. Here, each year covers two quarters:
<table>
<colgroup></colgroup>
<colgroup span="2"></colgroup>
<colgroup span="2"></colgroup>
<thead>
<tr>
<th rowspan="2" scope="col">Region</th>
<th colspan="2" scope="colgroup">2025</th>
<th colspan="2" scope="colgroup">2026</th>
</tr>
<tr>
<th scope="col">Q1</th><th scope="col">Q2</th>
<th scope="col">Q1</th><th scope="col">Q2</th>
</tr>
</thead>
<tbody>
<tr><th scope="row">North</th><td>120</td><td>135</td><td>150</td><td>162</td></tr>
</tbody>
</table>
That renders "Region" as a tall header on the left, with "2025" and "2026" each spread over a Q1 and Q2 pair. Run the slot count: the first header row is 1 + 2 + 2, the second is 4 plus Region carried down, and the body row is 1 + 4. All three total 5.
The <colgroup> elements define the column groups that scope="colgroup" points at. They don't draw anything. The <thead> and <tbody> wrappers just separate header rows from data rows. Our HTML table tutorial explains each of those grouping tags.
Making Merged Cells Readable to Screen Readers with scope and headers
Sighted readers see a merged header sitting over its columns. A screen reader only knows that if the markup says so. Plain headers take scope="col" or scope="row". A header that spans several columns takes scope="colgroup", and one that spans a whole <tbody> or other row group takes scope="rowgroup". Put scope on <th> only, because MDN lists scope on <td> as deprecated.
When a data cell sits under two header levels that scope can't describe cleanly, use headers. Give each <th> an id, then list the relevant ids, separated by spaces, on the data cell:
<table>
<tr><td></td><th id="y25" colspan="2">2025</th></tr>
<tr><td></td><th id="q1">Q1</th><th id="q2">Q2</th></tr>
<tr>
<th id="north">North</th>
<td headers="north y25 q1">120</td>
<td headers="north y25 q2">135</td>
</tr>
</table>
Visually, it's a smaller version of the grid above. Behind the scenes, the 120 cell now names North, 2025 and Q1 as its headers, so assistive technology can announce all three with the value.
We prefer scope on every <th> and reach for headers and id only when a cell genuinely sits under two levels that scope can't express. The W3C covers both patterns in its irregular headers tutorial and its multi-level headers tutorial.
Merged Cells in Markdown, Spreadsheets and Converters
Markdown pipe tables can't merge cells. Our Markdown table guide covers that gap and the rest of the pipe syntax. The usual workaround is to write that one table as raw HTML inside your Markdown file. Many renderers that allow HTML, GitHub among them, keep the spans. Paste the table into our editor and the live preview renders it on the right. If you're unsure when to make that switch, read Markdown vs HTML.
Conversion is where spans disappear. Our table to Markdown converter accepts pasted HTML tables and flattens each span by repeating the cell text into every slot it covered. There's no better option, because a pipe table can't hold a merge.
Here's the limitation to weigh before you merge anything. Merged cells make tables harder to sort, filter, squeeze onto a phone screen, or round-trip through CSV and spreadsheets. Merge only where the span carries meaning, such as a shared category or a group header, and keep the data rows plain.
Quick Answers on colspan and rowspan
To merge HTML table cells reliably, widen or lengthen one cell, delete what it covers, and count slots in every row before you publish. Add scope to each header so screen readers follow the merges too. You can build the plain grid first in our HTML table generator, which doesn't output merged cells, then add colspan and rowspan by hand and check the result in the editor preview.