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.
Section overview
5 resourcesCovers 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
-
Exercise 1: Create an HTML table with three rows and three columns. Use
colspanto merge the first two cells of the first row. -
Exercise 2: Modify the table from exercise 1. Use
rowspanto merge the first two cells of the first column. -
Exercise 3: Create an HTML table with five rows and five columns. Use both
colspanandrowspanto make the cell in the middle span to the two cells to its right and the two cells below it.
Solutions:
- 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>
- 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>
- 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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Random Password Generator
Create secure, complex passwords with custom length and character options.
Use toolLatest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article