Zen

Intro
Browser
The Page
Elements
Tables
Layout
Perfection


Homesite 101


The Secrets of Tables

Zen
Tables cause page builders more trouble than any other HTML feature, but once mastered, they can give you enormous power over your page layouts.

Two aspects make them difficult. You can't see very well what you're doing because you're writing tags to describe something you're really trying to draw. And, if you get parts of them wrong, you may not see anything at all! But the basics are very simple and even the most complex table structures are made up of those same simple pieces.

This is a basic table, made up of two rows and two data cells in each row:

Contents 1Contents 2
Contents 3Contents 4

Here's the HTML code (in black) that creates the table, with comments in green.

<TABLE> open the Table
<TR> open the Table Row 1
<TD> open the Table Data cell 1
Contents 1
</TD> close the Table Data cell 1
<TD> open the Table Data cell 2
Contents 2
</TD> close the Table Data cell 2
</TR> close the Table Row 1
<TR> open the Table Row 2
<TD> open the Table Data cell 1
Contents 3
</TD> close the Table Data cell 1
<TD> open the Table Data cell 2
Contents 4
</TD> close the Table Data cell 2
</TR> close the Table Row 2
</TABLE> close the Table

Well, you can see the problem.... a long string of <TR>'s and <TD>'s, and this is just for a simple table with four cells. You can imagine what a more complex table would be like. But, there are a couple of simple secrets that will keep everything straight and make you the master of tables.

Secret #1: Draw a picture of the table before you make it.

Here's a picture of the table, with the HTML tags to make it written in. Now you can see clearly how the table, the table rows, and the data cells open and close, and where the content will be inserted in the cells. To write this table, just copy the tags line by line, starting with the top <TR> line and inserting your text or image content in the cell(s) you want (between a <TD> open and a </TD> close).

OK, maybe you don't need to draw a picture to write this table, but you'll need them in our next meditation. Inside a table cell you can write another table, and sometimes you'll even nest three tables for precision layout control. If you can keep that all in your head, why are you reading this page?

Secret #2: Format and comment your HTML table tags.

You can run your table tags together like this, and many people do (even I do, occasionally, when it's a table I write all the time):

<TABLE><TR><TD> Contents 1</TD><TD> Contents 2</TD></TR><TR><TD> Contents 3</TD><TD> Contents 4</TD></TR></TABLE>

But if you use indenting, and write comments next to key parts, you can keep track of a complex table much more easily. Here's a fragment of indented and commented table code with one table inside of another:
<TABLE BORDER=1>
<! Open the master table -->
<TR><TD>  
<! Cell for the navigator -->
     <TABLE>  
     <! Open navigator table -->
     <TR><TD>
     <! Link to home here -->
     </TD></TR>
     <TR><TD>
     <! Link to favorites here --> 
     </TD></TR>
     </TABLE>  
     <! Close navigator table -->
</TD>
<! Close cell for the navigator -->
<TD>
<! Open Main Data cell -->
...etc
You can see that the indenting makes it very clear which <TR> and <TD> go with which table, and the comments make the entire code very readable.

Secret #3: Build your tables first, then insert your text or images inside.

In the code fragment just above, you can see that there isn't any content in the cells, just comments about what will go in later. When you forget to close a table tag, or put more data cells in one row than in another, you'll get strange results and all of the content after the table opening may not even show! If you've already got text or image calls mixed in with the table code, it can be very difficult to isolate the problem. When I'm building nested tables, I frequently build the outer table first, then the table(s) nested in the outer table cells, then insert the content in the inner table cells.

Also notice in the fragment that the table border has been turned on with the BORDER=1 attribute. Borders definitely make it easier to see what you're doing while building, even if you're going to make it invisible (BORDER=0) later. If you're using your table for fine layout control, though, remember that the extra pixels of your border will affect your spacing.

Zen

Still with me, grasshopper? This meditation is quite different from the previous ones, where we considered the concept more than the details. Now, I'm assuming that you have a good grasp of the concepts, have gained some experience, have a good HTML handbook at hand so that you can look up the details, and that you want to become a master.

Before we begin our next meditation, I suggest that you practice building some simple tables and putting images and text inside them, and then build some tables inside of tables (with images or text within them).

Then, when you're ready to go on, click here.

Dudette