Sub topic 6
Merging cells with colspan and rowspan
This chapter covers the two attributes that let a cell cover more than one grid position. It assumes you can write a basic table with table, tr, td, and th, and that you know how attributes work. It also assumes you have read the earlier chapters on header cells and row groups.
The grid model
A table is a grid. The browser reads each tr in order and places its cells into columns from left to right. A cell normally occupies one grid slot: one column in one row. The colspan and rowspan attributes make a cell occupy several slots.
colspan="n"makes the cell coverncolumns in its own row.rowspan="n"makes the cell covernrows, starting with its own row.
Both attributes default to 1. The value must be a positive whole number. The HTML specification limits colspan to 1000 and rowspan to 65534. Browsers clamp larger values.
The key rule is that you do not write placeholder cells for the slots a spanning cell covers. A cell that spans takes up those slots, so you leave the cells out of the markup.
Spanning columns with colspan
A colspan cell is the simplest case. It stays in one row and stretches sideways. A common use is a heading above a group of related columns.
<table>
<tr>
<th colspan="3" scope="colgroup">Quarterly report</th>
</tr>
<tr>
<th scope="col">Region</th>
<th scope="col">Q1</th>
<th scope="col">Q2</th>
</tr>
<tr>
<th scope="row">North</th>
<td>120</td>
<td>135</td>
</tr>
</table>The grid has three columns. The first row has one cell, and it covers all three. The other rows have three cells each. Every row accounts for three slots, which is what keeps the grid valid.
Spanning rows with rowspan
A rowspan cell stretches downward. The cell is written in the first row it covers. In each later row it covers, you write fewer cells, because the spanning cell already fills that slot.
<table>
<tr>
<th scope="col">Day</th>
<th scope="col">Time</th>
<th scope="col">Class</th>
</tr>
<tr>
<th scope="row" rowspan="2">Monday</th>
<td>09:00</td>
<td>Maths</td>
</tr>
<tr>
<td>11:00</td>
<td>Art</td>
</tr>
</table>The second body row has only two cells: 11:00 and Art. The browser places them in columns two and three because column one is already occupied by Monday, which started in the row above.
Combining both attributes
The example from the topic plan uses both attributes together. The Name header spans two rows and the Scores header spans two columns.
<table>
<tr>
<th rowspan="2">Name</th>
<th colspan="2">Scores</th>
</tr>
<tr>
<th>Math</th>
<th>Art</th>
</tr>
<tr>
<td>Ana</td>
<td>90</td>
<td>85</td>
</tr>
</table>Count the slots in each row to check that the table is well formed:
| Row | Cells written | Slots covered | Explanation |
|---|---|---|---|
| 1 | `Name`, `Scores` | 3 | `Name` covers 1 slot in this row. `Scores` covers 2. |
| 2 | `Math`, `Art` | 3 | `Name` still covers column 1, so these two cells fill columns 2 and 3. |
| 3 | `Ana`, `90`, `85` | 3 | No spans are active, so three cells fill three columns. |
Every row must add up to the same total, counting both the cells it contains and the slots that earlier rowspan cells still occupy. That total is the width of the table.
How the browser places cells
Knowing the placement algorithm helps you diagnose broken tables. In simplified form, for each row the browser keeps a cursor at the leftmost column. It then does the following:
- Skip any column already occupied by a
rowspancell from an earlier row. - Put the next cell of the row in that free column.
- Mark the columns covered by its
colspan, and the rows covered by itsrowspan. - Move the cursor right and repeat until the row has no more cells.
The following diagram shows the placement of the Name/Scores table.
What happens when the grid is wrong
HTML does not reject a badly formed table. The browser repairs it by its own rules, and the result is often not what you intended. Three problems are common.
A row is too short or too long
If one row covers fewer slots than the widest row, the browser leaves empty space at the end of that row. If a row covers more slots, the browser adds columns to the whole table. Either way, the layout shifts, and data may fall under the wrong header.
A rowspan runs past the end of its group
A rowspan cannot cross from one row group to another. If you write rowspan="5" in a tbody with only three rows, the browser clips the span at the end of the group. This matters when you mix thead, tbody, and tfoot, because each one is its own group. A cell in thead can never extend into tbody.
Two cells overlap
If a span and a later cell claim the same slot, the table has overlapping cells. The specification calls this an error. Browsers render the overlap in different ways, and assistive technology may announce it unpredictably. Always fix the markup so that no slot is claimed twice.
Using spans with accessibility attributes
A spanning cell often acts as a header for several columns or rows. Tell assistive technology which direction it applies to with the scope attribute.
- A
thwithcolspanthat heads a set of columns can usescope="colgroup". - A
thwithrowspanthat heads a set of rows can usescope="rowgroup". - A
ththat heads a single column or row usesscope="col"orscope="row".
Here, scope="rowgroup" on the Monday cell tells a screen reader that the cell labels both rows it covers.
<table>
<caption>Timetable</caption>
<tr>
<th scope="col">Day</th>
<th scope="col">Time</th>
<th scope="col">Class</th>
</tr>
<tr>
<th scope="rowgroup" rowspan="2">Monday</th>
<td>09:00</td>
<td>Maths</td>
</tr>
<tr>
<td>11:00</td>
<td>Art</td>
</tr>
</table>Simple scope values are enough when headers form one level on each axis. When a header sits beneath another spanning header, as in 2025 over Q1 and Q2, the scope values may not be enough. A later chapter covers id and headers for that case.
When not to merge cells
Merged cells make a table harder to read with a screen reader, harder to sort, and harder to edit. Consider these alternatives before you use a span.
| Situation | Better option |
|---|---|
| A label repeats down a column to save space | Repeat the label in each row, or split into several tables |
| A spanning cell is only decoration | Remove the span and use CSS spacing |
| A note applies to the whole table | Put it in the `caption` or in a paragraph near the table |
| Data has more than two header levels | Split it into separate tables, each with simple headers |
Use a span when the data truly has a grouped structure, such as a heading that covers several columns. Do not use one to produce a visual layout.
Debugging checklist
When a table with spans looks wrong, work through these steps:
- Write down the intended column count.
- For each row, add the cells you wrote to the slots taken by
rowspancells from earlier rows. - Find the first row whose total differs from the width.
- Check whether a
rowspancrosses athead,tbody, ortfootboundary. - Remove any cell you added only as a placeholder.
Browser developer tools help with this. Many of them can show the table grid or highlight a cell's boundaries when you inspect it.
Summary of rules
colspanstretches a cell across columns in its own row.rowspanstretches it across rows.- Omit the cells that a span already covers.
- Every row must account for the same number of slots.
- A span cannot cross from one row group into another.
- Use
scope="colgroup"andscope="rowgroup"on spanning headers.
Exercise
A table has four columns. Row 1 contains a th with rowspan="2" followed by three td cells. How many cells must row 2 contain, and why?
Show answer
Row 2 must contain three cells. The th from row 1 occupies column 1 of row 2, so the other three columns need cells. Adding a fourth cell would make the row five slots wide and break the grid.
Exercise
This table is meant to have three columns, but the last row renders with an extra column. Find the error. <tr><th rowspan="2">A</th><td>1</td><td>2</td></tr> <tr><td></td><td>3</td><td>4</td></tr>
Show answer
The second row contains three cells (<td></td>, 3, 4), but column 1 is already covered by the rowspan cell A. The row covers four slots in total, so the table gains an extra column. Delete the empty <td></td>.
Exercise
A th with colspan="2" sits in the thead and heads two columns. Which scope value suits it, and can it use rowspan to extend into the tbody?
Show answer
Use scope="colgroup". It cannot extend into the tbody, because a rowspan is clipped at the end of its row group. The thead and tbody are separate groups.