Adding Tables

 

We have all seen or worked with a spreadsheet at one time or another. But what does this have to do with tables? Well, the <TABLE></TABLE> tags will allow you to design your page to look like a spreadsheet, with columns and rows. Tables work a lot like HTML list tags, in that you must use the table container tag to hold together a group of tags that define each individual row.

Tags and Meaning

Description

Table Tags

<TABLE></TABLE>

The main container is the <TABLE></TABLE> tags, which enclose the following tags:
<TR></TR> - Table Row. Defines a horizontal row.
<TD></TD> - Table Data Cell or column.
<TH></TH> - table Header.

All the examples that follow have to be defined within the <BODY></BODY> tags. I have left out the rest of the HTML code to make the explaining process a little easier.

Every table needs at least one row, and of course every row has to have at least one table data cell. So let's create a table with one row and one data cell. But, we need something to put in that cell. Let's populate it with the following text: My first table.

<TABLE>
<TR>
<TD>My first table</TD>
</TR>
</TABLE>

And this is what the output will look like in your browser:

My first table

But you can't see the table. This is where the BORDER attribute of the <TABLE></TABLE> tags come in. BORDER defines the width of the border surrounding the table. The value for BORDER is a number in pixels (e.g. BORDER="5"). The default value is 0 pixels, and is defined as follows:

<TABLE BORDER="1">
<TR>
<TD>My first table</TD>
</TR>
</TABLE>

And there you have it, a table with a border:

My first table

The border can be made bigger or wider if you want, simply enter a bigger amount for the BORDER attribute, like so:

<TABLE BORDER="15">
<TR>
<TD>My first table</TD>
</TR>
</TABLE>

And the output for this will look as follows:

My first table

When no sizes are specified the table is only as big as it needs to be. The WIDTH and HEIGHT attributes however, will let you specify a custom size for your table. Note however, width and height can be entered as a percentage (WIDTH="50%") or pixel (HEIGHT="100") value:

<TABLE BORDER="1" HEIGHT="50" WIDTH="50%">
<TR>
<TD>My first table</TD>
</TR>
</TABLE>

And this is what the output will look like:

My first table

We have established that <TR></TR> defines the rows of a table. That would mean that if you wanted two rows in your table, you would have to define it as follows:

<TABLE BORDER="1">
<TR>
<TD>First Row</TD>
</TR>
<TR>
<TD>Second Row</TD>
</TR>
</TABLE>

And the output will look like this:

First Row
Second Row

This means that for every row you want, you have to define a new set of <TR></TR> tags. The same goes for the <TD></TD> tags, which by adding a new set of these tags will create a new column in the row it is specified in. So let's make a table with two rows and two columns each:

<TABLE BORDER="1">
<TR>
<TD>First Row Column One</TD>
<TD>First Row Column Two</TD>
</TR>
<TR>
<TD>Second Row Column One</TD>
<TD>Second Row Column Two</TD>
</TR>
</TABLE>

And the output for this will look as follows:

First Row Column One First Row Column Two
Second Row Column One Second Row Column Two

Other attributes of the <TABLE></TABLE> tags are: CELLSPACING, CELLPADDING, BGCOLOR, BORDERCOLOR, BORDERCOLORLIGHT, BORDERCOLORDARK, BACKGROUND and ALIGN. Note however, that it's not necessary to use any of the attributes for your table.

CELLSPACING tells the browser how much space to include between the walls of the table and between individual cells. The value is a number in pixels (e.g. CELLSPACING="5"). Let's take the previous example and set its CELLSPACING equal to 5. Note that I'm only showing the line of code that is going to change, so the rest of the tags to build up the table will be left out for the sake of clarity and simplicity, and don't forget the closing table tag </TABLE>.

<TABLE BORDER="1" CELLSPACING="5">

And this is what the output will look like in your browser:

First Row Column One First Row Column Two
Second Row Column One Second Row Column Two

CELLPADDING tells the browser how much space to give data elements away from the walls of the cell. The value is a number in pixels (e.g. CELLPADDING="5").

<TABLE BORDER="1" CELLPADDING="15">

And this is what the output will look like in your browser:

