The colspan attribute makes a cell stretch across several columns, and rowspan makes a cell stretch down several rows. You put them on a <td> or <th>, and the browser merges the cell with its neighbours. The html colspan rowspan pair is how you build grouped headings, subtotal rows and calendar-style layouts.
The value is a positive whole number that counts how many grid slots the cell covers, including its own. Because a merged cell takes the place of other cells, you must leave those cells out of your markup, or the table grows extra columns.
Set colspan="2" and the cell covers two columns. The row then needs two fewer <td> or <th> elements, because the merged cell already fills those slots.
The grid has three columns. The heading "Contact" spans columns two and three, so the header row has only two cells while each body row has three.
Set rowspan="2" and the cell covers its own row and the one below. In the next row you write one cell fewer, because the slot is taken.
The first row has three cells and the second has two, but both rows fill three columns. "Monday" sits at the left edge and spans both lines.
A single cell can use both attributes to cover a block. The example below builds a header with a corner cell and a two-level heading.
The empty corner cell covers two header rows. Each year covers two quarter columns. The grid is five columns wide, and every row accounts for exactly five slots.
The most common bug is a wrong cell count. Follow this procedure for each row:
rowspan.colspan values.If a row is short, the browser leaves a gap. If it is too long, the table silently gains an extra column and the layout shifts. Check the result in DevTools, which can show the table grid.
| Rule | Detail |
|---|---|
| Valid values | Positive integers. colspan="0" is treated as 1 in current browsers |
| Maximum colspan | Browsers cap it at 1000 |
| Maximum rowspan | Capped at 65534; rowspan="0" means "to the end of the row group" |
| Overlap | Two cells must never claim the same slot; this is a table model error |
| Row groups | A rowspan cannot cross from <thead> into <tbody>; it stops at the end of its group |
The overlap rule matters. If a rowspan cell reaches into a row that also has a cell in that column, the table is invalid and browsers render it unpredictably.
Merged cells make tables harder for screen readers, so keep them for real structure, not for decoration. For merged headings, give the header cell scope="colgroup" or scope="rowgroup" as shown above, so assistive technology knows it covers a set of columns or rows. For very complex layouts with irregular headers, the headers attribute on each data cell can name the header cells by id, though a simpler table split into two is usually better.
CSS works as usual. Centre a spanning heading and add a border so the grouping is visible.
The attribute selectors [colspan] and [rowspan] match any cell that merges, which avoids adding extra classes.
colspan merges cells horizontally across columns, and rowspan merges them vertically across rows. Both are set on <td> or <th>.
The row probably has too many cells. Remove one cell for every extra slot that the colspan now covers, and check the sum for each row.
Yes. The cell then covers a rectangle of columns and rows, as in the corner cell of a two-level header.
No. colspan belongs to the HTML table model. For page layout, use CSS grid with grid-column: span 2.
Use colspan to join cells across columns and rowspan to join them down rows. Remove the cells that the merged cell replaces, make every row add up to the same column count, and never let two cells overlap. Add scope="colgroup" or scope="rowgroup" on merged headings, and keep merging to the minimum so the table stays easy to read with a screen reader.