Lesson 18 of 36
HTML Tutorial

colspan and rowspan

Merge cells across columns and rows, and keep the grid consistent while you do it.

3 min readBeginnerWeb FundamentalsHTML Tables

Sometimes one cell needs to stretch across several columns or rows, such as a group heading ("Weekend") above two day columns. HTML provides two attributes for that: colspan and rowspan.

colspan: stretch across columns#

colspan="2" makes a cell occupy two columns.

html
<table>
  <caption>Brew and Byte opening hours</caption>
  <tr>
    <th scope="col">Day</th>
    <th scope="col" colspan="2">Hours</th>
  </tr>
  <tr>
    <th scope="row">Weekdays</th>
    <td>Opens 7am</td>
    <td>Closes 7pm</td>
  </tr>
  <tr>
    <th scope="row">Weekends</th>
    <td>Opens 8am</td>
    <td>Closes 9pm</td>
  </tr>
</table>

The header "Hours" spans the two right-hand columns. Count the row widths: the first row has 1 + 2 = 3 columns, and each later row has 1 + 1 + 1 = 3 cells. The totals must match.

rowspan: stretch across rows#

rowspan="2" makes a cell occupy two rows. The cells it covers are omitted from the following rows.

html
<table>
  <caption>Saturday workshops</caption>
  <tr>
    <th scope="col">Time</th>
    <th scope="col">Workshop</th>
    <th scope="col">Room</th>
  </tr>
  <tr>
    <td rowspan="2">Morning</td>
    <td>Latte art</td>
    <td>Upstairs</td>
  </tr>
  <tr>
    <!-- no "Time" cell here: it is covered by the rowspan above -->
    <td>Home roasting</td>
    <td>Garden</td>
  </tr>
</table>

Because "Morning" covers two rows, the second row starts with the "Workshop" cell. The browser slots it into the second column automatically.

Combining both#

A cell can have both attributes, such as colspan="2" rowspan="2" for a 2 by 2 block. Complex combinations quickly become difficult to maintain, so sketch the grid on paper first and number the rows and columns.

How to debug a broken grid#

If cells look misaligned:

  1. Count the columns in the widest row.
  2. For every other row, add up the cells plus any colspan values and subtract columns covered by earlier rowspan cells.
  3. Make sure each row ends up at the same count.

The browser developer tools can outline table cells, which makes counting easier.

Accessibility notes#

Merged cells make life harder for screen reader users, who navigate cell by cell. Wherever you can, prefer a simpler design:

  • Use a spanned header cell only when it clearly groups columns, and mark its scope as colgroup when appropriate.
  • Avoid merging cells in the body of a table, as it breaks the one-to-one mapping between headers and cells.
  • Consider two small tables instead of one complex table.

Common mistakes#

  • Forgetting to remove the covered cells in later rows when using rowspan.
  • Using colspan bigger than the number of columns.
  • Using merged cells just to centre a title. Use caption instead.
  • Writing colspan=2 unquoted or with text like "2 columns". The value must be a number.
  • Building giant tables with many spans that nobody can maintain.

Try it yourself#

Create a timetable with three columns: Time, Monday and Tuesday. Make a "Lunch break" row whose single cell spans both day columns. Add a "Morning" cell that spans two rows. After you finish, count cells in each row and verify the total matches in every row.

Recap#

colspan makes a cell cover several columns, rowspan covers several rows, and covered cells must be left out. Check the totals per row, keep tables as simple as possible, and prefer caption for table titles.

Check your understanding

Lesson quiz

Optional

A short quiz to check what you just learned.

Questions
4
Time limit
5 min
Pass mark
70%
Best score
-