What does the rowspan attribute do?
The rowspan attribute in HTML is used on <td> and <th> tags to merge cells vertically, meaning it lets a single cell span several table rows at once.
Main purpose
rowspan defines how many rows down a cell will stretch.
While a regular cell occupies one row, with rowspan it can merge two, three, or more.
Example:
html
<table border="1">
<tr>
<th>Name</th>
<th>City</th>
</tr>
<tr>
<td rowspan="2">Maria</td>
<td>Warsaw</td>
</tr>
<tr>
<td>Berlin</td>
</tr>
</table>Here the cell with the name "Maria" spans two rows and sits to the left of two different cities.
Explanation:
- The
rowspan="2"attribute tells the browser: this cell should occupy two consecutive rows. - Likewise,
rowspan="3"spans three rows, and so on.
Example in a summary table:
html
<table border="1">
<tr>
<th rowspan="2">Category</th>
<th colspan="2">Metrics</th>
</tr>
<tr>
<th>2024</th>
<th>2025</th>
</tr>
<tr>
<td>Income</td>
<td>1.2M</td>
<td>1.5M</td>
</tr>
</table>Here rowspan="2" merges the "Category" cell vertically, while colspan="2" merges the headers horizontally.
Important:
- The
rowspanvalue is a positive integer (for example,rowspan="3"). - The default is
1(the cell is not merged). - Setting too large a value can break the table's structure.
Difference between colspan and rowspan:
| Attribute | Merges | Direction |
|---|---|---|
colspan | Columns | Horizontal |
rowspan | Rows | Vertical |
Conclusion:
The rowspan attribute merges cells vertically, letting a single cell span several rows.
It is used to build more complex tables with data hierarchies, totals, and groupings.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.