thebroadroom.net


Tables







 
What is a table?

Structure
the basic table
adding a row
adding a cell
merging cells
columns

Space
width and height
padding the inside of the cells
adding space between the cells
placing a margin around text

Alignment
aligning the table
aligning data inside cells

Cosmetic
borders
background colors/using an image for a background

Misc.
fonts inside tables
keeping data inside the cells from wrapping




What is a table?

A table is one of the most important things you will ever learn how to do in HTML.

That said, a table is a structure. It has rows and columns. It's similar to an Excel spreadsheet or a Word for Windows table. Text and graphics go into the "cells" of the table.

Most HTML tables are invisible. That is, they have no borders, and no special background color. They are used solely to control where the text and graphics are placed on the page.

Can you make an HTML page without knowing how to do tables? Yes, you can, but you will soon become frustrated because you won't be able to place things exactly where you want them. It is much less work to learn how to do the tables up front, than it is trying to format HTML pages without them.

[ top ]


the basic table


<TABLE><TR><TD>
text and images go here...
</TD></TR></TABLE>


text and images go here...

Don't forget the closing table tag! Without it, you won't be able to see anything that you put inside the table. The whole chunk of text and graphics will disappear and you'll wonder what the...? happened.

<TABLE> as you will guess, signifies the start of the table. Everything inside the pair of Table tags will go inside the table.

<TR> stands for Table Row. Everything inside the pair of Table Row tags will go inside the same row of the table.

<TD> actually stands for Table Data. But that's confusing. I think of it as "table cell." Everything inside the pair of Table Data tags will go inside the same cell in the table.

The example looks pretty unspectacular. :) That's because you usually put more stuff inside a table.

[ top ]


adding a row


<TABLE><TR><TD>
first row...
</TD></TR>
<TR><TD>
second row...
</TD></TR>

</TABLE>

first row...
second row...

[ top ]


adding a cell


<TABLE><TR><TD>
first cell...
</TD> <TD>
second cell...
</TD>

</TR></TABLE>

first cell... second cell...

[ top ]


merging cells


<TABLE><TR><TD>
first cell...
</TD><TD>
second cell...
</TD></TR>
<TR><TD COLSPAN=2>
second row spans both cells
</TD></TR></TABLE>

first cell... second cell...
second row spans both cells

This is very important and can also be somewhat tricky. If I have more than one cell inside a table row, it affects all the rows in the table.

For example, suppose one row of my table contains 3 cells. All of the other rows in that table would either have to contain 3 cells, or have to use COLSPAN.

Here is an example of a correct table with a row containing 3 cells. A border has been placed around the cells to make the example clearer:


<TABLE BORDER=1><TR><TD>
first cell...
</TD><TD>
second cell...
</TD><TD>
third cell...
</TD></TR>
<TR><TD COLSPAN=3>
second row spans all 3 cells
</TD></TR>
<TR><TD COLSPAN=2>
third row, first cell, spans 2 cells
</TD><TD>
third row, second cell...
</TD></TR></TABLE>

first cell... second cell... third cell...
second row spans all 3 cells
third row, first cell, spans 2 cells third row, second cell...


[ top ]


columns

How do you make columns?

The short answer is this. If you have 2 cells in a table row, and you put all of your data--text, graphics, other tables, etc.--inside those 2 cells, you'll get 2 columns.

There's no way to have the data automatically flow into 2 columns. You have to become a typesetter. :) Divide the data in half. Put whatever you want in the first column into the first cell. Likewise, put everything you want in the second column into the second cell.

You can make as many columns as you like. Just add another cell to the table row.


<TABLE CELLSPACING=25><TR>
<TD>
first column...
<BR />yadda yadda yadda yadda yadda
<BR />yadda yadda yadda yadda yadda
<BR />yadda yadda yadda yadda yadda
<BR />yadda yadda yadda yadda yadda
<BR />yadda yadda yadda yadda yadda
</TD>
<TD>
second column...
<BR />yadda yadda yadda yadda yadda
<BR />yadda yadda yadda yadda yadda
<BR />yadda yadda yadda yadda yadda
<BR />yadda yadda yadda yadda yadda
<BR />yadda yadda yadda yadda yadda
</TD>
</TR></TABLE>

first column...
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
second column...
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda


[ top ]


width and height

You can set the width and height for the entire table. You can also set the height of a table row by setting the height of a cell inside it. Finally, you can set the height and width of a table cell.

Most of the time, you will not need to set the height of tables. Usually you let the data inside the table determine its height.

And sometimes you will not need to set the width of the table either. Tables automatically expand to fit their widest element. If the widest element is as wide as you want the table to be anyway, there's no need to set the table width.

There are two ways to set the width of a table: as a percentage, and as an absolute number of pixels:


<TABLE WIDTH="75%"><TR><TD>
This table is set to be 75% wide. That means it will expand in width to be 75% of the page it's on. If the table is inside a cell of another table, it will expand to be 75% of the width of the cell.
</TD></TR></TABLE>

