Section G.3. Intermediate XHTML Tables and Formatting


G.3. Intermediate XHTML Tables and Formatting

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

Figure G.2. Complex XHTML table.

  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. G.2: table2.html     -->  6  <!-- Intermediate table design -->  7  8  <html xmlns = "http://www.w3.org/1999/xhtml">  9     <head> 10        <title>Internet and WWW How to Program - Tables </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 used to --> 21           <!-- format entire columns                 --> 22           <colgroup>                            23 24              <!-- span attribute determines how many columns --> 25              <!-- the <col> tag affects                      --> 26              <col align = "right" span = "1" /> 27           </colgroup>                           28 29           <thead> 30 31              <!-- rowspans and colspans merge the 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>Yes</td> 64                 <td>Yes</td> 65              </tr> 66 67              <tr> 68                 <th>Llamas</th> 69                 <td>1</td> 70                 <td>Andes Mountains</td> 71                 <td>Yes</td> 72                 <td>Yes</td> 73              </tr> 74 75           </tbody> 76 77        </table> 78 79     </body> 80  </html> 

The table begins in 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).

Table cells are sized to fit the data they contain. Document authors can create larger data cells by using the 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 in 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.

Common Programming Error G.1

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 they do not, the formatting of your table will be distorted and you may inadvertently create more columns and rows than you originally intended.


Line 42 introduces the 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 at which each character in a word is aligned).



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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