Course contentsShow
← Web Development
Lesson 115 of 2,9822. HTML FundamentalsFree lesson

Spanning Cells: colspan and rowspan

Merge cells horizontally or vertically to create complex layouts.

Spanning Cells: colspan and rowspan

What you'll learn: How to make table cells stretch across multiple columns or rows to create more complex table layouts.

The Core Idea

By default, every <td> or <th> element occupies exactly one cell in your table grid. But sometimes you need a cell to span across multiple columns (like a wide header) or multiple rows (like a category label). HTML provides two attributes to do this: colspan for horizontal spanning and rowspan for vertical spanning.

Think of it like merging cells in a spreadsheet—you're combining adjacent cells into one larger cell.

How It Works

colspan tells a cell to stretch across multiple columns:

<table>
  <tr>
    <th colspan="3">Sales Report</th>
  </tr>
  <tr>
    <th>Product</th>
    <th>Price</th>
    <th>Quantity</th>
  </tr>
</table>

The first header spans all three columns, creating a unified title row.

rowspan tells a cell to stretch down through multiple rows:

<table>
  <tr>
    <th rowspan="2">Region</th>
    <th>Q1</th>
    <th>Q2</th>
  </tr>
  <tr>
    <td>$5000</td>
    <td>$6000</td>
  </tr>
</table>

The "Region" header occupies space in both rows, so the second row only needs two cells.

Remember the Count

When a cell spans multiple columns or rows, you need fewer cells in those affected rows or columns. If you span 3 columns, that row needs 2 fewer <td> elements. The browser automatically accounts for the space.

Key Takeaway: Use colspan to merge cells horizontally across columns and rowspan to merge cells vertically across rows, creating flexible table layouts beyond simple grids.