First Row Column One First Row Column Two
Second Row Column One Second Row Column Two

You can use more than one attribute at a time, so let's put in both CELLSPACING and CELLPADDING.

<TABLE BORDER="1" CELLSPACING="10" CELLPADDING="15">

And this is what the output will look like in your browser:

First Row Column One First Row Column Two
Second Row Column One Second Row Column Two

BGCOLOR lets you specify a background color for your table, and only the table area will be filled with the specified color. The value of BGCOLOR can either be just the color name like green, red, yellow or a color code like #008000.

<TABLE BORDER="1" BGCOLOR="#008080">

And this is what the output will look like in your browser:

First Row Column One First Row Column Two
Second Row Column One Second Row Column Two

BACKGROUND works in much the same way as BGCOLOR, the only difference is that instead of specifying a color you give it a picture or image to use as a background. Let's use the following picture as a background for the table:

tblback.jpg (1704 bytes)

<TABLE BORDER="1" BACKGROUND="tblback.jpg">

And this is what the output will look like in your browser:

First Row Column One First Row Column Two
Second Row Column One Second Row Column Two

BORDERCOLOR does just what it says, it changes the color of the table's border. The value of BORDERCOLOR can either be just the color name like green, red, yellow or a color code like #008080.

<TABLE BORDER="1" BORDERCOLOR="#008080">

And this is what the output will look like in your browser:

First Row Column One First Row Column Two
Second Row Column One Second Row Column Two

BORDERCOLORLIGHT, BORDERCOLORDARK both work the same except that each one colors a different part of the table. The value of BORDERCOLORLIGHT, BORDERCOLORDARK can either be just the color name like green, red, yellow or a color code like #008080. To demonstrate the attributes a little better I'm going to increase the border size to 5. Let's start with BORDERCOLORLIGHT.

<TABLE BORDER="5" BORDERCOLORLIGHT="#008080">

And this is what the output will look like in your browser:

First Row Column One First Row Column Two
Second Row Column One Second Row Column Two

The following code is for BORDERCOLORDARK:

<TABLE BORDER="5" BORDERCOLORDARK="#004848">

And this is what the output will look like in your browser:

First Row Column One First Row Column Two
Second Row Column One Second Row Column Two

As you can see the BORDERCOLORLIGHT attribute colors the top and left part of the table while the BORDERCOLORDARK attribute colors the bottom and right part. So if we combined the two attributes together for one table, the following effect will be created:

<TABLE BORDER="5" BORDERCOLORLIGHT="#008080" BORDERCOLORDARK="#004848">

And this is what the output will look like in your browser:

First Row Column One First Row Column Two
Second Row Column One Second Row Column Two
ALIGN is used to determine where the table or chart will appear relative to the browser window or object. Valid values are LEFT, RIGHT or CENTER. If you want to center the table on a page it will be done as follows:
<TABLE BORDER="1" ALIGN="CENTER">
And this is what the output will look like in your browser:
First Row Column One First Row Column Two
Second Row Column One Second Row Column Two

Images can also be used and manipulated in a table data cell. Remember, in the absence of other instructions, the table will make itself just big enough to contain the data. The table simply expands to the proper size. Take the following for example:

<TABLE BORDER="3">
<TR>
<TD><IMG SRC="image.gif" WIDTH="52" HEIGHT="80"></TD>
</TR>
</TABLE>

And this is what the output will look like in your browser:

alignpic.gif (2393 bytes)

As you can see, if you play with the table attributes a little you can use a table to create the effect of a frame around a picture.

 

The <CAPTION></CAPTION> tags are used as a description or heading for the table. You can also nest other HTML tags within the <CAPTION></CAPTION> tags. The <CAPTION></CAPTION> tags have two attributes, ALIGN and VALIGN. The VALIGN attribute can only have one of two values TOP or BOTTOM. The default value is TOP. ALIGN is used to place the caption text horizontally on the table and valid values are LEFT, RIGHT or CENTER. To align the caption to the bottom left corner of the table, enter the following:
 
<TABLE BORDER="1">
<CAPTION ALIGN="LEFT" VALIGN="BOTTOM">Caption text</CAPTION>

