CHAPTER 27 · HTML
Merged cells: understand colspan and rowspan without losing the grid
Sometimes one piece of information belongs across several columns or rows. Learn how spanning cells occupy table positions, why extra placeholder cells cause trouble, and when repeating a value is easier to understand.
Download example files (ZIP)One label can belong to more than one position
Colspan counts occupied columns
<tr>
<th scope="row">12 noon</th>
<td colspan="2">Shared break</td>
</tr>- 1Sketch the intended column grid
- 2Mark cells spanning from above
- 3Place new cells in free positions
- 4Check every row and group boundary
A covered position needs no duplicate cell. Rowspan zero stops at the end of its row group.
Complete program: shared-break.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Two Rooms and a Shared Break</title>
</head>
<body>
<h1>Read the shared workshop break</h1>
<table>
<caption>Workshop activities in two rooms</caption>
<thead>
<tr><th scope="col">Time</th><th scope="col">Room 6</th><th scope="col">Room 8</th></tr>
</thead>
<tbody>
<tr><th scope="row">11 am</th><td>Links</td><td>Lists</td></tr>
<tr><th scope="row">12 noon</th><td colspan="2">Shared break for both rooms</td></tr>
<tr><th scope="row">1 pm</th><td>Review</td><td>Practice</td></tr>
</tbody>
</table>
<p>The break belongs to both room columns.</p>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Rowspan counts downwards
Complete program: day-sessions.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sessions Grouped by Practice Day</title>
</head>
<body>
<h1>Follow the day and session together</h1>
<table>
<caption>Two sessions on each practice day</caption>
<thead>
<tr><th scope="col">Day</th><th scope="col">Topic</th><th scope="col">Room</th></tr>
</thead>
<tbody>
<tr><th rowspan="2" scope="rowgroup">Monday</th><th scope="row">Headings</th><td>Room 6</td></tr>
<tr><th scope="row">Paragraphs</th><td>Room 8</td></tr>
</tbody>
<tbody>
<tr><th rowspan="0" scope="rowgroup">Tuesday</th><th scope="row">Links</th><td>Room 6</td></tr>
<tr><th scope="row">Lists</th><td>Room 8</td></tr>
</tbody>
</table>
<p>Each day label belongs to the sessions in its own body group.</p>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
A span must respect group boundaries
Scope still needs thought
When not to merge
Problem and solution: the last room appears in a new column
Common mistakes and useful tips
Adding placeholder cells for positions already covered by a span. Counting only cell elements instead of effective grid positions. Writing colspan="0" expecting a span to the table's end. Letting a rowspan cross a row-group boundary. Using merges only for decoration. Treating scope and span as interchangeable attributes. Forgetting to retest the grid after inserting or deleting a row.
Practice questions
What does colspan="2" do? How many cell elements appear in the shared-break row, and how many column positions do they occupy? What must be omitted from a row when a cell above already spans into that position? How does rowspan="0" differ from colspan="0"? Can a cell legitimately span from one tbody into a different tbody? Add a third Tuesday session to day-sessions.html. Explain why the Tuesday label still covers it, and draw the resulting grid before checking the browser.
Check your practice answers
One cell occupies two column positions. Two elements occupy three positions: one time header and one two-column break cell. Omit the duplicate cell for the already occupied position. Rowspan zero extends to the end of its row group. Colspan zero is not an equivalent all-columns rule; use a positive intended count. No. Keep downward spans within their row group. Put the new row inside Tuesday's existing tbody with Topic and Room cells only. The Day position is covered by rowspan="0". Do not add a second Tuesday cell.
Interview questions
Why can rows with different cell counts still be correct?
Does rowspan automatically supply row-group header meaning?
Why are complex merged tables harder to maintain?
How would you debug a shifted spanning table?
Quick revision
Colspan extends across; rowspan extends down. Covered positions do not need duplicate cells. Rowspan zero means the rest of its row group. Keep the grid valid and simplify merges when they obscure meaning.