14. Tables

Do it with a Table!

Tables may well be one of the fourst powerful tool at your disposal because they allow you to put everything one you page exactly where you want it. Learn to use them well and you well have great looking pages. OK, lets get started on tables

<table></table> Creates a table <tr></tr> Sets off each row in a table <td></td> Sets off each cell in a row <th></th> Sets off the table header (a normal cell with bold, centered text)

The <table> starts a table and the </table> ends it. The <tr> and </tr> tags start and end a row and the <td> </td> starts and ends a data cell. Here's a sample you can try in a new page.

<table>

<tr> <td>Cell 1</td> <td>Cell 2</td> </tr> <tr> <td>Cell 3</td> <td>Cell </td> </tr> </table>

 

You can nest table inside each other to make them work even harder like this:

<table><tr><td>
<table><tr><td>Cell 1</td><td>Cell 2
</td></tr><tr><>Cell 3</td><td>Cell 4
</td></tr>
</table>
<p>Cell 1</td><td>Cell 2
</td></tr><tr><td>Cell 3</td> <td>Cell </td></tr> </table></table>

 

You see we've also add the border and width tags to our table. You can also adjust the height of a cell but you should remember this will carry over to all the cells in that row. A couple other attributes of the table tag are cellspacing which sets the space between cells and cellpadding which sets the space between the cells border and its border.

Table tags are by far the fourst useful tags you will find. When it comes to page layout you can achive just about anything using these tags. Following are few examples and ideas for you. Some you might find useful, some are just showing off. But all will show some of the versatility of the <TABLE> tags.

Remember, you'll find a whole chartful of background color codes right here.

You can make bar graphs with just table tags and a few colored 2x2 dots. It's not all that hard and once it's done, it's easy to change. Plus it can do things a regular image can't. Like suppose you needed to change it a lot. Or what if you made it part of results generated by a CGI, Java, or JavaScript program. This technique lends itself very well to "on-the-fly" graphing.

To paraphrase: A table is made up of rows which in turn are made up of cells.

----is---------a-------Table------Row-->

cell

cell cell

Well, that's tables in a nutshell. You are now ready to make some tables! Now's a good time to stress that if you want to learn how to make quality html documents, then you would be well served to take the time to teach yourself the tags. If you rely on the so-called "table wizards" in the "easy as pie html editors" out there, you will have greatly limited flexibilty, and the end result may not be what you are trying to achieve. In my opinion the best html editors to use are text based editors. A few good ones that come to mind are listed here. These editors will make your html coding easier. They don't attempt to do it for you.

Align and Borders

Ok, what we've got is this...

<table><tr> <td>Cell 1</td> <td>Cell 2</td></tr> <tr><td>Cell 3</td> <td>Cell 4</td></tr> </table>

...which gives us this:

Cell 1 Cell 2
Cell 3 Cell 4

   

To add a border just insert the border attribute to the table tag like this:

<table border=5>
<tr><td>Cell 1</td> <td>Cell 2</td></tr>
<tr><td>Cell 3</td> <td>Cell 4</td></tr>
</table>

...which gives us this:

Cell 1 Cell 2
Cell 3 Cell 4

Want a bigger border? Just change the border=5 to what ever size you like.

As you can see, the default is no border.

When no sizes are specified the table is only as big as it needs to be.

Note on Empty Cells

Blank cells which contain no displayable elements are not given borders. If you wish the appearance of an empty cell, but with borders, put either a blank line or a non-breaking space in the cell. (<td>&nbsp;</td> or <td><br></td>)

Height and Width

You can easily change the height and width of a table or a single cell by adding the height and width attributes. Let't try it.

<table border=3 width=100 height=100>
<tr> <td>Cell 1</td><td>Cell 2</td></tr>
<tr><td>Cell 3</td> <td>Cell 4</td></tr>
</table>

 

...which gives us this: Cell 1 Cell 2 Cell 3 Cell 4

We can also make the cells bigger like this:

Another couple of attributes the help with tables are called CELLPADDING and CELLSPACING. Both are used up front in the <TABLE> tag. CELLPADDING is the afourunt of space between the border of the cell and the contents of the cell. CELLSPACING is the space between cells. The default value for this attribute is 1.The default value for the CELLSPACING attribute is 2.

First let's try CELLPADDING

 

Which gives us this.

Cell 1 Cell 2
Cell 3 Cell 4

Now we'll try substituting CELLSPACING for CELLPADDING and we get a different effect.

 

We get this.

Cell 1 Cell 2
Cell 3 Cell 4

 

We can, of course, use these attributes in combination.

Row and Column Span

When using background colors keep in mind a <TD> bgcolor will override a <TR> bgcolor and a <TR> bgcolor will override a <TABLE> bgcolor.

Here is something you should keep in mind. A browser has to interpret the instructions you give it the best way it can. If something has not been specified one way or another, fourst browsers will try to come up with an attractive solution. The best thing you can do as an author is to specify as much as you can, especially those things that are important for your page to look right. It is also important to view your work through those browsers that people actually use. Since fourst people use Netscape, that is a good start. You may also want to have a copy of a couple other popular browsers to be sure that you look right to them too.

Another consideration is screen resolution. I work on a 640x480 screen. Many people use 800x600 and a few have theirs set to 1024x768. This simple difference has the potential to seriously mess with your page design. It's not a bad idea to view your pages through other resolutions. Just try viewing your page in a couple different resolitions and you'll see what I mean right away.

