|
Virginia's HTML—Tables |
|
Martin Rinehart's |
Java Consultant Site | Home of the Decaf Project |
When you relize that most things can also hold most things the top of this page (light blue-gray) makes sense. It's another table that has two elements (with no border)—the link to the contents and this page's title. The hyperlink is itself another table of one row and one column (with a border), the contents being the hyperlink to the contents page. Here's what it would look like if I turned the border on.
|
Virginia's HTML—Tables |
I regularly use tables as the elements of other tables. You can do this too if you learn just the three most basic table tags.
|
<TABLE ALIGN="center" BGCOLOR=#c0c0f0 BORDER=1 CELLPADDING=5>  <TR>        <TD>           row 1, datum 1        </TD>        <TD>           row 1, datum 2        </TD>    </TR>    <TR>        <TD>           row 2, datum 1        </TD>        <TD>           row 2, datum 2        </TD>    </TR> </TABLE> |
The BORDER=1 attribute is the narrowest border. (BORDER=0 turns the border off.) The CELLPADDING attribute is the number of pixels between the cells' edges and their contents. (5 is generous; 2 is cramped.) Study the example until you understand how a table is built from rows and rows are built from table data. This is the result.
| row 1, datum 1 | row 1, datum 2 |
| row 2, datum 1 | row 2, datum 2 |
| row 1, datum 1 | row 1, datum 2 |
| row 2, datum 1 | row 2, datum 2 |
| row 1, datum 1 | row 1, datum 2 |
| row 2, datum 1 | row 2, datum 2 |
This is almost never what you want when you have text in your cells. It may be just right if your cells are holding images. (We'll get to images tomorrow.)
| 0 | 2 (default) | 5 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
A good rule is to specify CELLPADDING explicitly but don't include a CELLSPACING unless you really need to go for some special effect.
|
row 1, datum 1 |
row 1, datum 2 |
| row 2, datum 1 | row 2, datum 2 |
You see that the browser supplies the height needed to fit the content. You seldom need to specify a height. In fact, you usually don't want to specify a height because different users' browsers will be set to different widths, so the text contents of your table's cells will be wrapped differently. If you trust your browsers it will probably come out just fine.
Sometimes though, such as right now when I'm going to segué into the VALIGN attribute, it's handy to specify a height. Here I'm going to get that <P> tag out of the first cell and specify <TD HEIGHT=100>, instead.
| row 1, datum 1 | row 1, datum 2 |
| row 2, datum 1 | row 2, datum 2 |
You can't specify a HEIGHT for the row, unfortunately, but you can get the same effect by specifying the height of the first table datum in the row. As you can see, the default vertical alignment is in the middle.
| VALIGN omitted | VALIGN="middle" |
| VALIGN="top" | VALIGN="bottom" |
| <TABLE WIDTH=75%> | ||
| <TD WIDTH=150> | <TD WIDTH=50%> | <TD> |
The table gets 75% of the browser window. The first table datum in the next row is 150 pixels wide. The second is 50% of the row's width and the last gets whatever space remains. Try resizing your browser window to see how this works dynamically.
|
<TABLE>
<TR>
<TR> </TABLE> |
The COLSPAN attribute makes a cell span multiple columns. Similarly, the ROWSPAN . . . Sorry. It should be perfectly obvious so I'll shut up.
One hint, though. When there's a ROWSPAN above, there's an implicit <TD> and </TD> pushed down into the row(s) below. If that's confusing, try COLSPANs before you go on to ROWSPANs.
Warning: this stuff can get tricky. Very tricky when you mix different COLSPAN and ROWSPAN attributes.
| top | ||
| left | row 2 | |
| row 3 | last cell |
|
It is also very, very satisfying when you get a tricky one worked out to show the exact result you wanted. So your assignment is to do just that. Put a table in one of your pages and then work through this section, top to bottom, beginning with a plain, boring table and then work in every feature you've seen here until you have a mind-blowing table. (Or two or three.)
If you think you're getting the hang of this HTML stuff, try putting a table between the <LI> and </LI> tags of an ordered or unordered list. Try putting a list between the <TD> and </TD> tags of a table's row. Try putting a list that includes a table into another table.
I look forward to seeing it!
And if you've followed this far, you've learned everything you need to know to create every page you've seen in this tutorial. Tomorrow I'll show you how to use images. They're much easier than tables.