Working with Colspans and Rowspans


Colspans and rowspans are the bane of layout tables, because they cause all sorts of strange breakage problems. These attributes come in handy for data tables, though, because that's what the people who dreamed up HTML designed colspans and rowspans for.

Technically speaking, the colspan attribute determines how many columns a particular table cell spans or straddles, while the rowspan attribute determines the number of spanned rows. The best way to understand how colspans and rowspans work is to look at an example, which Figure 56.2 provides.

Figure 56.2. This data table uses the colspan and rowspan attributes.


GEEKSPEAK

The colspan attribute determines how many columns a table cell straddles, while the rowspan attribute determines the number of straddled rows.


Listing 56.2. View Source for Figure 56.2.

[View full width]

 <table border="1">   <caption>Table 2. Color Transformations in Table Borders after Exposure to the Kenneth  Frequency</caption>   <colgroup span="2" style="width: 100px;"></colgroup>   <colgroup span="2" style="width: 150px; text-align: center;"></colgroup> <!-- The head element contains two table rows here. A few of the cells contain nonbreaking  spaces (&nbsp;). Use these to fill otherwise empty table cells. The last cell in the first row contains the colspan. As you can see in Figure 56.2, the  Border Color cell straddles two columns: the Before column and the After column. -->   <thead>     <tr>       <th>&nbsp;</th>       <th>&nbsp;</th>       <th colspan="2">Border Color</th>     </tr>     <tr>       <th>&nbsp;</th>       <th>x</th>       <th>Before</th>       <th>After</th>     </tr>   </thead>   <tfoot>     <tr>       <th>&nbsp;</th>       <th>x</th>       <th>Before</th>       <th>After</th>     </tr>   </tfoot> <!-- This table has two body elements. The first table cell in the first row contains the  rowspan. Looking at the figure, you see that the Phase 1 cell straddles the 1, 2, 3, and 4  rows. -->   <tbody>     <tr>       <th rowspan="4">Phase 1</th>       <th>1</th>       <td>#000000</td>       <td>#000066</td>     </tr>     <tr>       <th>2</th>       <td>#000033</td>       <td>#FFFF33</td>     </tr>     <tr>       <th>3</th>       <td>#000066</td>       <td>#CC6699</td>     </tr>     <tr>       <th>4</th>       <td>#000099</td>       <td>#3333FF</td>     </tr>   </tbody>   <!-- Here is the second body element. Again, the rowspan in the Phase 2 cell causes this  cell to straddle four rows. -->   <tbody>     <tr>       <th rowspan="4">Phase 2</th>       <th>5</th>       <td>#FFFFFF</td>       <td>#000000</td>     </tr>     <tr>       <th>6</th>       <td>#FFFF33</td>       <td>#000000</td>     </tr>     <tr>       <th>7</th>       <td>#FFFF66</td>       <td>#000000</td>     </tr>     <tr>       <th>8</th>       <td>#FFFF99</td>       <td>#663399</td>     </tr>   </tbody> </table> 

As you can see from the Listing 56.2, the colspan and rowspan attributes appear in the tag for the table cell, either th or td, depending on whether you need a header cell or a regular cell to span. The values of these attributes determine the number of columns or rows to span.

You'll notice that a row that includes a colspanned or rowspanned cell seems to be missing some markup. For example, in the first row of the data table in Figure 56.2, you find three table cells:

 <tr>   <th>&nbsp;</th>   <th>&nbsp;</th>   <th colspan="2">Border Color</th> </tr> 

TIP

A single table cell can contain both colspan and rowspan attributes.


The very next row has four table cells:

 <tr>   <th>&nbsp;</th>   <th>x</th>   <th>Before</th>   <th>After</th> </tr> 

The missing code in the first row is perfectly normal. In fact, this is the way it needs to be. The table cell with the colspan of 2 counts as two cells, not one, in which case both rows have four table cells after all.

Another point of note: The colgroup tags at the top of the table show the following structure:

 <colgroup span="2"></colgroup> <colgroup span="2"></colgroup> 

The spans of the colgroup tags add up to four columns, not three, even though there seem to be three columns in the very first row of the table. Once again, you have to count the cell with the colspan of 2 as two columns, not one, so the math does add up. The colgroup tags describe four table columns, and there are indeed four columns in that section of the table.



Web Design Garage
Web Design Garage
ISBN: 0131481991
EAN: 2147483647
Year: 2006
Pages: 202
Authors: Marc Campbell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net