And this is what the output will look like in your browser:

First Row Column One First Row Column Two
Second Row Column One Second Row Column Two
Caption text


I have shown you how to use all these attributes with the table, and this is all fine and well if you want to manipulate the table's appearance in any way. But what if you want to size or manipulate the data cells individually? BGCOLOR, BORDERCOLOR, BORDERCOLORLIGHT, BORDERCOLORDARK, BACKGROUND, ALIGN, VALIGN, WIDTH and HEIGHT are all attributes that work exactly the same as when used with the table but only changes the specified data cell.

The following table demonstrates the BGCOLOR, BORDERCOLOR, BORDERCOLORLIGHT, BORDERCOLORDARK and BACKGROUND attribute for individual data cells:

<TABLE BORDER="1">
<TR>
<TD BGCOLOR="#008080">BGCOLOR</TD>
<TD BORDERCOLOR="#000080">BORDERCOLOR</TD>
</TR>
<TR>
<TD BORDERCOLORLIGHT="#008080" BORDERCOLORDARK="#004848">BORDERCOLORLIGHT and DARK</TD>
<TD BACKGROUND="tblback.jpg">BACKGROUND</TD>
</TR>
</TABLE>

And this is what the output will look like in your browser:

BGCOLOR BORDERCOLOR
BORDERCOLORLIGHT and DARK BACKGROUND

Note however that when specifying any of the data cell attributes, in other words within the <TD></TD> tags, they will override the attributes specified for the table or <TABLE></TABLE> tags. So let's say you created a table with the background color set to turquoise and the one data cell was defined to have a background color Navy, this is what it will look like:

<TABLE BORDER="1" BGCOLOR="#008080">
<TR>
<TD BGCOLOR="#000080">First Row Column One</TD>
<TD>First Row Column Two</TD>
</TR>
<TR>
<TD>Second Row Column One</TD>
<TD>Second Row Column Two</TD>
</TR>
</TABLE>

And this is what the output will look like in your browser:

First Row Column One First Row Column Two
Second Row Column One Second Row Column Two

ALIGN and VALIGN used within the <TD></TD> tags will align the text or object within the data cell. ALIGN will place the text or object horizontally within the data cell. Valid values for the ALIGN attribute are LEFT, RIGHT or CENTER. VALIGN places the text vertically within the data cell. Valid values for the VALIGN attribute are TOP, MIDDLE, BASELINE or BOTTOM. Take a look at the following:

<TABLE BORDER="1" WIDTH="90%" HEIGHT="100">
<TR>
<TD ALIGN="LEFT" VALIGN="TOP">Left Top</TD>
<TD ALIGN="RIGHT" VALIGN="MIDDLE">Right Middle</TD>
<TD ALIGN="CENTER" VALIGN="BOTTOM">Center Bottom</TD>
<TD ALIGN="CENTER" VALIGN="BASELINE">Center Baseline</TD>
</TR>
</TABLE>

And this is what the output will look like in your browser:

Left Top Right Middle Center Bottom Center Baseline

WIDTH and HEIGHT used within the <TD></TD> tags sets just that, but only for the specified data cell. You can set the data cells individually like so:

<TABLE BORDER="1">
<TR>
<TD WIDTH="100" HEIGHT="30">Column One</TD>
<TD WIDTH="200" HEIGHT="30">Column Two</TD>
</TR>
</TABLE>

Note that if you set the HEIGHT of the first cell or column, the second cell or column is going to be the same height regardless of whether you have set its HEIGHT attribute to a different value. And this is what the output will look like in your browser:

Column One Column Two

As mentioned before the attribute settings of a cell or column will override the attribute setting of the table. In other words if you set the tables' WIDTH attribute equal to 100 and then specify the one cell or column to have a WIDTH of 60 and the other cell or column 50, the table will display in total at a width of 110 and not 100 as specified in the table's attribute.

Where it does come in handy to specify the WIDTH attribute of the table and then the WIDTH attributes of the cells or columns, is where you set the tables' WIDTH attribute equal to; let's say 400 pixels, and then set the first cell or column's WIDTH attribute equal to 70% and the second to 30%. This means the first cell or column will take up that percentage of the total width of the table and the second cell or column the rest, like so.