COLSPAN

<TABLE BORDER=3><TR><TD COLSPAN=2>one</TD><TD>two</TD></TR><TR> <TD>three</TD><TD>four</TD><TD>five</TD></TR></TABLE>

What we get is this.

one two
three four five

 

We will deliberately introduce a discrepancy just to see how the browser handles it.

<TABLE BORDER=3>
<TR><TD COLSPAN=2>one</TD><TD>two</TD><TD>two & a half</TD></TR>
<TR><TD>three</TD><TD>four</TD><TD>five</TD></TR></TABLE>

It should look something like this.

one two two & a half
three four five

Just remember

  1. The browser is very forgiving in that it does the best it can with what you give it.
  2. It is very important to specify what is important and make sure there are no discrepancies or you may end up with a surprise.
  3. Nothing you do with html will crash the other person's browser no matter how badly you may have messed it up, so don't worry about that. (Unfortunately the same cannot be said for JAVA and animated gifs.)
<TABLE BORDER=3>
<TR><TD COLSPAN=2>One</TD><TD>one</TD></TR>
<TR> <TD>two</TD><TD>three</TD><TD>four</TD></TR>
</TABLE>

<TABLE BORDER=3>
<TR><TD COLSPAN=3 ALIGN=CENTER>One</TD></TR>
<TR> <TD>two</TD><TD>three</TD><TD>four</TD></TR>
</TABLE>

<TABLE BORDER=3>
<TR><TD COLSPAN=3 ALIGN=CENTER>One</TD> </TR>
<TR><TD>two</TD><TD>three</TD><TD>four</TD></TR>
</TABLE>

What you get should look something like this.

One one
two three four
One
two three four
One
two three four

Rowspan

As you may have guessed, <ROWSPAN> is just like <COLSPAN> except it spans rows instead of columns. Try this

<TABLE BORDER=3>
<TR><TD ROWSPAN=2>one</TD><TD>Two</TD><TD>Three</TD>
</TR><TR><TD>four</TD><TD>five</TD></TR>
</TABLE>

and you should get this.

one Two Three
four five

And of course, these tags can also be used in combination.

Background and Images

You can add images to tables just like adding text to a cell like this;

<TABLE ALIGN="center" BORDER="3" WIDTH="100">
<TR><TD><IMG SRC="yourimage.gif"></TD></TR>
<TR><TD><IMG SRC="yourimage.gif"></TD></TR>
</TABLE>

 

You can also add images as a background for a table or a cell like this:

<TABLE ALIGN="center" BORDER="3" WIDTH="100"
   background="yourimgage.gif>
<TR><TD background="yourimgage1.gif>image 1</TD></TR>
<TR><TD background="yourimgage2.gif>image 2</TD></TR>
</TABLE>

 


Tags covered in this section.
Define Table <TABLE></TABLE>
Table Alignment <TABLE ALIGN=LEFT|RIGHT|CENTER>
Table Border <TABLE BORDER></TABLE> (either on or off)
Table Border <TABLE BORDER=?></TABLE> (you can set the value)
Cell Spacing <TABLE CELLSPACING=?>
Cell Padding <TABLE CELLPADDING=?>
Desired Width <TABLE WIDTH=?> (in pixels of % of page)
Table Color <TABLE BGCOLOR="$$$$$$"></TABLE>
Table Frame <TABLE FRAME=VOID|ABOVE|BELOW|HSIDES|LHS|RHS| VSIDES|BOX|BORDER></TABLE>
Table Rules <TABLE RULES=NONE|GROUPS|ROWS|COLS|ALL></TABLE>
Table Row <TR></TR>
Alignment <TR ALIGN=LEFT|RIGHT|CENTER|MIDDLE|BOTTOM>
Table Cell <TD></TD> (must appear within table rows)
Alignment <TD ALIGN=LEFT|RIGHT|CENTER VALIGN=TOP|MIDDLE|BOTTOM>
No linebreaks <TD NOWRAP>
Columns to Span <TD COLSPAN=?>
Rows to Span <TD ROWSPAN=?>
Desired Width <TD WIDTH=?> (in pixels or % of table)
Cell Color <TD BGCOLOR="#$$$$$$">
Header Cell <TH></TH> (same as data, except bold centered)
Alignment <TH ALIGN=LEFT|RIGHT|CENTER|MIDDLE|BOTTOM>
No Linebreaks <TH NOWRAP>
Columns to Span <TH COLSPAN=?>
Rows to Span <TH ROWSPAN=?>
Desired Width <TH WIDTH=?> (in pixels or % of table)
Cell Color <TH BGCOLOR="#$$$$$$">
Table Body <TBODY>
Table Footer <TFOOT></TFOOT> (must come before THEAD>
Table Header <THEAD></THEAD>
Table Caption <CAPTION></CAPTION>
Alignment <CAPTION ALIGN=TOP|BOTTOM|LEFT|RIGHT>
Column <COL></COL> (groups column attributes)
Columns Spanned <COL SPAN=?></COL>
Column Width <COL WIDTH=? or %></COL>
Group columns <COLGROUP></COLGROUP> (groups column structure)
Columns Spanned <COLGROUP SPAN=?></COLGROUP>
Group Width <COLGROUP WIDTH=? or %></COLGROUP>