|
Tables Guide |
|
by Craig Walker |
After the basic table tags explainations, we will build a table and apply the different formating options to watch how they affect our table.
What is a table? A table is a collection of data cells arranged in columns and rows, like a spreadsheet. I use tables extensively in my web pages. They are useful for keeping your graphics from straying due to a different browser, monitor, or other setup that might skew your well planned display. With a table, you can assign an object or text to a cell, and since the table will always maintain it's proper structure, rows and columns, your data stays where you put it in relation to the other objects. Of course you need to put it there correctly. Below is a table of basic table tags.
| Tags | Function |
|---|---|
| <TABLE> | Start a table |
| <CAPTION> </CAPTION> | Start-Stop a table caption |
| <TR> </TR> | Start-Stop a New table row |
| <TH> </TH> | Start-Stop a Column Header |
| <TD> </TD> | Start-Stop a table data cell |
| </TABLE> | Stop a table |
|
BORDER=# (pixels)
CELLSPACING=# (pixels) CELLPADDING=# (pixels) HSPACE=# (pixels) VSPACE=# (pixels) |
BGCOLOR=xxxxxx
WIDTH=# (% or pixels) HEIGHT=# (% or pixels) ALIGN=RIGHT ALIGN=LEFT |
You can use any number of these options at one time, as long as they make logical sense together. You are the designer, you decide. In this code example, all options will be applied to the table. <TABLE BORDER=5 BGCOLOR=000066 HSPACE=25 VSPACE=25 ALIGN=RIGHT>
To center a table on the page, use the <CENTER> </CENTER> tags to enclose the whole table code.
We will apply each of these options to our table to see how they affect it.
I have heard the closing tag is optional. You can get away with leaving it out in most situations, but it is necessary in certain situations. My rule is to always use the closing tag.
Within the table row tag you can set certain row formats, like:
|
ALIGN=RIGHT
ALIGN=LEFT ALIGN=CENTER ALIGN=JUSTIFY BGCOLOR=xxxxxx |
VALIGN=TOP
VALIGN=BOTTOM VALIGN=MIDDLE WIDTH=# (% or pixels) HEIGHT=# (% or pixels) |
Text and graphics in a data cell can be formated using the same common tags used on a plain page, like <B>, <I>, <FONT=>, etc.
These are some of the common format options you can use within the TD tag:
|
ROWSPAN=# (rows)
ALIGN=RIGHT ALIGN=LEFT ALIGN=CENTER ALIGN=JUSTIFY BGCOLOR=xxxxxx |
COLSPAN=# (columns)
VALIGN=TOP VALIGN=BOTTOM VALIGN=MIDDLE WIDTH=# (% or pixels) HEIGHT=# (% or pixels) |
Let's build a table!
I want to list some of my favorite motorcycles I have owned.
The favorite motorcycles I have owned are, a 69 Penton Berkshire 125cc, a 72 CZ MX 250cc, and a 77 KTM MC5 250cc.
We can make that a whole lot easier and more fun for our viewers to read if we put that data in a table like this.
| My Favorite Motorcycles | ![]() |
|||
|---|---|---|---|---|
| Year | Make | Model | Size | |
| 69 | Penton | Berkshire | 125cc | |
| 72 | CZ | MX | 250cc | |
| 77 | KTM | MC5 | 250cc | |
But first we will start with a basic table like the one below and then walk through the format options that will bring us to the result above.
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
Below is the HTML code used to create the basic table.
<TABLE>
<TR>
<TH>Year</TH> <TH>Make</TH> <TH>Model</TH> <TH>Size</TH>
</TR>
<TR>
<TD>69</TD> <TD>Penton</TD> <TD>Berkshire</TD> <TD>125cc</TD>
</TR>
<TR>
<TD>72</TD> <TD>CZ</TD> <TD>MX</TD> <TD>250cc</TD>
</TR>
<TR>
<TD>77</TD> <TD>KTM</TD> <TD>MC5</TD> <TD>250cc</TD>
</TR>
</TABLE>
Notice, in the table code above, how I separated logical groups, table rows, so my code will be easy to check and edit. When the table data is significantly bigger, I put each data cell on it's own line, but still grouped as a table row. Another thing you can do is indent your code, outline fashion, to help recognize sections. Neither of these styles of grouping will affect how your web page looks to your viewer.
Now we start adding the options.
<TABLE>
<CAPTION><B>My Favorite Motorcycles</B></CAPTION>
<TR>
Table with caption, bold:
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
There are some differences you should be aware of between the way MS Internet Explorer and Netscape Communicator interpret BORDER colors. Netscape seems to take on the color of the page background where MS IE takes on the color of the table background. In this example the border color drabs out the table in MS IE. In netscape it is a little bit cheerier because of the brighter border color.
Another thing that came up when the background color was changed in MS IE. The background color extended up into the caption area. That looked real sloppy with the border around the table, but not the caption, and the background color extending past the border and into the caption region. This happened on MS IE at work but not on MS IE at home.
If the BGCOLOR thing creeps up into your border region, you might be happier formatting the BGCOLOR in each row, TR, tag instead of in the TABLE tag.
I recommend cross checking your pages on both browsers if you are serious about publishing them on the Internet.
<TABLE BORDER>
<CAPTION><B>My Favorite Motorcycles</B></CAPTION>
<TR>
<TH>Year</TH>
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
<TABLE BORDER=10>
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
<TABLE BORDER CELLPADDING=10>
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
<TABLE BORDER CELLSPACING=10>
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
WIDTH=# (pixels) WIDTH=#% (percent) The width of your table can be specified in either pixels or percent (of available space).
<TABLE BORDER HEIGHT=250 WIDTH=400>
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
When scaling your table to a percentage, it is helpful to know what it is a percentage of. You are specifying a percentage of the visible area in the current section you are working in. The reference area for these tables, which does not include the dark blue frame on the left or the indent area of this outline style list, is from the left edge of this paragraph to the vertical scrollbar on the right side of the window. Try this experiment. Reduce the size of your window. The table below scales down until it can't scale down past the size of the data because the visible area has shrunk.
You can also specify greater than 100% which will give you a wider page and cause a horizontal scroll bar to be added to your page. I hate that.
<TABLE BORDER HEIGHT=50% WIDTH=75%>
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
<TABLE BORDER ALIGN=RIGHT HEIGHT=200 WIDTH=350>
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
In the TR, TH, and TD tags it is used to align the data in the cell(s).
In the example below, I have used all of the ALIGN commands in different context to show how each one works. Also notice how the more local the command, the more priority it has. If a whole row was right aligned in the TR tag, but one cell was left justified, that one cell would be left aligned while the rest of the row stays with the right alignment.
<TABLE BORDER ALIGN=LEFT HEIGHT=200 WIDTH=350>
<TR>
<TH>Year</TH> <TH ALIGN=LEFT>Make</TH> <TH>Model</TH> <TH>Size</TH>
</TR>
<TR ALIGN=RIGHT>
<TD ALIGN=CENTER>69</TD> <TD>Penton</TD> <TD ALIGN=MIDDLE>Berkshire</TD> <TD>125cc</TD>
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
The TH tag for Make was left aligned. The default for a TH tag is centered.
The whole row for the first motorcycle was right aligned, but 2 individual cells were center/middle aligned. That is a good example of local priority.
I think this table would look nice with all cells, including the headers, left aligned or center aligned.
<TABLE BORDER HEIGHT=200 WIDTH=350>
<TR VALIGN=TOP>
<TH>Year</TH> <TH VALIGN=BOTTOM>Make</TH> <TH>Model</TH> <TH>Size</TH>
</TR>
<TR VALIGN=TOP ALIGN=RIGHT>
<TD VALIGN=BOTTOM ALIGN=RIGHT>69</TD> <TD>Penton</TD> <TD>Berkshire</TD> <TD>125cc</TD>
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
Note: I haven't been able to make this work in MS Internet Explorer yet.
HSPACE=# (pixels) and VSPACE=# (pixels) can be used together or separately. They specify a buffer width between the table and the other objects beside, above, or below. It keeps graphics or text from crowding the table.
This example uses the ALIGN=LEFT option to left align the whole table to the left side of the page. Check out the difference, referencing the ALIGN=LEFT examples, when you add horizonal and vertical spacing, HSPACE and VSPACE. Now you can read the text or the table without the other one cluttering up the view.
<TABLE BORDER BGCOLOR=66cc99 HEIGHT=150 WIDTH=400>
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
You can use the BGCOLOR command in either the TABLE, TR, TH, or TD tags to affect that portion of the table. When it comes to table formatting, the more local tag has the highest priority. Notice in the example below, the BGCOLOR for the whole table (table tag) is the green color, but the TR tag for the header row was coded for the yellow color. If I had specified any of the TH tags to be blue, then that cell would be blue.
<TABLE BORDER BGCOLOR=66cc99 HEIGHT=150 WIDTH=400>
<TR BGCOLOR=66cc99>
<TH>Year</TH><TH>Make</TH><TH>Model</TH><TH>Size</TH>
</TR>
<TR>
<TD BGCOLOR=00ccff>69</TD> <TD>Penton</TD> <TD>Berkshire</TD> <TD>125cc</TD>
| Year | Make | Model | Size |
|---|---|---|---|
| 69 | Penton | Berkshire | 125cc |
| 72 | CZ | MX | 250cc |
| 77 | KTM | MC5 | 250cc |
I'm going to remove the caption and place the same text in a row above the header row. The row will span all four columns by using the COLSPAN=4 command. I used the TH tag instead of the TD tag because it automatically centers and bolds the text, which is what I wanted.
Next I have added a picture to my table. To keep it large enough to view but not skew the whole table, I used the ROWSPAN=5 command. I put the picture in the top row of the table so it would start from there. I removed the WIDTH=350 setting and added CELLPADDING=10. That allows my table to be flexible without cramping the cells.
<TABLE BORDER CELLPADDING=10 BGCOLOR=66cc99 HEIGHT=150>
<TR BGCOLOR=ffff66>
<TH COLSPAN=4>My favorite Motorcycles</TH>
<TH ROWSPAN=5><IMG SRC="pictures/penton.gif"></TH>
</TR>
| My Favorite Motorcycles | ![]() |
|||
|---|---|---|---|---|
| Year | Make | Model | Size | |
| 69 | Penton | Berkshire | 125cc | |
| 72 | CZ | MX | 250cc | |
| 77 | KTM | MC5 | 250cc | |
Here is the complete code for the table above.
<CENTER>
<TABLE CELLPADDING=10 BORDER=5 BGCOLOR=66cc99 HEIGHT=150>
<TR BGCOLOR=ffff66>
<TH COLSPAN=4>My Favorite Motorcycles</TH>
<TH ROWSPAN=5><IMG SRC="pictures/penton.gif"></TH>
</TR>
<TR BGCOLOR=00ccff ALIGN=CENTER>
<TH>Year</TH> <TH>Make</TH> <TH>Model</TH> <TH>Size</TH>
</TR>
<TR ALIGN=CENTER>
<TD>69</TD> <TD>Penton</TD> <TD>Berkshire</TD> <TD>125cc</TD>
</TR>
<TR ALIGN=CENTER>
<TD>72</TD> <TD>CZ</TD> <TD>MX</TD> <TD>250cc</TD>
</TR>
<TR ALIGN=CENTER>
<TD>77</TD> <TD>KTM</TD> <TD>MC5</TD> <TD>250cc</TD>
</TR>
</TABLE>
</CENTER>
|
by Craig Walker |