<TABLE BORDER="1" WIDTH="400">
<TR>
<TD WIDTH="70%" HEIGHT="30">Column One</TD>
<TD WIDTH="30%" HEIGHT="30">Column Two</TD>
</TR>
</TABLE>

And this is what the output will look like in your browser:

Column One Column Two

The <TH></TH> tags are used to define table headings. It works just like the <TD></TD> tags, the only difference being that the text defined within the <TH></TH> tags are automatically made darker or bold. Look at the following example:

<TABLE BORDER="1">
<TR>
<TH>Heading 1</TH>
<TH>Heading 2</TH>
</TR>
<TR>
<TD>Column Text 1</TD>
<TD>Column Text 2</TD>
</TR>
</TABLE>

And this is what the output will look like in your browser:

Heading 1 Heading 2
Column Text 1 Column Text 2

The <TD></TD> and <TH></TH> tags have two more attributes COLSPAN and ROWSPAN.

COLSPAN is used to force a cell to span more than one column. A valid value will be a number of columns not more than the amount of columns specified for one row in a table. In other words if the total columns for a table is 3, you can not set the value of COLSPAN equal to 4, only equal or less, the same applies for ROWSPAN. Look at the following example:

<TABLE BORDER="1" WIDTH="400">
<TR>
<TD COLSPAN="2">Column 1 plus Column 2</TD>
<TD>Column 3</TD>
</TR>
<TR>
<TD>Column 1</TD>
<TD>Column 2</TD>
<TD>Column 3</TD>
</TR>
</TABLE>

Note that the number you set COLSPAN to, is the amount of data cells you are combining as one big data cell. In other words, if you set COLSPAN to 2 as in the example and there are three data cells per row, then you only have to define the amount of outstanding data cells, in this case one more set of <TD></TD> tags, but if you had set COLSPAN equal to 3 you would not have defined any more data cells or <TD></TD> tags for the first row. And this is what the output of the example above will look like in your browser:

Column 1 plus Column 2 Column 3
Column 1 Column 2 Column 3

ROWSPAN is used to force a cell to span more than one row. The same rule as with the COLSPAN attribute applies here, regarding defining data cells. The only difference is that you work down and not across. In other words, if you set ROWSPAN equal to 2 and the table is defined to have three rows and three columns, you will still define three data columns or <TD></TD> tags in the row you defined ROWSPAN, but only have two data cells or <TD></TD> tags defined in the row the data cell expands to, since one data cell is going to be taken up. Take a look at the following example, and note how the second row only has one data cell defined.

<TABLE BORDER="1" WIDTH="400">
<TR>
<TD ROWSPAN="2">Row 1 plus Row 2</TD>
<TD>Row 1</TD>
</TR>
<TR>
<TD>Row 2</TD>
</TR>
<TR>
<TD>Row 3</TD>
<TD>Row 3</TD>
</TR>
</TABLE>

And this is what the output will look like in your browser:

Row 1 plus Row 2
Row 1
Row 2
Row 3 Row 3

And of course, the COLSPAN and ROWSPAN attributes can also be used in combination.

<TABLE BORDER="1" WIDTH="400">
<TR>
<TD ROWSPAN="2">Row 1 plus Row 2 Column 1</TD>
<TD COLSPAN="2">Row 1 Column 2 plus Column 3</TD>
</TR>
<TR>
<TD>Row 2 Column 2</TD>
<TD>Row 2 Column 3</TD>
</TR>
<TR>
<TD>Row 3 Column 1</TD>
<TD>Row 3 Column 2</TD>
<TD>Row 3 Column 3</TD>
</TR>
</TABLE>

And this is what the output will look like in your browser:

Row 1 plus Row 2
Column 1
Row 1 Column 2 plus Column 3
Row 2 Column 2 Row 2 Column 3
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3

Some HTML authors are omitting closing cell </TD>, </TR>, </TH> and </TABLE> tags. The idea is that the browser should know that when another cell begins, the last one must have ended. Unfortunately, as your tables become more complex, some browsers don't always understand this and the table goes awry. The easiest way to completely sidestep this issue is to always include those closing tags.

