
Lesson 5. Tables.
Introduction to Tables.
Tables are one of the most versatile and useful
elements in HTML design. They provide a way of strictly defining where
you want various objects or elements to appear.
Tables allow you to divide your HTML page as displayed into rows and columns,
and are a great aid to the presentation of crisply organised Websites.
Be warned however, tables can be both your greatest friend and your worst
nightmare!
The coding for tables is quite simple, and it allows (in most browsers)
for sloppier coding for the content of each cell by using the action of
closing a particular cell to close all the tags within it. Thus it doesn't
matter if you leave off the odd </FONT> or </H2>
but this bad practice brings with it a price to pay with the more sensitive
browsers that won't allow these little indiscretions, and also means that
many tags that would usually be allowed to flow through a document, such
as FONT, must be implemented in each table cell.
Although the coding is forgiving on the content, the tags for the table
rows and cells themselves are very sensitive to errors. A page can be
fully tabled up and looking and performing really well..... some editing
of content, and the accidental deletion of a table related tag (they seem
to bury themselves into the code when things get complicated, and can
be very easily overlooked especially when you have tables within tables)
and suddenly the whole page reformats itself resembling its former self
having been dragged through a hedge backwards!
The Table Tag. <TABLE> </TABLE>
A Closed Tag signifying the start and end of the table's definition.
All table content and its own makeup go between these two tags.
Rows and Cells. <TR>
<TD> Content </TD> </TR>
A table is made up of rows which are defined by the <TR>
and </TR> tags. Each pair of these
tags represents a row in the table. For every row in your table you must
have a pair of <TR> and </TR>. These tags go between the <TABLE>
and </TABLE> tags, this positioning of tags within tags is called
"nesting."
"Nested" in the <TR> tags are the contents of the rows
which you've defined.
The content is usually in the form of table "cells" which are
defined by the <TD> and </TD>
tags. A row contains cells and the cells contain the visible content for
the page, be it text, images or a combination of both.
A Simple Table.
To make things easy to follow I have shown the displayed table content
in white
, the new tags in green
and the tags that have been previously covered in dark
green . We shall continue this colour coding throughout this
section. As new tags are introduced, and as attributes for each tag are
introduced, they will be shown in red.
To start with lets look at a simple table with three rows and three columns.
The code for this table follows.
<TABLE>
<TR>
<TD>
Top Left </TD>
<TD>
Top Middle </TD>
<TD>
Top Right </TD>
</TR>
<TR>
<TD>
Middle Left </TD>
<TD>
Center </TD>
<TD>
Middle Right </TD>
</TR>
<TR>
<TD>
Bottom Left </TD>
<TD>
Bottom Middle </TD>
<TD>
Bottom Right </TD>
</TR>
</TABLE> |
........ and here is that table.
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
(Notice how the cells are laid out in the HTML, using Tab to formulate.
Most HTML authors set tables out in this way, and it is generally accepted
that this is the easiest way to read and edit the code.)
Additionally you can combine two cells in a row together using the <TH>
tag. It's useage in the code is the same as the <TD>
tag (in that it is a closed tag and is nested in the <TR>
tags) with the exception of the fact that you must specify a value for
the variable COLSPAN="
" The <TH> tag allows you more flexibility with
cells and gives you further refinements to your formatting.
Taking our previous table and removing the top left and top centre cells
to replace them with one large spanning cell (effectively joing the two
cells together) requires the <TD> tags to be replaced by a <TH>
and </TH> pair as shown below. The <TH> tag is a table header
cell and automatically makes any text enclosed within it bold. To combine
cells avoiding the default bold text is done by applying the COLSPAN attribute
to the <TD> tag instead.
<TABLE>
<TR
>
<TH
COLSPAN="2" >
Spanning 2 </TH>
<TD>
Top Right </TD>
</TR>
<TR>
<TD>
Middle Left </TD>
<TD>
Center </TD>
<TD>
Middle Right </TD>
</TR>
<TR>
<TD>
Bottom Left </TD>
<TD>
Bottom Middle </TD>
<TD>
Bottom Right </TD>
</TR>
</TABLE> |
The joining of the two cells looks like this..........
| Spanning 2 |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
Table Variables - for the <TABLE> tag.
The <TABLE> tag can take several attributes which are used in the
form.....
<TABLE ATTRIBUTE="
">
The attributes that the <TABLE> tag will take are as follows.....
BORDER="
" sets the width of the table border in pixels.
Use BORDER="0"
for no borders on your tables.
Border examples using the same 3x3 table once again.
<TABLE
BORDER="0">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
<TABLE
BORDER="3">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
<TABLE
BORDER="5">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
CELLPADDING="
" sets the amount of space in pixels between the edges
of a cell and its contents.
Two examples of the CELLPADDING attribute.......
<TABLE
CELLPADDING="5">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
<TABLE
CELLPADDING="10">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
CELLSPACING="
" is sort of the opposite to CELLPADDING in many ways,
although when tables have BORDER=0 the effect looks similar. CELLSPACING
sets the distance in pixels between the cells themselves..
<TABLE
CELLSPACING="5">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
<TABLE
CELLSPACING="10">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
WIDTH="
" HEIGHT=" " sets the height or width of the
whole table in either pixels or as a percentage of the total available
height or width. (this is not necessarily the total screen width or height,
the WIDTH & HEIGHT attributes also work
within another table's cells when whole tables are nested inside others).
We shall cover this area later in the tutorial. If these attributes are
not specified in the <TABLE> tag then the table will automatically
size down to the minimum size required to display all the information
in it.
In the third example the width specified is too narrow to display all
the information on single lines. As no height was specified the table
height has resized to allow this information to be displayed.
Some WIDTH examples......
<TABLE
WIDTH="400">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
<TABLE
WIDTH="300">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
<TABLE
WIDTH="200">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
BGCOLOUR="
" sets the background colour for the table and its cells.
The colour is set using the six character RGB value that we covered in
Lesson 3 of this tutorial.
For more information on Hexadecimal colour codes we have a Resource
sheet and further information can be sourced from our HTML
Resource Centre.
At the time of writing this attribute does not work with the Netscape
Browser.
Some examples of BGCOLOR......
<TABLE
BGCOLOR="#660000">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
<TABLE
BGCOLOR="#006600">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
<TABLE
BGCOLOR="#000066">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
BACKGROUND="
" allows you to specify an image file as the table background
in much the same way as the BODY tag.
An example using "image1.gif" from the previous tutorial....
<TABLE
BACKGROUND="./images/images1.gif">
| Top Left |
Top Middle |
Top Right |
| Middle Left |
Center |
Middle Right |
| Bottom Left |
Bottom Middle |
Bottom Right |
Table Variables - for the <TR> and <TD>
tags.
The <TR> and <TD> tag can also take several attributes which
are used just as the previous <TABLE> ones.....
<TR ATTRIBUTE="
"> </TR>
<TD ATTRIBUTE="
"> </TD>
For the purpose of this exercise the examples will be shown in a table
of fixed WIDTH="500" pixels.
The attributes that the<TR> and <TD>tags will take are as
follows.....
ALIGN="
" when used with the <TR> tag sets the text and
image justification for all teh cells in that row to either LEFT, RIGHT
or CENTER.
An example showing the first row justified to LEFT, the second to CENTER
and the third to RIGHT....
| Aligned LEFT |
using |
<TR
ALIGN="left"> |
| Aligned CENTER |
using |
<TR
ALIGN="center"> |
| Aligned RIGHT |
using |
<TR
ALIGN="right"> |
When applied instead to the individual <TD> tags..............
| Aligned
LEFT |
Aligned
CENTER |
Aligned
RIGHT |
| using |
using |
using |
| <TD
ALIGN="left"> |
<TD
ALIGN="center"> |
<TD
ALIGN="right"> |
in the example above the cells are all invidually aligned using the ALIGN
tag.
Another example
| Aligned
RIGHT |
Aligned
CENTER |
Aligned
LEFT |
| Aligned
LEFT |
Aligned
RIGHT |
Aligned
CENTER |
| AlignedCENTER |
Aligned
LEFT |
Aligned
RIGHT |
VALIGN="
" works in much the same way as ALIGN but sets the vertical
justification of a row of cells when used in the <TR> tags, and
of a single cell when used in the <TD> tags..
Note:
This variable is NOT cross-browser compatible.
BGCOLOR="
" as with the BGCOLOR=""
attribute for the <TABLE> tag, you can use this to set the background
colours of rows of cells in the TR tag or individual cells in TD tags.
At the time of writing, this attribute does not work with the Netscape
Browser (4.5).
| <TD
BGCOLOR="#FFFFFF"> |
<TD
BGCOLOR="#CCCCCC"> |
| <TR
BGCOLOR="#CCCCEE"> |
|
| <TD
BGCOLOR="#00FFFF"> |
<TD
BGCOLOR="#00CCCC"> |
Table Variables - for the <TD>
tag only.
The <TD> tag takes further unique attributes in the usual form
<TD ATTRIBUTE="
"> </TD>
WIDTH="
" allows you to specify widths
of cells. Care should be taken when using this particular tag as tables,
by their very nature, are temperamental and use of this attribute should
be applied identically to every row. In effect this tag is used to specify
column widths.
This attribute can be specified as a number of pixels or a percentage
of the total available width.
Examples of width specification on tables of total given width 500 pixels...
|
<TD
WIDTH="40%">
|
<TD
WIDTH="60%">
|
|
<TD
WIDTH="40%">
|
<TD
WIDTH="60%">
|
|
<TD
WIDTH="40%">
|
<TD
WIDTH="60%">
|
|
<TD
WIDTH="200">
|
<TD
WIDTH="292">
|
|
<TD
WIDTH="200">
|
<TD
WIDTH="292">
|
|
<TD
WIDTH="200">
|
<TD
WIDTH="292">
|
note that as BORDER="2" the <TD> width totals have to
take this into account. Each of the 2 cells in a row has a 2 pixel left
border and a two pixel right border.
T hus 200+292+8(for borders)=500
COLSPAN="
" Which we covered earlier
when we introduced the <TH> tag.
It creates larger cells by merging smaller ones together.
ROWSPAN="
" Which works in the same
way as colspan but creates rows by merging cells vertically..
A Complex Table and the code required to create it.
As before attributes are shown in red, tags in green and content in white.
| One cell |
One cell |
One cell |
| One cell spanning two rows |
One cell spanning three rows |
| One cell spanning two rows |
One cell |
| A Row |
<TABLE
WIDTH="500" BORDER ="1" CELLSPACING="1"
CELLPADDING="1" >
<TR>
<TD><B>One
cell</B></TD>
<TD><B>One
cell</B></TD>
<TD><B>One
cell</B></TD>
</TR>
<TR>
<TD COLSPAN="2"><B>One
cell spanning two rows </B></TD>
<TD ROWSPAN="3"><B>One
cell spanning three rows</B></TD>
</TR>
<TR>
<TD ROWSPAN="2"><B>One
cell spanning two rows</B></TD>
<TD><B>One
cell</B></TD>
</TR>
<TR>
<TD><B>A
Row </B></TD>
</TR>
</TABLE>
Nested Tables
Mention has been made of "tables within tables", and it only remains
to explain an over-view of this process. Essentially, by placing an
entire table - from <TABLE>
to </TABLE> - within a cell of a
parent table - within <TD> and </TD>
- you can execute even greater control over the layout of your HTML
page.
<TABLE>
<TR>
<TD><B>One
cell</B></TD>
<TD>
<TABLE>
<TR>
<TD><B>One cell</B></TD>
<TD>
</TD>
<TD><B>One cell</B></TD>
</TR>
</TABLE>
</TD>
<TD><B>One
cell</B></TD>
</TR>
</TABLE>
A word of warning: Tables
are complex elements at the best of times. utilising nested tables can
be very complicated, and it is particularly easy to lose a tag
- so please take care with your code, and make use of the HTML authoring
protocol mentioned previously on this page.
Final Word
You have now reached the end of this HTML tutorial. Everything contained
within these tutorials will enable you, with the right application,
to create pages just like these. With the exception of only a couple
of minor details there is nothing in the HTML coding of these tutorials
that isn't covered in the text.
The next part is up to you. Design is a combination of knowledge and
flair. We cannot provide the flair for you but we hope that these tutorials
have helped you over the first hurdle and allow you to express your
own flair and individualism in ways that previously were unavailable
to you.
Please make use of our HTML
resource centre as a reference library in your future authoring
and if there's something that you feel should be added then please submit
requests via our response form.
|
|

Tutorial
Site Map
HTML Resource Centre
Lesson 1
Getting Started
Lesson 2
Structuring Your Page
- Introduction to HTML
- A Basic HTML Document
Lesson 3
Text on Your Page
- Formatting Text
- Text Hyperlinks
- Horizontal Rule
- Line Break & Paragraphs
Lesson 4
Images and Backgrounds
- Page Backgrounds
- Placing an Image
- Image Hyperlinks
Lesson 5
Tables and Formatting
- Creating a Table
- Invisible tables as Layout
- Table Backgrounds
- Nested Tables
|