HTML / HTML Tables

Merging Table Cells with Colspan/Rowspan

This tutorial will teach you how to merge table cells using the colspan and rowspan attributes. These can be used to create more complex table structures or highlight certain data.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Covers the creation and styling of tables in HTML.

Merging Table Cells with Colspan/Rowspan

Introduction

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.

Step-by-Step Guide

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.

Using colspan

If you want a cell to span across two columns, you would write:

<td colspan="2">Content</td>

Using rowspan

If you want a cell to span across two rows, you would write:

<td rowspan="2">Content</td>

Code Examples

Example 1: Merging cells horizontally with colspan

<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.

Example 2: Merging cells vertically with rowspan

<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.

Summary

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

Practice Exercises

  1. Exercise 1: Create an HTML table with three rows and three columns. Use colspan to merge the first two cells of the first row.

  2. Exercise 2: Modify the table from exercise 1. Use rowspan to merge the first two cells of the first column.

  3. 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:

  1. Solution to Exercise 1:
<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>
  1. Solution to Exercise 2:
<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>
  1. Solution to Exercise 3:
<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!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

Image Converter

Convert between different image formats.

Use tool

CSV to JSON Converter

Convert CSV files to JSON format and vice versa.

Use tool

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help