Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 122 ยท Tables

Spanning Rows and Columns

Merge table cells with colspan and rowspan. Learn to plan the grid on paper first, count cells correctly, and avoid the overlapping cells that break table layouts.

Real tables are not always a perfect grid of equal cells. A heading may cover two columns, or one value may apply to several rows. Spreadsheet programs call this merging cells. In HTML you do it with two attributes on <td> and <th>: colspan and rowspan.

They are easy to write and surprisingly easy to get wrong. This lesson shows how they work and gives you a reliable way to plan a table so the cells always line up.

colspan: one cell, several columns

colspan makes a cell stretch to the right across more than one column. Its value is the number of columns the cell covers.

A heading that spans columns

The table has five columns. In the first header row, "Saturday" and "Sunday" each cover two of them, so that row needs only three cells (City + Saturday + Sunday), yet it still fills all five columns.

That is the golden rule of spanning: a spanning cell replaces the cells it covers. If you add colspan="2" to a cell, remove one cell from that row.

rowspan: one cell, several rows

rowspan makes a cell stretch downward. In the weather table above, "City" has rowspan="2", so it fills its own row and the row below it. That's why the second header row starts with "High" and has only four cells: the first column is already taken.

Here is rowspan doing heavier work in a nutrition table, where one food group covers several foods:

Grouping rows with rowspan

Only the first row of each group contains the spanning cell. The rows below leave it out, because that slot is already filled from above. Try changing rowspan="3" to rowspan="2" and watch "Tofu" slide left into the wrong column. That's the classic spanning bug.

A row span stops at the end of its row group. A cell in one <tbody> can never reach down into the next <tbody> or the <tfoot>. That's why each food group above sits in its own <tbody>.

Combining both

A cell may use colspan and rowspan at the same time and cover a rectangle of slots. In a schedule, a two-hour workshop that runs on two days could take a 2ร—2 block:

HTML
<td colspan="2" rowspan="2">Pottery workshop</td>

Use this sparingly. Every merged block makes the table harder to read, harder to maintain and harder for screen-reader users to navigate.

Plan the grid before you write it

Spanning goes wrong when you write rows without thinking about the whole grid. Use this method instead:

  1. Sketch the finished table on paper or in a spreadsheet, with the merged cells drawn as merged.
  2. Count the columns of the full grid. Every row must fill exactly that many slots.
  3. Write the rows top to bottom. For each row, list the cells left to right, but skip any slot already filled by a rowspan from above.
  4. Check every row by adding up: (number of cells) + (extra columns from colspan) + (slots filled from above) = total columns.

Let's check row 2 of the weather table: 4 cells + 0 extra from colspan + 1 slot filled by "City" from above = 5. Correct.

Find and fix the bug

The Wednesday row has three cells, but its "Closes" slot is already filled by Tuesday's rowspan="2". The extra cell sticks out as a fourth column. Delete it and the table snaps into shape.

The attribute values in detail

  • Both attributes take a positive whole number. The default is 1, so colspan="1" does nothing.
  • colspan can go up to 1000 and rowspan up to 65534. Real tables never come close.
  • The spec also allows rowspan="0", meaning "span to the end of the row group". It is rarely used, and an explicit number is clearer to anyone reading your code.
  • Spans that run past the edge of the table don't throw errors. The browser just makes the table bigger or clips the span, so you won't be warned about mistakes. Inspect your tables carefully.

When not to merge cells

Merged cells are great for headers that group columns or rows. They are less great inside the data:

  • Don't merge just to avoid repetition. If three products all cost $5, writing "$5" three times keeps each row complete and readable on its own.
  • Avoid deep nesting of spans. If your table needs headers that span in three or four levels, consider splitting it into several smaller tables.
  • Think about narrow screens and sorting. Tables with merged data cells are hard to sort, filter or rearrange into cards on mobile.

A useful test: imagine reading the table one cell at a time, out loud, without seeing the grid. If a merged data cell makes it unclear which row or column a value belongs to, unmerge it and repeat the value.

For complex headers with spans, assistive technology may need extra help to work out which headers apply to which cells. You'll learn the headers attribute for that in Accessible Tables.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A table has 4 columns. One row has a cell with colspan="3". How many other cells does that row need?

    The spanning cell fills 3 of the 4 column slots, so the row needs exactly one more cell.
  2. 2. A cell has rowspan="2". What must you do in the row below it?

    The spanning cell occupies a slot in the next row too, so that row has one fewer cell. Adding one pushes the rest of the row out of line.
  3. 3. Which direction does colspan stretch a cell?

    colspan stretches a cell across columns, toward the end of the row. rowspan stretches it down.
  4. 4. Can a rowspan cell in one <tbody> reach into the next <tbody>?

    Row groups are separate blocks of rows. Put rows that belong together in the same <tbody>.

Summary

  • colspan="n" stretches a cell across n columns; rowspan="n" stretches it down across n rows.
  • A spanning cell replaces the cells it covers: remove them from the code.
  • Rows below a rowspan must skip the slot it fills.
  • Plan the full grid first, then check that every row adds up to the same number of columns.
  • Row spans stop at the end of their row group.
  • Merge cells for grouping headers; keep data cells simple and unmerged where possible.

Next lesson: Column Groups.