www.gibmonks.com

Main Page

Previous Page
Next Page

[Page 1346 (continued)]

J.11. Intermediate XHTML Tables and Formatting

In the previous section, we explored the structure of a basic table. In Fig. J.11, we enhance our discussion of tables by introducing elements and attributes that allow the document author to build more complex tables.

Figure J.11. Complex XHTML table.
(This item is displayed on pages 1346 - 1348 in the print version)

 1  <?xml version = "1.0"?>
 2  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 3     "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 4
 5  <!-- Fig. J.11: table2.html      -->
 6  <!-- Intermediate table design. -->
 7
 8  <html xmlns = "http://www.w3.org/1999/xhtml">
 9     <head>
10        <title>Intermediate table design</title>
11     </head>
12
13     <body>
14
15        <h1>Table Example Page</h1>
16
17        <table border = "1">
18           <caption>Here is a more complex sample table.</caption>
19
20           <!-- <colgroup> and <col> tags are -->
21           <!-- used to format entire columns -->
22           <colgroup>
23
24              <!-- span attribute determines how  -->
25              <!-- many columns <col> tag affects -->
26              <col align = "right" span = "1" />
27           </colgroup>
28
29           <thead>
30
31              <!-- rowspans and colspans merge specified    -->
32              <!-- number of cells vertically or horizontally  -->
33              <tr>
34
35                 <!-- merge two rows -->
36                 <th rowspan = "2">
37                    <img src = "camel.gif" width = "205"
38                       height = "167" alt = "Picture of a camel" />
39                 </th>
40
41                 <!-- merge four columns -->
42                 <th colspan = "4" valign = "top">
43                    <h1>Camelid comparison</h1><br />
44                    <p>Approximate as of 9/2002</p>
45                 </th>
46              </tr>
47
48              <tr valign = "bottom">
49                 <th># of Humps</th>
50                 <th>Indigenous region</th>
51                 <th>Spits?</th>
52                 <th>Produces Wool?</th>
53              </tr>
54
55           </thead>
56
57           <tbody>
58
59              <tr>
60                 <th>Camels (bactrian)</th>
61                 <td>2</td>
62                 <td>Africa/Asia</td>
63                 <td rowspan = "2">Llama</td>
64                 <td rowspan = "2">Llama</td>
65              </tr>
66
67              <tr>
68                 <th>Llamas</th>
69                 <td>1</td>
70                 <td>Andes Mountains</td>
71              </tr>
72
73           </tbody>
74
75        </table>
76
77     </body>
78  </html>

Common Programming Error J.5

When using colspan and rowspan to adjust the size of table data cells, keep in mind that the modified cells will occupy more than one column or row; other rows or columns of the table must compensate for the extra rows or columns spanned by individual cells. If you do not, the formatting of your table will be distorted, and you could inadvertently create more columns and rows than you originally intended.


The table begins at line 17. Element colgroup (lines 2227) groups and formats columns. The col element (line 26) specifies two attributes in this example. The align attribute determines the alignment of text in the column. The span attribute determines how many columns the col element formats. In this case, we set align's value to "right" and span's value to "1" to right-align text in the first column (the column containing the picture of the camel in the sample screen capture).


[Page 1348]

Table cells are sized to fit the data they contain. Document authors can create larger data cells by using attributes rowspan and colspan. The values assigned to these attributes specify the number of rows or columns occupied by a cell. The th element at lines 3639 uses the attribute rowspan ="2" to allow the cell containing the picture of the camel to use two vertically adjacent cells (thus the cell spans two rows). The th element at lines 4245 uses the attribute colspan ="4" to widen the header cell (containing Camelid comparison and Approximate as of 9/2002) to span four cells.

Line 42 introduces attribute valign, which aligns data vertically and may be assigned one of four values"top" aligns data with the top of the cell, "middle" vertically centers data (the default for all data and header cells), "bottom" aligns data with the bottom of the cell and "baseline" ignores the fonts used for the row data and sets the bottom of all text in the row on a common baseline (i.e., the horizontal line to which each character in a word is aligned).


Previous Page
Next Page