Login for Software or Support

CoffeeCup - HTML Editor, Flash & Web Design Software

Over 49,068,676 Downloads in 87 Countries

Tell a Friend About Our Cool Software

Buy the Book Here !

Chapter 4: Merging Cells


The tables I have presented so far have a standard structure in which each row has the same number of cells as any other row, and each column has the same number of cells as any other column. When you need more flexibility in your table layout you can merge cells, combining two or more adjacent cells into a single cell. The resulting merged cell can span two or more original cells horizontally, vertically, or both.

You create merged cells by using either or both of the following two attributes in a <td> tag:

  • colspan: set to the number of cells to be spanned horizontally.
  • rowspan: set to the number of cells to be spanned vertically.
Here's a simple example. This HTML code creates a table with a basic structure of four rows and two columns. By inserting the attribute rowspan="4" into the first <td> tag in the first row, the top left cell in the table is merged over four rows--its own row and the three below it. The resulting table is shown in Figure 4-6

<table border="1" cellpadding="5" width="100%">
<tr>
<td width="50%" rowspan="4">Merged cell</td>
<td width="50%">Original cell</td>
</tr>
<tr>
<td width="50%">Original cell</td>
</tr>
<tr>
<td width="50%">Original cell</td>
</tr>
<tr>
<td width="50%">Original cell</td>
</tr>
</table>

Figure 4-6. A table with a cell that is merged vertically over four rows.

When you look at the HTML code for this table, please note the following. The first <tr> tag contains two <td> tags, as you would expect. The other three <tr> tags, however, each contain only a single <td> tag. This is because the top left cell is merged down into these three rows, so they only need one additional cell to make up the overall two column structure of the table.

Next, let's look at an example of merging horizontally. The principle is the same but the details are slightly different. Here's the HTML code to define the table shown in Figure 4-7.

<table border="1" cellpadding="5" width="90%"> <tr> <td width="33%">Original cell</td> <td width="33%">Original cell</td> <td width="34%">Original cell</td> </tr>
<tr> <td width="33%">Original cell</td> <td width="67%" colspan="2">Merged cell</td> </tr> <tr> <td width="33%">Original cell</td> <td width="33%">Original cell</td> <td width="34%">Original cell</td> </tr>
</table>

Merged Cells

Figure 4-7. A table with a cell that is merged horizontally over two
columns.

Finally, we'll look at a table that contains a cell that is merged both vertically and horizontally. This requires including both the colspan and rowspan attributes in the <td> tag of the merged cell. Here's the HTML; the table is shown in Figure 4-8.

<table border="1" cellpadding="5" width="90%"> <tr> <td width="25%">Original cell</td> <td width="25%">Original cell</td> <td width="25%">Original cell</td> <td width="25%">Original cell</td> </tr>
<tr> <td width="25%">Original cell</td> <td width="50%" colspan="2" rowspan="2">Merged cell</td> <td width="25%">Original cell</td> </tr> <tr> <td width="25%">Original cell</td> <td width="25%">Original cell</td> </tr> <tr> <td width="25%">Original cell</td> <td width="25%">Original cell</td> <td width="25%">Original cell</td> <td width="25%">Original cell</td> </tr>
</table>

Merged Cells
Figure 4-8. Merging a cell both vertically and horizontally.

When creating merged cells, the most important thing to remember is that you must reduce the number of cells--the number of <td> tags--in the rows and columns that the merged cell is being merged into. While browsers are usually pretty forgiving in dealing with less-than-perfect code, you may get unpredictable results if you ignore this warning.


Table of Contents
Download Our Software:

... and don't forget about our Free Software

CoffeeCup Home Page | Software | Copyright & Legal | Site Map | © 1996 - 2008 CoffeeCup Software, Inc.