This table is set to be 75% wide. That means it will expand in width to be 75% of the page it's on. If the table is inside a cell of another table, it will expand to be 75% of the width of the cell.


<TABLE WIDTH="200"><TR><TD>
This table is set to be 200 pixels wide. Sometimes you need an absolute pixel value for the width of a table. Most of the time, however, I use the percentage. The percentage seems to be better at adapting your HTML to different sizes and resolutions of monitors.
</TD></TR></TABLE>


This table is set to be 200 pixels wide. Sometimes you need an absolute pixel value for the width of a table. Most of the time, however, I use the percentage. The percentage seems to be better at adapting your HTML to different sizes and resolutions of monitors.

I don't usually set the height of rows or the width of cells. There are times when you'll need to do that though. For example, say you wanted all the rows in the table to be exactly the same height, or all the cells exactly the same width. Or you wanted the first cell in the table to be a certain width.


<TABLE BORDER=1>
<TR><TD HEIGHT="100">
The highest cell is 100 pixels high.
</TD><TD>
Therefore the row is 100 pixels high.
</TD></TR>
<TR><TD COLSPAN=2>
This row has no height set.
</TD></TR> </TABLE>

The highest cell is 100 pixels high. Therefore the row is 100 pixels high.
This row has no height set.


<TABLE WIDTH="100%" BORDER=1>
<TR><TD WIDTH="25%">
The first cell is 25% of the width of the table.
</TD><TD>
The second cell has to expand to fill the rest of the table.
</TD></TR> </TABLE>

The first cell is 25% of the width of the table. The second cell has to expand to fill the rest of the table.


[ top ]


padding the inside of the cells

Sometimes you want to put some space between the elements inside the cells and the borders of the cells.


<TABLE BORDER=1><TR><TD>
This table has no cellpadding.
</TD></TR></TABLE>

This table has no cellpadding.


<TABLE BORDER=1 CELLPADDING=15><TR><TD>
This table pads each cell with 15 pixels.
</TD></TR></TABLE>

This table pads each cell with 15 pixels.


[ top ]


adding space between the cells

Likewise, sometimes you want to put more space between the cells.


<TABLE BORDER=1><TR><TD>
This table has no cellspacing.
</TD><TD>
Sometimes you want cellspacing.
</TD></TR></TABLE>

This table has no cellspacing. Sometimes you want cellspacing.


<TABLE BORDER=1 CELLSPACING=20><TR><TD>
This table puts 20 pixels between each cell.
</TD><TD>
Usually you use this feature with invisible tables, to space out the data.
</TD></TR></TABLE>

This table puts 20 pixels between each cell. Usually you use this feature with invisible tables, to space out the data.


[ top ]


placing a margin around text

That's one of the first uses of the spacing features, described above, that you'll need to know. There are several ways to do it.

One is to set the width of the table to, say, 90%. That creates a margin on both sides of whatever's inside the table.

Another way is to set the cellpadding attribute of the table. This places a margin on all four sides of the table. If you have cells inside the table, of course they will also be padded.

Yet another way is to set the cellspacing attribute of the table. This places a margin on all four sides of the table and also places a margin around the cells inside the table.

More newfangled HTML programming uses coordinates to set the dimensions of areas on the page. I myself prefer the old-style table programming since you have more control over the placement of the elements on the page.

[ top ]


aligning the table

Tables can be aligned on a page. It is slightly tricky though. I've had my tables overlap other objects on the page. Other times it seems to work perfectly. So keep it in mind, and play with it.

Most of the time, I use centered tables. That's really easy:


<CENTER>
<TABLE><TR><TD>
text and images go here...
</TD></TR></TABLE>
</CENTER>


text and images go here...

It doesn't matter if the data inside the table is or isn't centered. Most of the time. you'll want the table itself centered on the page.

For a piece on Peru, the author contributed 5 pictures. I wanted to place the pictures in such a way that there would be links at the bottom of each picture.

The obvious way to do that would have to involve tables. At first I tried to put the pictures inside their own cells. I put up a table row, with a picture in one cell and some of the text in the adjacent cell.

This worked but it was hardly perfection. You couldn't wrap the text around the picture that way.

I decided to try putting the picture and its links inside a table. I used the ALIGN feature to left- and right- align the tables inside the text:

This worked...exactly the same way aligning an image would. The text wrapped around the tables.


<TABLE BORDER="1" ALIGN="right"><TR><TD>
text and images go here...
</TD></TR></TABLE>
text and images go here...




[ top ]


aligning data inside cells

Each cell has a horizontal and vertical alignment.

The default horizontal alignment is left. If you don't specify otherwise, the data inside the cell will be left-aligned.


