Logo

How to Merge Cells in HTML Tables?

How to Merge Cells in HTML Tables?

How to Merge Cells in HTML Tables?

Sometimes, you need to merge cells in your HTML tables to better display your data. HTML provides the colspan and rowspan attributes to achieve this. In this guide, we’ll show you how to use these attributes to create more complex table structures.

Using colspan and rowspan :

The colspan attribute allows a cell to span multiple columns, while the rowspan attribute allows a cell to span multiple rows. Here’s an example:

				
					<!DOCTYPE html>
<html>
<head>
    <title>HTML Table with Colspan and Rowspan</title>
</head>
<body>
    <h1>Table with Merged Cells</h1>
    <table border="1">
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
            <th>Header 3</th>
        </tr>
        <tr>
            <td rowspan="2">Rowspan 2</td>
            <td>Data 1</td>
            <td>Data 2</td>
        </tr>
        <tr>
            <td colspan="2">Colspan 2</td>
        </tr>
    </table>
</body>
</html>

				
			

Explanation of colspan and rowspan :

  • rowspan: This attribute allows a cell to span multiple rows. In the example, rowspan="2" makes the first cell in the second row span across two rows.
  • colspan: This attribute allows a cell to span multiple columns. In the example, colspan="2" makes the cell in the third row span across two columns.

Practical Use Cases :

  1. Merging Headers: Use colspan to merge header cells for categories that span multiple columns.
  2. Creating Subsections: Use rowspan to create subsections within your table, making it easier to understand complex data.

Conclusion :

Using colspan and rowspan in your HTML tables allows you to create more dynamic and flexible table layouts. Experiment with these attributes to see how they can enhance your data presentation.

Scroll to Top