Sub topic 6
Merging cells with colspan and rowspan
This chapter assumes you can write a basic table with table, tr, td, and th, and that you know what an attribute is. By the end you will be able to merge cells across columns and rows, and you will know how to find the mistakes that break the grid.
How the table grid works
A table is a grid of rows and columns. Normally each cell fills one slot. Two attributes let a cell fill more than one:
colspanmakes a cell cover several columns in its own row.rowspanmakes a cell cover several rows in its own column.
Both take a positive whole number. The default is 1. The browser reads the table row by row and places each cell in the next free slot. A spanning cell claims every slot it covers, and the other cells fit around it. Counting slots is the only skill you need.
A worked example
This table has a header cell that spans two columns and another that spans two rows.
<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 row by row. The grid has three columns.
| Row | Cells written in the markup | Slots filled |
|---|---|---|
| 1 | `Name` (rowspan 2), `Scores` (colspan 2) | 1 + 2 = 3 |
| 2 | `Math`, `Art` | 2, plus 1 slot taken by `Name` from row 1 |
| 3 | `Ana`, `90`, `85` | 3 |
Row 2 contains only two cells in the markup. That is correct, because Name already occupies its first slot. You do not write an empty cell to hold the place.
The rule for each row
Add up the slots in every row. The total must equal the number of columns. For each row:
- Start with the cells you wrote, counting a cell with
colspan="n"as n. - Add one for each cell from a previous row whose
rowspanreaches into this row, counting itscolspanas well. - Check that the total matches the other rows.
Common errors
Browsers do not report span mistakes. They repair the table in their own way, so the result looks wrong with no warning.
- Writing a cell the rowspan already covers. If
Namehasrowspan="2"and you also add an emptytdin row 2, that cell moves one slot to the right. The table grows a fourth column in that row. - Spanning past the last column. A
colspanlarger than the space left makes the browser add columns. The other rows then look short. - Overlapping spans. A
rowspancell and acolspancell may not claim the same slot. This is invalid HTML, and browsers handle it inconsistently. - Crossing a row group. A
rowspancannot extend out of itsthead,tbody, ortfoot. The browser cuts it off at the group boundary.
A body example with a rowspan that is written correctly:
<table>
<tr>
<th scope="col">Day</th>
<th scope="col">Time</th>
<th scope="col">Class</th>
</tr>
<tr>
<td rowspan="2">Monday</td>
<td>09:00</td>
<td>Math</td>
</tr>
<tr>
<td>11:00</td>
<td>Art</td>
</tr>
</table>The last row has two cells because Monday fills its first slot.
Spans and accessibility
A header cell that spans several columns or rows needs extra markup, so that a screen reader knows which cells it applies to. The chapters on scope and on the id and headers attributes cover this. For now, remember that spans make a header's reach harder to infer, so keep them to a minimum.
Special values
rowspan="0"extends the cell to the end of its row group. Browsers support it, but an explicit number is clearer and safer.- Browsers cap
colspanat 1000 androwspanat 65534. You will never need values near those limits. - Spans can be combined on one cell:
<td colspan="2" rowspan="2">covers a 2 by 2 block.
Exercise
A table has three columns. Row 1 has a th with rowspan="2" followed by two th cells. How many cells should row 2 contain, and why? What happens if you write three?
Show answer
Row 2 should contain two cells. The rowspan="2" cell from row 1 already fills the first slot of row 2, so only the remaining two slots need cells. A third cell has no slot left in the three-column grid, so the browser adds a fourth column to that row and the table no longer lines up.