You create a wide variety of tables with only five tags: the <table> tag, which encapsulates a table and its elements in the HTML 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 to the table. Each tag has one or more required and optional attributes, some of which affect not only the tag itself, but related tags. [<tr>, 9.2.2] [<th>, 9.2.3] [<td>, 9.2.3] [<caption>, 9.2.4]
The <table> tag and its </table> end tag define and encapsulate a table within the body of your HTML document. 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.
Unless overridden by the align attribute, the table's alignment in the browser window matches that of the containing text flow. Normally, this means that tables are aligned against the left margin of the current text flow. However, the table may be centered in the browser window if the preceding text is centered with the <center> tag or <div align=center>, or right-aligned by being in a right-aligned table cell (see the align attribute options below). [<p> 4.1.2] [<th>, 4.7.6]
The only content allowed within the <table> tag besides the optional <caption> tag is one or more <tr> tags, which define each row of table contents.
Like images, tables are rectangular objects that float in the browser display, aligned according to the current text flow: normally left-justified, abutting the left margin of the display window, or centered if under the influence of the <center> tag, centered paragraph, or centered division. Unlike images, however, tables are normally not inline objects. Text content normally flows above and below a table, not beside it. You change that display behavior for Netscape or Internet Explorer with the align attribute for the <table> tag.
The align attribute accepts a value of either left 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. This alignment style corresponds to the left and right alignment of images with text wrapping around the image.
You use the align attribute within the <table> tag differently than within the <tr>, <td>, and <th> tags. In those tags, the attribute controls text alignment within the table cells, not alignment of the table within the containing text flow.
This attribute is only supported by Internet Explorer and Netscape.
You may make the background of a table a different color than the document's background with the bgcolor attribute for the <table> tag. Honored only by Internet Explorer and ignored by all other browsers, the color value for the bgcolor attribute must be set to either an RGB color value or a standard color name. Both the syntax of color values and the acceptable color names are provided in Appendix E, Color Names and Values.
Internet Explorer gives every cell in the table this background color. Individual cell colors can be changed by providing the bgcolor attribute for those cells.
Supported by Internet Explorer only, these attributes set the color of the table borders, if displayed. Their values can be either an RGB hexadecimal color value or a standard color name, both of which are described fully in Appendix E.
Netscape and Internet Explorer normally draw a table border with three colors. Netscape uses black and shades of gray. Internet Explorer does, too, unless you set those colors with special attributes: The bordercolorlight and bordercolordark colors shade the edges of the border to give it a 3D appearance, while bordercolor shades the central body of the border.
The effectiveness of the 3D effect is directly tied to the relationship of these three colors. In general, the light color should be about 25 percent brighter than the border color, and the dark color should be about 25 percent darker.
The optional border attribute for the <table> tag tells the browser to add a border around the table and the rows and cells within it, and enables the various other <table> attributes that affect the appearance and spacing of the border.
A border value is optional; alone, the attribute simply enables borders and a set of default characteristics. Mosaic ignores a border attribute value, if you include one; the browser provides only one style border line for tables (see Figure 9-1).
Netscape and Internet Explorer, on the other hand, let you supply an integer value for border equal to the pixel width of the chiseled-edge lines that make the table appear to be embossed onto the page. If you don't give it one, Netscape's and Internet Explorer's default border value is 1, and therefore draws the table borders with a chiseled edge that is one pixel wide. The border attribute value of 0 has the same effect as no border at all.
The cellspacing attribute is for Netscape only; Mosaic and Internet Explorer ignore it. This attribute controls the amount of space Netscape places between adjacent cells in a table and along the outer edges of cells along the edges of a table.
Netscape normally puts two pixels of space between cells and along the outer edges of the table. If you include a border attribute in the <table> tag, the cell spacing between interior cells grows by two more pixels (four total) to make space for the chiseled edge on the interior border. The outer edges of edge cells grow by the value of the border attribute.
By including the cellspacing attribute you can widen or reduce the interior cell borders. For instance, to make the thinnest possible interior cell borders, include the border and cellspacing=0 attributes in the table's tag.
The cellpadding attribute is supported only by Netscape. It controls the amount of space between the edge of a cell and its contents, which by default is one pixel. You may make all the cell contents in a table touch their respective cell borders by including cellpadding=0 in the table tag. You may also increase the cellpadding space by setting its value greater than 1.
With Netscape, the interactions between the border, cellpadding, and cellspacing attributes of the <table> tag combine in ways that can be confusing. Figure 9-2 summarizes how these attributes interact to create interior and exterior borders of various widths.
While all sorts of combinations of the border and cellspacing attributes are possible, the most common are:
Just as with inline images, the hspace and vspace attributes tell Internet Explorer and Netscape to add some extra room on the left and right sides (for hspace) and the top and bottom (for vspace) of a table, thereby setting it off from the window edge and surrounding content. The attribute value is the integer number of pixels for that padding; a value of 0 is the default.
Figure 9-3 illustrates the effect of the hspace and vspace attribute spacing around a left-justified table with wraparound text.
The valign attribute for the <table> tag currently is supported only by Internet Explorer. It sets the default vertical alignment of data in their cells for the entire table. You achieve similar effects in Netscape by including a valign attribute within the individual <tr>, <td>, and <th> tags.
Acceptable values for the valign attribute in <table> are top or bottom; the default vertical position is the center of the cell.
The extended browsers automatically make a table only as wide as needed to correctly display all of the cell contents. With Netscape and Internet Explorer, but not Mosaic, you can make a table wider with the width attribute.
The value of the width attribute is either an integer number of pixels or a relative percentage of the screen width, including values greater than 100 percent. For example,
<table width=400>
tells the extended browser to make the table 400 pixels wide, including any borders and cell spacing that extend into the outer edge of the table. If the table is wider than 400 pixels, the browser ignores the attribute.
Alternatively,
<table width="50%">
tells the browser to make the table half as wide as the display window. Again, this width includes any borders or cell spacing that extend into the outer edge of the table, and has no effect if the table normally is more than half the user's current screen width.
Use relative widths for tables you want to automatically resize to the user's window; for instance, tables you always want to extend across the entire window (<table width="100%">). Use an absolute width value for carefully formatted tables whose contents will become hard to read in wide display windows.
Every row in a table is created with a <tr> tag. Within the <tr> tag are one or more cells containing headers, each defined with the <th> tag, and data, each defined with the <td> tag (see below).
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.
The extended browsers automatically align cell contents inside their respective cells. The align attribute for the <tr> tag lets you change the default horizontal alignment of all 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, or center causes the extended browser to align the contents of each cell in the row against the left or right edge, or in the center of the cell, respectively. You also may change the alignment for individual cells within a row, overriding the value of the align attribute in the <tr> tag with the align attribute for the <th> and <tr> tags, as described below. Accordingly, use the align attribute in the <tr> tag to specify the most common cell content justification for the row (if not the default), and use a different align attribute for those individual cells that deviate from that common alignment.
Table 9.1 displays the horizontal (align) and vertical (valign) table cell-content attribute values and options. Values in parentheses are the defaults.
Attribute | Netscape and Internet Explorer | Mosaic | ||
---|---|---|---|---|
Headers | Data | Headers | Data | |
align | Left | (Left) | (Left) | (Left) |
(Center) | Center | Center | Center | |
Right | Right | Right | Right | |
valign[1] | Top | Top | (Top) | (Top) |
(Center) | (Center) | N/A[2] | N/A | |
Bottom | Bottom | N/A | N/A | |
Baseline | Baseline | N/A | N/A | |
Footnotes:
[1] |
Like its relative for the <table> tag, the bgcolor attribute for the <tr> tag is used by Internet Explorer only to set the background color of the entire row. Its value is either an RGB color value or a standard color name. Both the syntax of color values and the acceptable color names are provided in Appendix E.
Every cell in the row will be given this background color. Individual cell colors can be changed by providing the bgcolor attribute for those cells.
Like their brethren for the <table> tag, Internet Explorer lets you use these attributes to set the color of the borders within the current row.
Their values override any values set by the corresponding attribute in the containing <table> tag. See the corresponding description of these extensions in 9.2.1 for details. Color values can be either an RGB color value or a standard color name, both of which are described fully in Appendix E.
With Netscape and Internet Explorer, you may change the default vertical alignment for the contents of data cells contained within a table row. Normally, the browsers render cell contents centered vertically. By including the valign attribute in the <tr> tag with a value of top or bottom, you tell the extended browsers to place the table row's contents flush against the top or bottom of their cells.
Besides top, bottom, and the default center values for valign, Netscape supports a fourth option, baseline, which specially aligns cell contents to the baseline of the top line of text in other cells in the row (Figure 9-4). The value center, although acceptable, has no real effect since it simply reiterates the default vertical alignment.
<table 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>
The <th> and <td> tags go inside the <tr> tags of an HTML table to create the cells and contents within the row. The tags operate similarly; the only real differences are that Netscape and Mosaic 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 (Table 9.1). Internet Explorer makes no special formatting for <th> and renders it identical to <td>.
Like those available for the table row (<tr>) tag, the extended browsers provide a rich set of content-alignment attributes you may apply to a single data or header cell and override the default values for the current row. The browsers also support special attributes that control the number of columns or rows a cell may span in the table.
The contents of the <th> and <td> tags can be anything you might put in the body of an HTML 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.
Except with Mosaic, 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.
The align and valign attributes are identical to those of the same name for the table row tag (<tr>; see above), 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. And, while align works with all the extended browsers, valign is not supported by NCSA Mosaic. See Table 9.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. The valign attribute may have a value of top, bottom, center, or baseline, telling Netscape and Internet Explorer to align the cell's contents to the top or bottom edge, or in the center of the cell, or, with Netscape only, to the baseline of the first line of text in other cells in the row.
Like its twin in the <table> tag which lets you widen a table, the width attribute for Netscape's and Internet Explorer's (not Mosaic's) table cell tags lets you widen an individual cell and, hence, the entire column it occupies. You set the width to an integer number of pixels, or a percentage indicating the cell's width as a fraction of the table as a whole.
For example,
<th width=400>
sets the current header cell's width, and hence the entire column of cells, to 400 pixels wide. Alternatively,
<td width="40%">
creates a data cell whose column will occupy 40 percent of the entire table's width.
Since the extended browsers make all cells in a column the same width, you should place a width attribute in only one cell within a column, preferably the first instance of the cell in the first row for source readability's sake. If two or more cells in the same column happen to have width attributes, the widest one is honored. You can't make a column thinner than what the browser automatically determines is the minimum width needed to display all of any cell contents in the column. So, if the browser determines that the column of cells needs to be at least 150 pixels wide to accommodate all the cells' contents, it will completely ignore a width attribute in one of the column's cell tags that attempts to make the cell only 100 pixels wide.
It's common to have a table header that describes several columns beneath it, like the headers we use in Table 9.1. Use the colspan attribute in a table header or data tag to extend an HTML 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.)
Just as the colspan attribute layers a table cell across several columns, the rowspan attribute stretches a cell down two or more rows in the table.
You include the rowspan attribute in the <th> or <td> tag of the uppermost row of the table where you want the cell to begin and set its value equal to the number of rows you want it to span. The cell then occupies the same space as the current row and an appropriate number of cells below that row. The browser flows the contents of the cell to occupy the entire extended space.
For example,
<td rowspan=3>
creates a cell that occupies the current row plus two more rows below that.
Like the colspan attribute, the browser ignores over-extended rowspan attributes and will only extend the current cell down rows you've explicitly defined by other <tr> tags following the current row. The browsers will not add empty rows to a table to fill a rowspan below the last defined row in a table.
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 may explicitly include in the same tag, as described earlier.
The extended 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 a table header or data tag, stops that normal word wrapping. 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.
Yet again, Internet Explorer lets you change the background color--this time for an individual data cell. Its value is either an RGB hexadecimal color value or a standard color name. Both the syntax of color values and the acceptable color names are provided in Appendix E.
Internet Explorer lets you alter the colors that make up an individual cell's border--if xtable borders are turned on with the border attribute, of course. See the respective attributes' descriptions under the <table> tag in 9.2.1 for details.
The values for these three attributes override any values set for the containing <table> or <tr> tag. Their values can be either an RGB color value or a standard color name, both of which are described fully in Appendix E.
A table commonly needs a caption to explain its contents, so the extended browsers provide a table-caption tag. Authors typically place the <caption> tag and its contents immediately after the <table> tag, but it can be placed nearly anywhere inside the table and between the row tags. The caption may contain any body content, much like a cell within a table.
Unfortunately for document authors, Netscape Navigator and NCSA Mosaic implement one method of caption alignment and positioning, while Internet Explorer provides a conflicting set of attributes for the same purpose. By default, the browsers center the caption, except Netscape and Internet Explorer center with respect to the table, including word wrapping when necessary. Mosaic centers the caption text within the browser window, not necessarily centered on its associated table. Mosaic does, however, specially embellish caption text with a boldfaced font style (Figure 9-1).
All the extended browsers let you put a caption above or below the table; they just can't agree on the details. All default to a caption on top. Netscape and Mosaic let you place it below the table with the align attribute set to the value bottom (the value top, of course, is equivalent to the default).
Internet Explorer, on the other hand, uses the align attribute to control the horizontal position of the caption and a special valign attribute to move it below the table. With Internet Explorer, you set the align attribute to left, center (the default), or right, which positions the caption to the respective locations relative to the table. The other browsers ignore Internet Explorer's different caption-xalign values, and vice versa.
Internet Explorer alone recognizes a special valign attribute to control the vertical position of a caption. The valign attribute may be set to either top or bottom; if the attribute is not specified, top is assumed.