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)

"http://www.w3.org/1999/xhtml"> 9 10

1 6 7 8

Intermediate table design 11 12 13 14 15

Table Example Page

16 17 "1"> 18 19 20 21 22 23 24 25 26 "right"span ="1"/> 27 28 29 30 31 32 33 34 35 36 40 41 42 46 47 48 "bottom"> 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

Here is a more complex sample table.
"2"> 37 "camel.gif" width = "205" 38 height = "167" alt = "Picture of a camel" /> 39 "4" valign = "top"> 43

Camelid comparison

44

Approximate as of 9/2002

45
# of Humps Indigenous region Spits? Produces Wool?
Camels (bactrian) 2 Africa/Asia "2">Llama "2">Llama
Llamas 1 Andes Mountains
76 77 78

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).


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).

Категории