10.2. Table Tags
A wide variety of tables can be created with only five tags: the
<table> tag, which encapsulates a table and
its elements in the document's body content; the
<tr> tag, which defines a table row; the
<th> and <td> tags,
which define the table's headers and data cells; and the
<caption> tag, which defines a title or
caption for the table. Beyond these core tags, you may also define
and control whole sections of tables, including adding running
headers and footers, with the <colgroup>,
<col>, <tbody>,
<thead>, and
<tfoot> tags. Each tag has one or more
required and optional attributes, some of which affect not only the
tag itself, but also related tags.
10.2.1. The <table> Tag
The
<table> tag and its
</table> end tag define and encapsulate a
table within the body of your document. Unless otherwise placed
within the browser window by style sheet, paragraph, division-level,
or other alignment options, the browser stops the current text flow,
breaks the line, inserts the table beginning on a new line, and then
restarts the text flow on a new line below the table.
<table>
- Function:
-
Define a table
- Attributes:
-
ALIGN | ONCLICK |
BACKGROUND  | ONDBLCLICK |
BGCOLOR | ONKEYDOWN |
BORDER | ONKEYPRESS |
BORDERCOLOR  | ONKEYUP
|
BORDERCOLORLIGHT  | ONMOUSEMOVE |
CELLPADDING | ONMOUSEOUT |
CELLSPACING | ONMOUSEOVER |
CLASS | ONMOUSEUP |
COLS  | RULES |
DIR | STYLE |
FRAME | SUMMARY |
HEIGHT  | TITLE |
HSPACE  | VALIGN  |
ID | VSPACE  |
LANG | WIDTH |
NOWRAP  |
- End tag:
-
</table>; never omitted
- Contains:
-
table_content
- Used in:
-
block
|
The only content allowed within the <table>
is one or more <tr> tags, which define each
row of table contents, along with the various table sectioning tags:
<thead>, <tfoot>,
<tbody>, <col>, and
<colgroup>.
10.2.1.1. The align attribute (deprecated)
The HTML 4 and XHTML standards have deprecated this attribute in favor of the align property provided by Cascading Style Sheets. Yet it remains popular and is currently well-supported by the popular browsers.
Like images, tables are rectangular objects that float in the browser
display, aligned according to the current text flow. Normally, the
browser left-justifies a table, abutting its left edge to the left
margin of the display window. Or the table may be centered if under
the influence of the <center> tag, centered
paragraph, or centered division. Unlike images, however, tables are
not inline objects. Text content normally flows above and below a
table, not beside it. You can change that display behavior with the
align attribute for the
<table> tag.
The align attribute accepts a value of either
left, center, or
right, indicating that the table should be placed
flush against the left or right margin of the text flow, with the
text flowing around the table, or in the middle with text flowing
above and below.
Note that the align attribute within the
<table> tag is different from those used
within a table's element tags <tr>,
<td>, and <th>. In
those tags, the attribute controls text alignment within the
table's cells, not alignment of the table within the containing
text flow.
10.2.1.4. The frame and rules attributes
With Netscape Navigator 4, the border attribute is
all or nothing, affecting the appearance and spacing both of the
frame around the table and the rule lines between data cells.
Internet Explorer Version 4 and later, as well as the latest Netscape
Navigator Version 6, on the other hand, let you individually modify
the various line segments that make up the borders around the table
(frame) as well as around the data cells
(rules).
The standard frame attribute modifies
border's effects for the lines that surround
the table. The default value -- what you get if you don't
use frame at all -- is box,
which tells the browser to draw all four lines around the table. The
value border does the same thing as
box. The value void removes all
four of the frame segments. The
frame values above,
below, lhs, and
rhs draw the various border segments on the top,
bottom, left, and right side, respectively, of the table. The value
hsides draws borders on the top and bottom
(horizontal) sides of the table; vsides draws
borders on the left and right (vertical) sides of the table.
With standard tables (supported in Internet Explorer 4 and later, and
in Netscape 6), you also may control the thickness of a table's
internal cell borders via the
rules attribute. The default behavior, represented
by the value of all, is to draw borders around all
cells. Specifying groups places thicker borders
between row and column groups defined by the
<thead>, <tbody>,
<tfoot>, <col>, and
<colgroup> tags. Using
rows or cols places borders
only between every row or column, respectively, while using
none removes borders from every cell in the table.
10.2.1.10. The valign and nowrap attributes
The valign
attribute sets the default vertical alignment of data in cells for
the entire table. Acceptable values for the valign
attribute in <table> are
top, bottom,
middle, or baseline; the
default vertical position is the center of the cell.
Browsers treat each table cell as though
it's a browser window unto itself, flowing contents inside the
cell as they would common body contents (although subject to special
table-cell alignment properties). Accordingly, the browsers
automatically wrap text lines to fill the allotted table cell space.
The nowrap attribute, when included in the
<table> tag, stops that normal word wrapping
in all rows in the table. With nowrap, the browser
assembles the contents of the cell onto a single line, unless you
insert a <br> or
<p> tag, which then forces a break so that
the contents continue on a new line inside the table cell.
The valign and nowrap
attributes for the <table> tag currently are
supported only by Internet Explorer. You achieve similar effects in
Netscape by including a valign or
nowrap attribute within the individual
<tr>, <td>, and
<th> tags.
10.2.3. The <tr> Tag
Make
a new row in a table with the
<tr> tag. Place within the
<tr> tag one or more cells containing
headers, defined with the <th> tag, and
data, defined with the <td> tag (see Section 10.2.4, "
The <th> and <td> Tags"). The <tr> tag
accepts a number of special attributes that control its behavior,
along with the common table attributes described in Section 10.2.2, "Common Table Attributes"
<tr>
- Function:
-
Define a row within a table
- Attributes:
-
ALIGN | ONDBLCLICK |
BGCOLOR | ONKEYDOWN |
BORDERCOLOR  | ONKEYPRESS |
BORDERCOLORDARK  | ONKEYUP |
BORDERCOLORLIGHT  | ONMOUSEDOWN |
CHAR | ONMOUSEMOVE |
CHAROFF | ONMOUSEOUT |
CLASS | ONMOUSEOVER |
DIR | ONMOUSEUP |
ID | STYLE |
LANG | TITLE |
NOWRAP  | VALIGN
|
ONCLICK |
- End tag:
-
</tr>; may be omitted in HTML
- Contains:
-
tr_content
- Used in:
-
table_content
|
Every row in a table has the same number of cells as the longest row;
the browser automatically creates empty cells to pad rows with fewer
defined cells.
10.2.3.1. The align and valign attributes
The align attribute for the
<table> tag may be deprecated in the HTML
and XHTML standards, but it is alive and kicking for
<tr> and other table elements. The
align attribute for the
<tr> tag lets you change the default
horizontal alignment of all the contents of the cells in a row. The
attribute affects all the cells within the current row, but not
subsequent rows.
An align attribute value of
left, right,
center, justify, or
char causes the browser to align the contents of
each cell in the row against the left or right edge, in the center of
the cell, spread across the cell, or to a specified character in the
cell, respectively.
Similarly, change the default vertical alignment for the contents of
data cells contained within a table row with the
valign attribute. Normally, the browsers render
cell contents centered vertically. By including the
valign attribute in the
<tr> tag with a value of
top, bottom, or
baseline, you tell the browser to place the table
row's contents flush against the top or bottom of their cells
or aligned to the baseline of the top line of text in other cells in
the row (Figure 10-3). The value
middle, although acceptable, has no real effect
since it simply reiterates the default vertical alignment:
<table border="border">
<tr>
<th>Alignment</th>
<th>Top</th>
<th>Baseline</th>
<th>Center</th>
<th>Bottom</th>
</tr>
<tr align="center">
<th><h1>Baseline_ _<br />Another line</h1></th>
<td valign="top">AAyy</td>
<td valign="baseline">_AAyy_</td>
<td valign="center">AAyy</td>
<td valign="bottom">AAyy</td>
</tr>
</table>
Figure 10-3. Effects of the valign attribute on table cell content alignment
You also may specify the horizontal and vertical alignments for
individual cells within a row (Section 10.2.4.1, "The align and valign attributes").
Use the alignment attributes in the <tr> tag
to specify the most common cell content justifications for the row
(if not the default), and use a different align or
valign attribute for those individual cells that
deviate from the common alignment.
Table 10-1 contains the horizontal
(align) and vertical (valign)
table cell- content attribute values and options. Values in
parentheses are the defaults for the popular browsers.
Table 10-1. Table Cell-Content Alignment Attribute Values and Options
Attribute |
Netscape and IE Headers
|
Netscape and IE Data
|
|
|
Left
|
(Left)
|
|
align
|
(Center)
|
Center
|
|
|
Right
|
Right
|
|
|
Justify[63]
|
Justify[63]
|
|
|
Char[63]
|
Char[63]
|
|
|
Top
|
Top
|
|
valign
|
(Center)
|
(Center)
|
|
|
Bottom
|
Bottom
|
|
|
Baseline
|
Baseline
|
10.2.4.
The <th> and <td> Tags
The
<th> and <td> tags
go inside the <tr> tags of a table to create
the cells and contents within the row. The tags operate similarly;
the only real differences are that the browsers render header
text -- meant to entitle or otherwise describe table data -- in
boldface font style and that the default alignment of their
respective contents may be different than for data. Data typically
gets left-justified by default; headers get centered (Table 10-1).
<th> and <td>
- Function:
-
Define table data and header cells
- Attributes:
-
ABBR | NOWRAP |
ALIGN | ONCLICK |
AXIS | ONDBLCLICK |
BACKGROUND  | ONKEYDOWN |
BGCOLOR | ONKEYPRESS |
BORDERCOLOR  | ONKEYUP |
BORDERCOLORDARK  | ONMOUSEDOWN |
BORDERCOLORLIGHT  | ONMOUSEMOVE |
CHAR | ONMOUSEOUT |
CHAROFF | ONMOUSEOVER |
CLASS | ONMOUSEUP |
COLSPAN | ROWSPAN |
DIR | SCOPE |
HEADERS | STYLE |
HEIGHT | TITLE |
ID | VALIGN |
LANG | WIDTH |
- End tag:
-
</th> or </td>; may be omitted in HTML
- Contains:
-
body_content
- Used in:
-
tr_content
|
Like those available for the table row
(<tr>) tag, the table cell tags support a
rich set of style and content-alignment attributes you may apply to a
single data or header cell. These attributes override the default
values for the current row. There are also special attributes that
control the number of columns or rows a cell may span in the table.
The <th> and <td>
tags also accept the common table attributes described in Section 10.2.2, "Common Table Attributes".
The contents of the <th> and
<td> tags can be anything you might put in
the body of a document, including text, images, forms, and so
on -- even another table. And, as described earlier, the browser
automatically creates a table large enough, both vertically and
horizontally, to display all the contents of any and all the cells.
If a particular row has fewer header or data items than other rows,
the browser adds empty cells at the end to fill the row. If you need
to make an empty cell before the end of a row, for instance, to
indicate a missing data point, create a header or data cell with no
content.
Empty cells look different than those containing data or headers if
the table has borders: the empty cell will not be seemingly embossed
onto the window, but instead is simply left blank. If you want to
create an empty cell that has incised borders like all the other
cells in your table, be sure to place a minimal amount of content in
the cell: a single <br> tag, for instance.
10.2.4.1. The align and valign attributes
The align and
valign
attributes are identical to those of
the same name for the table row tag (<tr>;
see Section 10.2.3, "The <tr> Tag"), except that when used with
a <th> or <td> tag,
they control the horizontal or vertical alignment of content in just
the current cell. Their value overrides any alignment established by
the respective align or valign
attribute of the <tr> tag, but does not
affect the alignment of subsequent cells. See Table 10-1 for alignment details.
You may set the align attribute's value to
left, right, or
center, causing the browsers to align the cell
contents against the left or right edge, or in the center of the
cell, respectively. In addition, Internet Explorer supports a value
of justify to fill each line of text so that it is
flush to both sides of the cell. The valign
attribute may have a value of top,
bottom, middle, or
baseline, telling the browser to align the
cell's contents to the top or bottom edge, or in the center of
the cell, or (Netscape only) to the baseline of the first line of
text in other cells in the row.
10.2.4.4. The colspan attribute
It's
common to have a table header that describes several columns beneath
it, like the headers we use in Table 10-1. Use the
colspan attribute in a table header or data tag to
extend a table cell across two or more columns in its row. Set the
value of the colspan attribute to an integer value
equal to the number of columns you want the header or data cell to
span. For example:
<td colspan="3">
tells the browser to make the cell occupy the same horizontal space
as three cells in rows above or below it. The browser flows the
contents of the cell to occupy the entire space.
What happens if there aren't enough extra cells on the right?
The browser just extends the cell over as many columns as exist to
the right; it doesn't add extra empty cells to each row to
accommodate an over-extended colspan value. You
may defeat that limitation by adding the needed extra, but
content-less, cells to a single row. (Give them a single
<br> tag as their contents if you want
Netscape's embossed border around them.)
10.2.4.6. Combining colspan and rowspan
You may extend a single cell both across several columns and down
several rows by including both the colspan and
rowspan attributes in its table header or data
tag. For example:
<th colspan="3" rowspan="4">
creates a header cell that, as you might expect, spans across three
columns and down four rows, including the current cell and extending
two more cells to the right and three more cells down. The browser
flows the contents of the cell to occupy the entire space, aligned
inside according to the current row's alignment specifications
or to those you explicitly include in the same tag, as described
earlier.
 |  |  | | 10. Tables |  | 10.3. Newest Table Tags |
Copyright © 2002 O'Reilly & Associates. All rights reserved.
|