The colspan and rowspan attributes in HTML tables allow cells to span multiple columns or rows, enabling complex layouts for grouping or summarizing data. These attributes are used within <th> or <td> tags to merge cells horizontally or vertically. This tutorial explains how to use colspan and rowspan with examples and previews, helping beginners create structured and visually appealing tables.
Colspan (column span) makes a cell occupy multiple columns, while rowspan (row span) makes a cell occupy multiple rows. These attributes are useful for creating headers that cover multiple columns, combining data, or organizing complex table layouts.
Let’s explore how to use colspan and rowspan in tables with examples and previews.
The colspan attribute merges cells across multiple columns. The value specifies how many columns the cell spans.
<table style="border: 1px solid black; border-collapse: collapse;">
<tr>
<th colspan="2" style="border: 1px solid black; padding: 8px;">Student Info</th>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Alice</td>
<td style="border: 1px solid black; padding: 8px;">25</td>
</tr>
</table>
Student Info | |
---|---|
Alice | 25 |
The rowspan attribute merges cells across multiple rows. The value specifies how many rows the cell spans.
<table style="border: 1px solid black; border-collapse: collapse;">
<tr>
<th style="border: 1px solid black; padding: 8px;">Category</th>
<th style="border: 1px solid black; padding: 8px;">Item</th>
</tr>
<tr>
<td rowspan="2" style="border: 1px solid black; padding: 8px;">Fruit</td>
<td style="border: 1px solid black; padding: 8px;">Apple</td>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Banana</td>
</tr>
</table>
Category | Item |
---|---|
Fruit | Apple |
Banana |
You can use both attributes together for complex table layouts.
<table style="border: 1px solid black; border-collapse: collapse;">
<tr>
<th colspan="2" style="border: 1px solid black; padding: 8px;">Employee Details</th>
</tr>
<tr>
<td rowspan="2" style="border: 1px solid black; padding: 8px;">Bob</td>
<td style="border: 1px solid black; padding: 8px;">Developer</td>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Designer</td>
</tr>
</table>
Employee Details | |
---|---|
Bob | Developer |
Designer |
Combine colspan, rowspan, and a <caption> for a complete table.
<table style="border: 1px solid black; border-collapse: collapse;">
<caption style="padding: 8px;">Weekly Schedule</caption>
<tr>
<th colspan="3" style="border: 1px solid black; padding: 8px;">Tasks</th>
</tr>
<tr>
<td rowspan="2" style="border: 1px solid black; padding: 8px;">Monday</td>
<td style="border: 1px solid black; padding: 8px;">9 AM</td>
<td style="border: 1px solid black; padding: 8px;">Meeting</td>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">10 AM</td>
<td style="border: 1px solid black; padding: 8px;">Coding</td>
</tr>
</table>
Tasks | ||
---|---|---|
Monday | 9 AM | Meeting |
10 AM | Coding |
Using colspan and rowspan requires careful planning:
colspan="2"
).
<td colspan="2">Spans two columns</td>
rowspan="3"
).
<td rowspan="3">Spans three rows</td>
To make your use of colspan and rowspan effective:
background-color
) for visual clarity.Beginners often make these errors:
background-color
or text-align: center
) to make merged cells stand out.
Create a simple HTML file and experiment with colspan and rowspan. Try combining them in a table layout.
<!DOCTYPE html>
<html lang="en">
<body>
<h3>Course Schedule</h3>
<table style="border: 1px solid navy; border-collapse: collapse;">
<caption style="padding: 8px;">Weekly Classes</caption>
<tr>
<th colspan="2" style="border: 1px solid navy; padding: 8px; background-color: #e6f0ff;">Class Details</th>
</tr>
<tr>
<td rowspan="2" style="border: 1px solid navy; padding: 8px;">Monday</td>
<td style="border: 1px solid navy; padding: 8px;">Math</td>
</tr>
<tr>
<td style="border: 1px solid navy; padding: 8px;">Science</td>
</tr>
</table>
</body>
</html>
Class Details | |
---|---|
Monday | Math |
Science |
By mastering colspan and rowspan, you can create complex, well-organized tables that enhance your website’s data presentation. Practice regularly to get comfortable!