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

Imagine a timetable where both rooms take the same lunch break. Repeating Break is possible, but one cell across both room columns can express the shared event. Colspan tells a cell how many column positions it occupies.

Now imagine several sessions belong to Monday. Rowspan lets one day cell occupy positions down more than one row. These attributes work on both th and td. They do not merge separate HTML elements after the page loads; you author one cell with a larger span.

Colspan counts occupied columns

HTML fragment — part of a document
<tr>
  <th scope="row">12 noon</th>
  <td colspan="2">Shared break</td>
</tr>

This row covers three column positions: one for the time and two for the shared break. It contains only two actual cell elements. Do not add another empty td for the second half of the break; that position is already occupied.

Colspan defaults to 1. Write a positive integer such as 2, not 2px, two or a percentage. Colspan="0" does not mean all remaining columns. Count the actual intended positions.

Count occupied positions
  1. 1Sketch the intended column grid
  2. 2Mark cells spanning from above
  3. 3Place new cells in free positions
  4. 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

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>
Browser previewDocument title: Two Rooms and a Shared Break

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.

Expected result: the break text occupies the area under both room headings. Without borders the merge may be less visually obvious, but the source and cell relationships still matter. Count three effective positions in every row rather than insisting on three cell elements in every row.

Rowspan counts downwards

For rowspan="2", a cell occupies its position in the current row and the next row. In the next row, omit the cell already covered from above. Start with the next free position.

Draw a small pencil grid before writing complicated spans. Mark every position occupied by a cell from an earlier row. Then place the current row's new cells in the remaining positions. This prevents accidental shifts and overlap.

Complete program: day-sessions.html

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>
Browser previewDocument title: Sessions Grouped by Practice Day

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.

The Monday cell occupies the Day column in two rows. The second Monday row therefore begins with its Topic cell, Paragraphs. Adding another Day cell there would add a position that the span already covers.

Tuesday demonstrates a special valid value: rowspan="0" extends to the end of its row group. Here that means the two rows in Tuesday's tbody. If another session row is added to that same group, the Tuesday cell continues to cover it. This special zero rule is for rowspan, not colspan.

A span must respect group boundaries

Do not make a cell span from one tbody into another, or from thead into tbody. Keep each downward span within its row group. The separate Tuesday tbody limits its rowspan="0"; the span does not swallow every remaining row of the table.

Use ordinary positive values for most beginner work. Reach for zero only when extending to the group's end is your actual intention. Clear explicit structure is easier to review than clever-looking markup.

Scope still needs thought

In day-sessions.html, scope="rowgroup" says the day header labels the rows in its actual tbody group. Scope="row" on a topic labels that topic's row. A rowspan by itself changes occupied positions; it does not fully explain which relationships you intend.

A header spanning several columns may need scope="colgroup" together with a real column group. Chapter 28 introduces those column groups, and Chapter 29 covers explicit header references for less regular tables. Do not assume that making a large bold box is enough to explain complex data to every reader.

When not to merge

A simple record list is often easier to scan, copy and maintain when each row repeats its day. Merging is useful only when the shared relationship becomes clearer. If a table is turning into a puzzle, simplify it or split it into smaller tables with clear captions.

Also distinguish a blank value from a shared value. A merged “Not recorded” cell across unrelated columns may hide which specific information is missing. Use separate cells when each column needs its own clear state.

Problem and solution: the last room appears in a new column

Problem: after a day cell receives rowspan="2", the next row still contains a duplicate day cell. The browser finds the first position occupied and places the extra cells farther across.

Solution: remove the duplicate covered cell. Redraw the grid, mark the downward span and count occupied positions. Do not fix the symptom by shrinking text or hiding the last cell with CSS.

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

  1. What does colspan="2" do?
  2. How many cell elements appear in the shared-break row, and how many column positions do they occupy?
  3. What must be omitted from a row when a cell above already spans into that position?
  4. How does rowspan="0" differ from colspan="0"?
  5. Can a cell legitimately span from one tbody into a different tbody?
  6. 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
  1. One cell occupies two column positions.
  2. Two elements occupy three positions: one time header and one two-column break cell.
  3. Omit the duplicate cell for the already occupied position.
  4. Rowspan zero extends to the end of its row group. Colspan zero is not an equivalent all-columns rule; use a positive intended count.
  5. No. Keep downward spans within their row group.
  6. 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?

A spanning cell can occupy several grid positions. Correctness depends on the effective grid and its relationships, not simply on every tr having the same number of child elements.

Does rowspan automatically supply row-group header meaning?

No. Rowspan describes occupied positions. Suitable th scope and actual row grouping describe header relationships. Choose both deliberately.

Why are complex merged tables harder to maintain?

An edit can affect cells in several rows or columns. It becomes easier to create overlaps, shift data under the wrong header or leave unclear associations. A simpler design often reduces these risks.

How would you debug a shifted spanning table?

Sketch its grid, mark all positions occupied by spans, check group boundaries and place each remaining cell in order. Then compare every value with its intended headers.

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.