Basic Table Structure:
A table in HTML is defined by the basic command pair:
|
Begin Table
|
End Table
|
<table>
|
</table>
|
Captions, headers and data are set up within tables as follows:
|
|
Column A
|
Column B
|
Column C
|
|
|
Begin
|
<table>
|
|
Caption
|
<caption>
|
Table Caption
|
</caption>
|
|
Headers
|
<tr>
|
<th>
H1
</th>
|
<th>
H2
</th>
|
<th>
H3
</th>
|
<td>
A1
</td>
|
<td>
B1
</td>
|
<td>
C1
</td>
|
<td>
A2
</td>
|
<td>
B2
</td>
|
<td>
C2
</td>
|
<td>
A3
</td>
|
<td>
B3
</td>
|
<td>
C3
</td>
|
|
</tr>
|
|
Row 1
|
<tr>
|
</tr>
|
|
Row 2
|
<tr>
|
</tr>
|
|
Row 3
|
<tr>
|
</tr>
|
|
End
|
</table>
|
The table caption (<caption></caption>)
and column headers (<th></th>) are
optional elements.
Example:
<table border=1>
<tr>
<td>
Column 1
</td>
<td>
Column 2
</td>
<td>
Column 3
</td>
</tr>
</table>
|
The code at the left would produce the simple table below with one row and three columns.
| Column 1 |
Column 2 |
Column 3 |
|
This basic syntax can produce very complex table structures, but as the complexity increases
so does the complexity of the code required to place and define the table. It is a good idea
to map out a table on paper before coding and to check that all table commands are properly
paired and placed within the structure.
Something else to remember is that HTML cannot display a table until all its components have
been loaded. Very large tables can take a long time to load and may confuse or irritate
users. If it is possible, large tables should be broken down into smaller units.
Note also that some low end browsers cannot display tables at all.
Table Display and Formatting Parameters:
The table below outlines the various command parameters that can be used within tables
to refine display and set formatting:
|
Parameter
|
Applies to
|
Definition/Action
|
Examples
|
align=value
|
Rows and Cells
|
Where value defines the horizontal alignment for the table element.
Allowed values are:
left, right and center
|
<table width=200 border=1>
<tr>
<td width=100
align=right>
Right
</td>
<td width=100
align=center>
Center
</td>
</tr>
</table>
|
|
NOTE: When applied to table rows
(<tr>)
horizontal alignment governs the entire row. When applied to table data
(<td>) alignment affects only the
current table cell.
|
border=n
|
Table
|
Where n defines the width, in pixels, of the border to be placed
around every table cell.
|
<table border=0>
<tr>
<td>
Unbordered
</td>
<td>
Table
</td>
</tr>
</table>
|
<table border=1>
<tr>
<td>
Bordered
</td>
<td>
Table
</td>
</tr>
</table>
|
|
|
|
|
|
NOTE: Bordering applies to the entire table. Borders of larger
than 1 pixel are rendered in most browsers with an embossed appearance.
|
cellpadding =n
|
Table
|
Where n defines the space, in pixels, to be placed around every
table data element, as a standoff, within each table cell.
|
<table width=200 border=1
cellpadding=20>
<tr>
<td width=100>
Cell 1
</td>
<td width=100>
Cell 2
</td>
</tr>
</table>
|
|
NOTE: Cell padding applies to the entire table. It forces the
amount of space defined to be placed around the content of each table element. This
is particularly useful for spacing table components for visibility and clarity but
can interfere with width assignments.
|
cellspacing =n
|
Table
|
Where n defines the space, in pixels, to be placed around every
table cell.
|
<table width=200 border=1
cellspacing=10>
<tr>
<td width=100>
Cell 1
</td>
<td width=100>
Cell 2
</td>
</tr>
</table>
|
|
NOTE: Cell spacing applies to the entire table. It forces the
amount of space defined to be placed around every table cell. This can also help
placement of data in text tables. If borders are used and the value for cell
spacing is greater than the value set for the border width, then the border becomes
as wide as the cell spacing. This can look odd (see the example above). Cell spacing
can also interfere with proper table width assignments.
|
colspan=n
|
Cells
|
Where n defines the number of columns a table cell will span.
|
<table width=200 border=1>
<tr>
<td width=100>
Cell 1
</td>
<td width=100>
Cell 2
</td>
</tr>
<tr>
<td width=200 colspan=2>
Cell 3 Spans 2 Columns
</td>
</tr>
</table>
| Cell 1 |
Cell 2 |
| Cell 3 Spans 2 Columns |
|
|
NOTE: Column spanning can only be set effectively in a
single table data cell
<td> and applies only to that cell.
|
height=nwidth=n
|
Table Rows Cells
|
Where n defines the widthof a table element in
absolute pixels or as a percent of the total space available for display or
the height of a table element in absolute pixels.
|
<table width=100% border=1>
<tr>
<td width=33%>33%</td>
<td width=66%>66%</td>
</tr>
</table>
|
|
NOTE: Table height and width parameters can apply to the
whole table, individual rows or individual cells. If no height and width
parameters are specified then the browser will compute the amount of space to
be allowed for each element based on a proportion of the space available for
display. This may yield unexpected and untidy results. It is better to control
display of table elements using these parameters.
|
nowrap
|
Cells
|
Prevents table text from being wrapped within the cell but can be
overridden using the <br> command (see
example at right).
|
<table width=200 border=1>
<tr>
<td nowrap>
Text here can't wrap<br>
to multiple lines.
</td>
</tr>
<tr>
<td>
Text here can wrap
to multiple lines.
</td>
</tr>
</table>
Text here can't wrap to multiple lines. |
Text here can wrap to multiple lines. |
|
|
NOTE: Preventing text from wrapping can lead to overlapping
or oddly placed table data (see example above).
|
rowspan=n
|
Cells
|
Where n defines the number of rows a table cell will span.
|
<table width=200 border=1>
<tr>
<td rowspan=2>
Cell 1<br>Spans 2
<br>Rows
</td>
<td>Cell 2</td>
</tr>
<tr>
<td>Cell 3</td>
</tr>
</table>
Cell 1 Spans 2 Rows |
Cell 2 |
| Cell 3 |
|
|
NOTE: Row spanning can only be set effectively in a
single table data cell
<td> and applies only to that cell.
|
valign=value
|
Rows and Cells
|
Where value defines the vertical alignment for the table element.
Allowed values are:
top, bottom and center
|
<table width=200 border=1>
<tr>
<td width=100 height=50
valign=top>
Top
</td>
<td width=100 height=50
valign=center>
Center
</td>
</tr>
</table>
|
|
NOTE: When applied to table rows (<tr>)
vertical alignment governs the entire row. When applied to table data
(<td>) alignment affects only the current table
cell.
|
Color in Tables:
The background color of any cell <td></td>
in a table can be specified using the bgcolor="#RRGGBB"
command parameter and argument. The basic form of the parameter syntax is shown below:
<td bgcolor="#RRGGBB">
Where:
RRGGBB specifies the red, green and blue components, respectively, of the
desired cell background color. Both Netscape and Internet Explorers at version
2.0 and higher can recognize and properly display tables which contain cells that
have color backgrounds specified using this method. The RR, GG
and BB color components are specified using hexadecimal code and can range
from 00 (zero) to FF (255). This allows a total of 2563,
or, over 16 million possible colors. Practically speaking, however, the color
palette is often reduced to 256 either by the user's display capability or by the
browser. Unless the screen settings allow sufficient color depth the browser
may "dither" (reduce to a cartoon-like mosaic of color dots) fine gradations of color
into only a standard palette of 216 colors using only six possible hexadecimal values
for each color - 00, 33, 66, 99, CC, and FF.
This yields only 63, or, 216 possible colors.
See a discussion of standard color tables
which uses the table background color capabilities.
For a full discussion on specifying color using HTML see course segment
Color in HTML.
Difficulties with Color in Tables
In addition to the possible dithering problem alluded to in the discussion above,
which can produce unsightly stepped gradient effects,
color backgrounds in tables can produce some peculiar results under some conditions.
If a table containing cells with color backgrounds is laid over a page which uses a background
image, the table color overlies and masks the underlying page background image. If,
however, a transparent GIF98a image is placed as an inline graphic within a table cell
with a color background, the background image of the page may show through the transparent
color index of the image rather than the intended background color of the table cell in
which it was placed.
By default browsers do not print color backgrounds but render them as white on printed
output from a webpage. If white or very light text is used on a dark table cell
background for effect in a webpage, it may print as white-on-white, which is to say,
it won't print at all.
Some less than admirable webpage designers may use "invisible" tables, that is, tables
which contain cells that use the same background and text colors, in order to cram as many
keywords onto the end of a website as possible while at the same time concealing them from
their users. Even though the users cannot see the additional text because it is
the same color as the background on which it is displayed, they must still wait while
it loads. The WWW search engines, however, can and do read such hidden words.
The aim, of course, is to "beef up" the webpage with a sometimes very lengthy invisible
keyword list in the hope of earning a higher hit ranking for the webpage from the
search engines. The practice is dishonest, wasteful of the principal Net resource,
bandwith, and exceptionally punitive for Net users with slow dial-up connections.
Tricks with Tables:
Every cell in a table can, and does, contain a complete HTML world. No text or color
formatting can carry over from cell to cell. Each cell is an island. A cell can
contain another table:
Note that this can produce double-bordered cells which may be useful for emphasis.
You can create a web page with two or more columns of text, with or without borders,
using a table:
Two Poems by Emily Dickinson
It dropped so low in my regard
I heard it hit the ground,
And go to pieces on the stones
At bottom of my mind;
Yet blamed the fate that fractured, less
Than I reviled myself
For entertaining plated wares
Upon my silver shelf.
|
I'm nobody! Who are you?
Are you nobody, too?
There's two of us -- don't tell!
They'd banish us -- you know!
How dreary to be somebody!
How public like a frog
To sing one's name the livelong day
To an admiring bog!
|
Perhaps the most useful thing you can do with a table is to center justify a single
column of text, or, you're whole WWW page, within a table with only one cell.
At the standard screen resolution of 640X480 your page may display perfectly
with all alignments looking good, but if a user comes in at 800X600 then alignments
can be disrupted unless the entire page is precisely placed within a single,
centered, 600-pixel wide table cell. The trouble with this is that a table cannot
display until all of its components are loaded. You may wish to place only text
and relatively small inline images into centered single-cell tables:
A Single-Celled Centered Table
The giant panda, a rare and closely protected animal, lives in the cool, damp
bamboo forests of mountainous central China, generally at elevations of 1,500 to
4,000 m (5,000 to 13,000 ft). Its thick, woolly coat is black or brownish black
and yellowish white; the darker color forms patches around the eyes, covers the
ears, legs, and chest, and forms a band across the shoulders. Giant pandas
grow to 1.5 m (5 ft) long, plus a short tail, and weigh 150 kg (330 lb) or more.
Bamboo constitutes most of their diet, but they may also feed on other plants
and even an occasional small animal. Mating takes place in April or May, and
one or two young are born, usually in January.
|
|