In this tutorial, we aim to understand how to merge table cells using the colspan
and rowspan
attributes in HTML. These attributes offer you the flexibility to build complex tables or to emphasize certain information in your table.
By the end of this tutorial, you will learn:
- What the colspan
and rowspan
attributes are
- How to use them to merge cells vertically and horizontally
Prerequisites: Basic knowledge of HTML and HTML tables.
The colspan
attribute is used to allow a cell to span across multiple columns while the rowspan
attribute allows a cell to span across multiple rows.
To use these attributes, you add them to the <td>
or <th>
tag and specify the number of rows or columns the cell should span.
If you want a cell to span across two columns, you would write:
<td colspan="2">Content</td>
If you want a cell to span across two rows, you would write:
<td rowspan="2">Content</td>
<table border="1">
<tr>
<td colspan="2">This cell spans two columns</td>
</tr>
<tr>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
</table>
In the above example, the first cell of the first row spans two columns due to the colspan="2"
attribute. The result is a table with the first row having one cell and the second row having two cells.
<table border="1">
<tr>
<td rowspan="2">This cell spans two rows</td>
<td>Cell 2</td>
</tr>
<tr>
<td>Cell 3</td>
</tr>
</table>
In the above example, the first cell of the first row spans two rows due to the rowspan="2"
attribute. The result is a table with the first column having one cell and the second column having two cells.
In this tutorial, we learned how to use the colspan
and rowspan
attributes in HTML to merge cells either horizontally or vertically.
Next, you could dive deeper into the world of HTML tables, exploring other attributes and styles to enhance your tables.
Additional resources:
- W3Schools: HTML Tables
- Mozilla Developer Network: HTML Tables
Exercise 1: Create an HTML table with three rows and three columns. Use colspan
to merge the first two cells of the first row.
Exercise 2: Modify the table from exercise 1. Use rowspan
to merge the first two cells of the first column.
Exercise 3: Create an HTML table with five rows and five columns. Use both colspan
and rowspan
to make the cell in the middle span to the two cells to its right and the two cells below it.
Solutions:
<table border="1">
<tr>
<td colspan="2">This cell spans two columns</td>
<td>Third Column</td>
</tr>
<tr>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
</tr>
<tr>
<td>Cell 5</td>
<td>Cell 6</td>
<td>Cell 7</td>
</tr>
</table>
<table border="1">
<tr>
<td rowspan="2">This cell spans two rows</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 4</td>
<td>Cell 5</td>
</tr>
<tr>
<td>Cell 6</td>
<td>Cell 7</td>
<td>Cell 8</td>
</tr>
</table>
<table border="1">
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
<td>Cell 5</td>
</tr>
<tr>
<td>Cell 6</td>
<td>Cell 7</td>
<td rowspan="2" colspan="2">This cell spans two columns and two rows</td>
<td>Cell 9</td>
</tr>
<tr>
<td>Cell 10</td>
<td>Cell 11</td>
<td>Cell 13</td>
</tr>
<tr>
<td>Cell 14</td>
<td>Cell 15</td>
<td>Cell 16</td>
<td>Cell 17</td>
<td>Cell 18</td>
</tr>
<tr>
<td>Cell 19</td>
<td>Cell 20</td>
<td>Cell 21</td>
<td>Cell 22</td>
<td>Cell 23</td>
</tr>
</table>
Keep practicing until you feel comfortable with the concepts. Happy coding!