home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeHTML & XHTML: The Definitive GuideSearch this book

17.4. Tricks with Tables

Enough with the cheap tricks. On to some really good ones: tricks with the <table> tag and attributes that add some attractive features to your documents.

By design, tables let authors create appealing, accessible tables of information. But the table tags also can be exploited to create innovative, attractive page designs that are otherwise unattainable in standard HTML or XHTML.

17.4.1. Multicolumn Pages

One very common and popular page-layout element missing from the either HTML or XHTML standard is multiple columns of text. Although Netscape version 4 (and earlier) supports the <multicol> extension, for a more universal solution, place your document content inside a table of one row with two or more columns. Section 14.2, "Multicolumn Layout" Straddle heads

The basic multicolumn format is just the start. By adding cells that span across the columns, you create headlines. Similarly, you can make figures span across more than one column: simply add the colspan attribute to the cell containing the headline or figure. Figure 17-3 shows an attractive three-column layout with straddle heads and a spanning figure, created from the following HTML source with table tags:

<table border=0 cellspacing=7>
    <th colspan=5><h2>The History of the Kumquat</h2>
  <tr valign=top>
    <td rowspan=2>Copy for column 1...
    <td rowspan=2 width=24><br>
    <td>Copy for column 2...
    <td width=24><br>
    <td>Copy for column 3...
    <td colspan=3 align=center><img src="pics/fruit.gif">
    <i>The Noble Fruit</i>

To achieve this nice layout, we used the colspan attribute on the cell in the first row to span all five table columns (three with copy and the two intercolumn spaces). We use the rowspan attribute on the first column and its adjacent column spacer to extend the columns down beside the figure. The figure's cell has a colspan attribute so that the contents span the other two columns and intervening spaces.

17.4.2. Side Heads

The only text-heading features available in HTML and XHTML are the <h1> through <h6> tags. These tags are always embedded in the text flow, separating adjacent paragraphs of text. Through multiple columns, you can achieve an alternative style that places headings into a separate side column, running vertically alongside the document text.

Figure 17-4 shows you a fairly fancy pair of side heads, the result of the following bit of source XHTML table code:

    <th width="20%" align="right">
      <h3>Section 1</h3></th>
      Copy for section 1 goes on and on a bit 
      so that it will take up more than one line in the
      table cell window... </td>
    <th align="right">
      <h3>Section 2</h3></th>
      Copy for section 2 goes on and on a bit 
      so that it will take up more than one line in the
      table cell window...</td>
Figure 17-3

Figure 17-3. Fancy straddle heads and spanning figures with HTML table tags

Notice how we create reasonably attractive side heads set off from the left margin of the browser window by adjusting the first header cell's width and right-justifying the cell contents.

Just as in our multicolumn layout, the example side-head layout uses an empty column to create a space between the narrow left column containing the heading and the wider right column containing the text associated with that heading. It's best to specify that column's width as a percentage of the table width, rather than explicitly in numbers of pixels to make sure that the heading column scales to fit both wide and narrow display windows.

Figure 17-4

Figure 17-4. Table tags created these side heads

17.4.3. Better Forms Layout

Of all the features in HTML and XHTML, forms cry out for better layout control. Unlike other structured elements, forms look best when rendered in a fixed layout with precise margins and vertical alignment of elements. However, except for carefully planned <pre> formatted form segments, the common language just doesn't give us any special tools to better control forms layout. Basic form layout

Your forms will almost always look better and be easier for your readers to follow if you use a table to structure and align the form's elements. For example, you might use a vertical alignment to your forms, with field labels to the left and their respective form elements aligned to an adjacent vertical margin on the right. Don't try that with just standard HTML or XHTML.

Rather, prepare a form that contains a two-column table. The following HTML source does just that, as shown in Figure 17-5:

<form method=post action="http:/cgi-bin/process">
      <th align=right>Name: 
      <td><input type=text size=32>
      <th align=right>Address: 
      <td><input type=text size=32>
      <th align=right>Phone: 
      <td><input type=text size=12>
      <td colspan=2 align=center>
      <input type=submit value="Register">

Of course, more complex form layouts can be managed with tables. We recommend you first sketch the form layout on paper and plan how various combinations of table elements, including row- and column-straddled table cells might be used to effect the layout.

Figure 17-5

Figure 17-5. Align your forms nicely with tables

17.4.4. Embedded Guides

We generally argue for subtlety when you include hyperlinks in your documents, embedding them within the content and within context. But there are times when prominent guides to additional content are appropriate, like street signs in a crowded neighborhood.

Traditionally, HTML authors have placed their street signs (arrow icons with text labels) between major sections or at the beginning and end of the document to guide users back to a home page or on to the next page in the document series. Using <table> and its align attribute, you also can embed those guideposts within the document flow, but distinct from the content. And tables help you align the signpost elements for a more pleasing and concise presentation.

The following XHTML segment, for example, uses a two-column table to set a hyperlink guide apart from the document content. The technique also nicely aligns the guide's graphical and textual elements, thereby giving the reader a clear and distinct option to jump to another section of the document, as shown in Figure 17-7:

The role of the kumquats in earthly and cosmic affairs has been well documented.
Nearly from the moment that humankind began recording history -- even as 
oral tradition -- historians have reported the extraordinary and 
omnipresent influences of kumquats.</p>
<table align="right">
   <td><a href="#arts"><img src="pics/d_arrow.gif" border="0"></a></td>
   <td><a href="#arts"><h6>Kumquat influence<br/>in the Arts</h6></a></td>
For instance, early in the history of man, the kumquat played a vital role in the
formation of religious beliefs.  Central to annual harvest celebrations was the day 
upon which kumquats ripened.  Likened to the sun (<em>sol</em>), the golden fruit 
was taken (<em>stisus</em>) from the trees on the day the sun stood highest in the 
sky.  We carry this day forward even today, as our summer <em>solstice</em>.

Figure 17-7

Figure 17-7. Tables let you embed signposts in your content flow.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.