The Garsonix guide to HTML

Chapter 6: Adding Tables.

Having a good layout is very important on a web page and just being able to centre things is quite restrictive. Luckily tables provide a way to precisely position things on our page. The actual HTML is not very hard, but you have to carefully plan your layout, or it could all go pear shaped. Another use for tables is to display statistics in a table, a bit like a spreadsheet. I'll show you how to do that first because it's easier to explain.

Using a Table to Display Statistics.

The first thing you have to do when you want to start a table is to declare it, like this:

<TABLE WIDTH=100% BORDER=1 CELLPADDING=2>

WIDTH=100% makes the table fill the whole width of the screen, if you want it to fill less you put a lower percentage in.
BORDER=1 gives the table a thin border. A higher number would make it thicker where as BORDER=0 would make it invisible, leaving it out also makes them invisible.
CELLPADDING=2 makes the writing in the table appear 2 pixels away from the edge of its cell, you can play about with this or leave it out.

Tables are made from cells, these are the individual boxes. These occur on different rows. To start a new cell you use the <TD> tag, and to go on to the next row you use the <TR> tag. Now that you're learning about tables you should be able to tell that the example boxes are actually a table with a width of 80%, a border of one and just one cell. Here is an example of a table that shows some interesting statistics about my sock drawer. I've made up all the figures by the way. I'm not that interested in socks.

<HTML>
<HEAD>
<TITLE>Tables.</TITLE>
</HEAD>
<BODY>

<TABLE WIDTH=100% BORDER=1 CELLPADDING=2>
<TR><TD> Colour
<TD>Pattern
<TD>Material
<TD>Number of this type
<TR><TD> Red<TD>Zig-zaggy<TD>wool<TD>356
<TR><TD> Blue<TD>squares<TD>cotton<TD>1109867
<TR><TD> Yellow<TD>Zig-zaggy<TD>metal<TD>1
</TABLE>

</BODY>
</HTML>

Display this example.

It's important to end your table, not doing so can sometimes really mess up your page. The hard thing about tables is actually knowing where everything is going to appear. You work across each row from left to right and then do the same on the next row down. Laying your html file out neatly is important so you can keep track of everything.

We have a lot of control over tables and are able to change the width of each column and also align text to centre it or make it justify to the right. We'll use the example before but make some changes; I've shortened it to only two rows so our example doesn't get too long. Here is a list of what we are going to do.

<HTML>
<HEAD>
<TITLE>Tables.</TITLE>
</HEAD>
<BODY>

<TABLE WIDTH=100% BORDER=1 CELLPADDING=2>
<TR><TD WIDTH=10%> Colour
<TD ALIGN="middle">Pattern
<TD ALIGN="middle">Material
<TD AlIGN="right">Number of this type

<TR><TD> Red
<TD ALIGN="middle">Zig-zaggy
<TD ALIGN="middle">wool
<TD ALIGN="right">356
</TABLE>

</BODY>
</HTML>

Display this example.

If you like you can give each column a width but these all have to add up to the full column width that you set in the table declaration. All the alterations we have made to this table are done by extending the <TD> tag that starts a new cell. Changing the width of the column is done by adding the parameter WIDTH= followed by the percentage that you would like it to fill. You can see this done in the example above. You can also change the alignment of the text by adding the parameter ALIGN=" ", between the " " you can choose between left, middle and right. If you leave this parameter out left is standard.

Using a Table to Arrange Your Page.

The easiest way to give your page a nice layout is to add columns, to give it a magazine or newspaper feel. You need to make the table borders invisible and create a table with three columns. Here is the example:

<HTML>
<HEAD>
<TITLE>Tables.</TITLE>
</HEAD>
<BODY>

<TABLE WIDTH=100% BORDER=0 CELLPADDING=2>
<TR><TD>
Lots of pointless text etc.
<TD>
Lots of pointless text etc.
<TD>
Lots of pointless text etc.
</TABLE>

</BODY>
</HTML>

Display this example.

As you should see from this example it gives us the desired effect. A problem occurs when one column has less writing in than the others do. For a start in the example above the column with less writing in it would become thinner. This can be solved by giving each column a width using the WIDTH= parameter. Then what happens is the writing in that column is in the middle of that column rather than starting at the top. This is easy to sort out and we use a similar parameter to ALIGN, it is VALIGN which stands for vertical align. So if you want the writing to start at the top of the cell you add VALIGN="top", you can also have middle or bottom.

Sometimes you need to add a title that spans across all the columns under it. This is like the headline of a newspaper. This is in fact very easy to do. All you need to do is add the parameter COLSPAN= into the cell that you want to take up the room of more than one cell. The number you add after the = is how many columns you want it to take up. We'll use our three column example from before and add an extra cell to the top with a big title.

<HTML>
<HEAD>
<TITLE>Tables.</TITLE>
</HEAD>
<BODY>

<TABLE WIDTH=100% BORDER=0 CELLPADDING=2>
<TR><TD COLSPAN=3 ALIGN="middle">
<H1>An Example of Column Spanning.</H1>
<TR><TD>
Lots of pointless text etc.
<TD>
Lots of pointless text etc.
<TD>
Lots of pointless text etc.
</TABLE>

</BODY>
</HTML>

Display this example.

Of course another way to achieve this affect would have been to start the table after you had written the large title, but there are times when you do need COLSPAN. As well as colspan there is also rowspan. This is less used than colspan but there are times when it is useful. Here is an example of it in action.

<HTML>
<HEAD>
<TITLE>Tables.</TITLE>
</HEAD>
<BODY>

<TABLE WIDTH=100% BORDER=1 CELLPADDING=2>
<TR><TD ROWSPAN=2>
Lots of pointless text in a cell spanning two rows.
<TD>
Lots more pointless text in a normal cell.
<TR><TD>
Lots more pointless text in a normal cell.
</TABLE>

</BODY>
</HTML>

Display this example.

Adding Colour to Tables.

This is a new feature and only works in version three of Netscape and Microsoft Internet Explorer. It is a nice feature though. It allows us to make a table a different colour from the background. This is done by using adding the standard COLOR=" " parameter which we first came across in chapter 2. If you do this in the main table declaration at the top the whole table becomes that colour. If you put it in individual <TD> tags each cell can be a different colour. When used carefully this could really add to a table.

That is about everything on tables. They can be quite tricky but with careful planning they should not be too much trouble and they give some great benefits. In the next chapter we are going to learn about forms.

Return to contents page.
Go on to next chapter.

A GARSONIX WEB SITE: ©1997.