G.2. Basic XHTML Tables

Tables are used to organize data in rows and columns. Our first example (Fig. G.1) creates a table with six rows and two columns to display price information for fruit.

Figure G.1. XHTML table.

1 6 7 8

"http://www.w3.org/1999/xhtml"> 9 10 A simple XHTML table 11 12 13 14 15 16 "1"width ="40%"17 summary = "This table provides information about 18 the price of fruit"> 192021222324252627 28 29 30 313233343536 37 38 39 404142434445 46 47 48 49505152535455565758596061 6263646566
Price of Fruit
Fruit Price
Total $3.75
Apple $0.25
Orange $0.50
Banana $1.00
Pineapple $2.00
67 68 69

Tables are defined with the table element (lines 1666). Lines 1618 specify the start tag for a table element that has several attributes. The border attribute specifies the table's border width in pixels. To create a table without a border, set border to "0". This example assigns attribute width the value "40%" to set the table's width to 40 percent of the browser's width. A developer can also set attribute width to a specified number of pixels. Try resizing the browser window to see how the width of the window affects the width of the table.

As its name implies, attribute summary (lines 1718) describes the table's contents. Speech devices use this attribute to make the table more accessible to users with visual impairments. The caption element (line 22) describes the table's content and helps textbased browsers interpret the table data. Text inside the tag is rendered above the table by most browsers. Attribute summary and element caption are two of the many XHTML features that make Web pages more accessible to users with disabilities.

A table has three distinct sectionshead, body and foot. The head section (or header cell) is defined with a thead element (lines 2631), which contains header information such as column names. Each tr element (lines 2730) defines an individual table row. The columns in the head section are defined with th elements. Most browsers center text formatted by th (table header column) elements and display them in bold. Table header elements are nested inside table row elements.

The foot section (lines 3540) is defined with a tfoot (table foot) element. The text placed in the footer commonly includes calculation results and footnotes. Like other sections, the foot section can contain table rows, and each row can contain columns.

The body section, or table body, contains the table's primary data. The table body (lines 4464) is defined in a tbody element. In the body, each tr element specifies one row. Data cells contain individual pieces of data and are defined with td (table data) elements within each row.

G 3 Intermediate XHTML Tables and Formatting

Категории