<TABLE WIDTH="100%" BORDER="2"><TR>
<TD COLSPAN="2" ALIGN="center">
horizontal alignment = center
</TD></TR>
<TR><TD>
default alignment = left
</TD><TD ALIGN="right">
horizontal alignment = right
</TD></TR></TABLE>

horizontal alignment = center
default alignment = left horizontal alignment = right


The default vertical alignment is center. If you don't specify otherwise, the data inside the cell floats vertically to the middle of the cell.


<TABLE HEIGHT=150 WIDTH=400 BORDER="2"><TR>
<TD VALIGN="top">
vertical alignment = top
<BR>Data floats to the top of the cell.
</TD><TD>
default vertical alignment = center
<BR>Data floats to the middle of the cell.
</TD><TD VALIGN="bottom">
vertical alignment = bottom
<BR>Data floats to the bottom of the cell.
</TD></TR></TABLE>

vertical alignment = top
Data floats to the top of the cell.
default vertical alignment = center
Data floats to the middle of the cell.
vertical alignment = bottom
Data floats to the bottom of the cell.


[ top ]


borders

Tables, by default, have no borders. If you would like to have a border, use the BORDER attribute:


<TABLE BORDER="10"> <TR><TD>
first row...
</TD></TR>
<TR><TD>
second row...
</TD></TR> </TABLE>

first row...
second row...


Netscape and IE render borders differently. The Netscape rendition is much better. But they are similar enough.

You can also specify a border color:


<TABLE BORDER="10" BORDERCOLOR="red"><TR><TD>
text and images go here...
</TD></TR></TABLE>

text and images go here...


The cellspacing can affect the border:


<TABLE BORDER="1" CELLSPACING="0"><TR><TD>
text and images go here...
</TD></TR></TABLE>

text and images go here...


<TABLE BORDER="1" CELLSPACING="10"><TR><TD>
text and images go here...
</TD></TR></TABLE>

text and images go here...


[ top ]


background colors/using an image for a background

The old way to do this was to use the bgcolor attribute to set a background color, and the background attribute to tile the background of the table with an image--exactly the same way you would for the body of an HTML page.

The newer, and better, style is to use style sheets to control these attributes. See Style Sheets, Setting fonts and colors for tables.

You can also define a class and use that for individual tables, table rows, and table cells as follows:


style sheet:

.bgblue {
  background-color: #9966ff;
}



HTML page:

<TABLE><TR><TD class="bgblue">
table cell has a blue background
</TD></TR></TABLE>

table cell has a blue background


For reference purposes, here is the deprecated code to set the background color/image:


<TABLE BGCOLOR="#FFCCCC"><TR><TD>
table with pink background
</TD></TR></TABLE>

table with pink background


<TABLE BORDER=1>
<TR><TD>
table with alternating white and blue rows
</TD></TR>
<TR BGCOLOR="#CCCCFF"><TD>
table with alternating white and blue rows
</TD></TR>
<TR><TD>
table with alternating white and blue rows
</TD></TR> </TABLE>

table with alternating white and blue rows
table with alternating white and blue rows
table with alternating white and blue rows


<TABLE><TR>
<TD>
cell with a white background
</TD> <TD BGCOLOR="#99FF99">
cell with a green background
</TD> <TD>
cell with a white background
</TD> </TR></TABLE>

cell with a white background cell with a green background cell with a white background


Using an image as a background is the same thing:


<TABLE BORDER=1 HEIGHT=300 WIDTH=300 BACKGROUND="../../images/coffee_bg2.jpg"><TR><TD>
table is tiled with a coffee cup image
</TD></TR></TABLE>

table is tiled with a coffee cup image


[ top ]


fonts inside tables

Again, the better way to set fonts inside tables involves style sheets. See Style Sheets, Setting fonts and colors for tables.

Using the old way, you can't set the font for the entire table. You have to use inline <font> tags and place a set of them inside each table cell.

For reference purposes, here is the code for that:


<TABLE><TR>
<TD><FONT FACE="Impact">
Impact font
</FONT>
</TD>
<TD><FONT FACE="Verdana">
Verdana font
</FONT>
</TD>
</TR></TABLE>

Impact font Verdana font


[ top ]


keeping data inside the cells from wrapping

Use NOWRAP inside the cell:


<TABLE><TR> <TD NOWRAP>
Data inside this cell will not wrap. Be careful using this.
<BR />You can get an enormous table.
</TD> <TD>
Default value is to let data inside the cell wrap. Sometimes that's not desirable. IE is really weird about that. Sometimes it'll add in an extra new line out of nowhere and you have to go back and use NOWRAP to get rid of it.
</TD> </TR></TABLE>


Data inside this cell will not wrap. Be careful using this.
You can get an enormous table.
Default value is to let data inside the cell wrap. Sometimes that's not desirable. IE is really weird about that. Sometimes it'll add in an extra new line out of nowhere and you have to go back and use NOWRAP to get rid of it.


[ top ]