Only one more question remains, can we have a table within a table? Most definitely. Take a look at the following:

<TABLE BORDER="1" WIDTH="400" HEIGHT="100">
<TR>
<TD>
   <TABLE BORDER="1" WIDTH="200" HEIGHT="50">
   <TR>
   <TD>Table inside a table</TD>
   </TR>
   </TABLE>
</TD>
</TR>
</TABLE>

And this is what the output will look like in your browser:

Table inside a table

 

Here's a situation where a table can solve a common problem. You've got this great idea for an animated gif, but the picture you want to use is pretty big. Since you know that an animated gif is basically a series of Gif's displayed one after another, it will take a long time to load due to the web speed issue. Not only that, but because of its size, a viewer's browser is going to take forever trying to display it. One solution. Cut it up and display it inside a table. To demonstrate, the CELLSPACING attribute is set equal to 2.

dleft.jpg (6534 bytes) dtop.jpg (1412 bytes) dright.jpg (5398 bytes)
dmiddle.gif (19830 bytes)
dbottom.jpg (3697 bytes)

The only part of the picture that is animated are the eyes. This is the layout:

Left Picture

Top Middle Picture

Right Picture

Animated Picture

Bottom Middle Picture

And this is what the end result will look like in your browser with CELLSPACING set to zero.

dleft.jpg (6534 bytes) dtop.jpg (1412 bytes) dright.jpg (5398 bytes)
dmiddle.gif (19830 bytes)
dbottom.jpg (3697 bytes)

The code for the example above will look as follows:

<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD ROWSPAN="3" WIDTH="125" HEIGHT="203">
   <IMG SRC="dleft.jpg" WIDTH="125" HEIGHT="203">
</TD>
<TD WIDTH="61" HEIGHT="51">
   <IMG SRC="dtop.jpg" WIDTH="61" HEIGHT="55">
</TD>
<TD ROWSPAN="3" WIDTH="110" HEIGHT="203">
   <IMG SRC="dright.jpg" WIDTH="110" HEIGHT="203">
</TD>
</TR>
<TR>
<TD WIDTH="61" HEIGHT="25">
   <IMG SRC="dmiddle.gif" WIDTH="61" HEIGHT="25">
</TD>
</TR>
<TR>
<TD WIDTH="61" HEIGHT="123">
   <IMG SRC="dbottom.jpg" WIDTH="61" HEIGHT="123">
</TD>
</TR>
</TABLE>

Getting too specific with table cell dimensions is a tricky business, often the browser doesn't render a table exactly the way we have specified. The best way to overcome this is to design your table in such a way that minor differences in the way a browser renders it won't destroy what you're trying to do.

 

Lets design a Web page with all the new goodies we have learned about tables. For this exercise we are going to make a calendar page for the month of January 2000. Follow the instructions carefully. You may use other tags that we have done up to now as well to make the following exercise more interesting. Although the solution is available, try to create the Web page without having to look at the answer until after you've completed the exercise.

 

screen.gif (1270 bytes) Click here to see what the output of the above code would look like. To come back to this page when you've finished looking at the result, close the new window it opened in.
 
correct.gif (1081 bytes) Click here to see the solution to this exercise. To come back to this page when you've finished looking at the answer, close the new window it opened in.
 
output.gif (894 bytes) Click here to go to the personal homepage example, for this section of the tutorial. 

 

[Next Section - Creating Frames] [Back To Index Page]

Introduction | Basic Tags | More HTML Tags | List Tags | Adding Graphics | Creating Links | Adding Forms | Adding Tables
Creating Frames | Multimedia | Uploading Files | Downloads | Questions & Answers | Useful Links | e-mail Me
 
Example Page 1 | Example Page 2 | Example Page 3 | Example Page 4 | Example Page 5 | Example Page 6
Example Page 7 | Example Page 8 | Example Page 9 | Example Page 10
 
Color Chart | HTML Tester
bar.gif (1848 bytes)
Copyright © 1999 - 2000 Green Tentacle. All rights reserved. This tutorial is protected by SA and international copyright laws.
1