Tables can be used for many things, not just for laying out columns of data etc. Take a look at the bottom of each of these lessons and you will see a set of 'buttons' for the previous lesson, next lesson & back to the home page. This is a simple table but I am using it as a button bar - because I can !
Lets get straight in and create a simple table, like the one I use for navigation around these lessons. Fire up your trusty editor and put the following code into the BODY area. (OK, I said I wouldn't say that again, but ......)
<TABLE>
</TABLE>
These are the required tags for creating a table. As with most tags, there are attributes that can be applied, but we will work those out as we go on.
To define a row in a table, use the Table Row tag which looks like this <TR> and has a closing tag which is this </TR>. Every row in the table must be surrounded by a pair of these tags. Let us add a single row to our skeleton table. The code needs to be changed to that shown below :
<TABLE>
<TR>
</TR>
</TABLE>
Some people like to use indentation to make tables stand out better. This is fine and I agree that it makes things more readable. However, all that blank space has to be downloaded, scanned and acted upon (ie ignored) by the HTML parser in the browser. Bear this in mind. I'm all in favour of readable code when defining my pages, and I will probably get around to running a filter to remove the whitespace before uploading the page. Alternatively, keep your pages short to reduce the overhead.
So far we have nothing in our actual table, so we need to define a column (or two) in the row. Read on....
A column is defined using a tag - no surprises there then, but not a <TC> tag as one might expect, but a <TD> and its closing tag </TD>. I suspect that TD stands for Table Down ? - who knows. Lets add a column to our row.
<TABLE>
<TR>
<TD>
</TD>
</TR>
</TABLE>
And finally, add something into the column, I shall put my name in mine, you can put your name, cue the obvious jokes from the back !
<TABLE>
<TR>
<TD>
My Name (Sorry !)
</TD>
</TR>
</TABLE>
And so our table looks like this :
My Name (Sorry !)
It doesn't look like a table does it ? In fact, it looks just like some text on a page. We shall remedy that later, but for now we shall add a column header
Column headers require a row of their own. This is because they take up a single row in the table. So we need to add a row to the table, I won't show that step now as we know how to do it ! A header is also a column in the row, so do we need to add a column tag ? The answer is no, we add a new tag called a table header tag and this is what it looks like <TH> and its closing tag looks like this </TH>. Lets add these to our table and put some text into the header as well. Our table's code should look like this :
<TABLE>
<TR>
<TH>
Column Header
</TH>
</TR>
<TR>
<TD>
My Name (Sorry !)
</TD>
</TR>
</TABLE>
And so our table looks like this :
Column Header My Name (Sorry !)
As you can see, the header has been defined in bold text and is centre justified within the cell width. (We can't really see that here as we don't have any borders showing the cell widths !). We could use TD attributes to do the same thing, but using TH saves typing and is less effort - get the browser to do it for you !
Another thing, our simple table is starting to spread out a bit (see above) at least the HTML source is. As I mentions in the previous lesson, the format of the source code is mostly ignored when rendered by the browser, we can save some room in our source file and reduce the time to download by re-organising as follows :
<TABLE>
<TR>
<TH>Column Header</TH>
</TR>
<TR>
<TD>My Name (Sorry !)</TD>
</TR>
</TABLE>
And our table still looks like this :
Column Header My Name (Sorry !)
In fact, as this is a simple one row, one column (not including headers) table, we can refine it thus :
<TABLE>
<TR><TH>Column Header</TH></TR>
<TR><TD>My Name (Sorry !)</TD></TR>
</TABLE>
And our table still looks like this :
Column Header My Name (Sorry !)
Never forget, the browser throws away most of your formatting
So, our simple table now has headings and a single column. It still looks like text on the page, so lets tart it up. We shall use TABLE level attributes to affect how the entire table looks. Table attributes are added to the TABLE tag.
To add a border around the table, add the BORDER attribute and give it a size :
<TABLE BORDER="2">
<TR><TH>Column Header</TH></TR>
<TR><TD>My Name (Sorry !)</TD></TR>
</TABLE>
Our table is now starting to look like one !
Column Header My Name (Sorry !)
We can alter the background colour of the table to make it stand out from the rest of the page. We use the BGCOLOR attribute to do this - we must also specify a colour :
<TABLE BORDER="2" BGCOLOR="#FFC0CB">
<TR><TH>Column Header</TH></TR>
<TR><TD>My Name (Sorry !)</TD></TR>
</TABLE>
Our table is now starting to stand out from the crowd !
Column Header My Name (Sorry !)
The following table is a list of commonly used table attributes which should be viewable on most browsers :
| Attribute | Description |
|---|---|
| WIDTH | Sets the width of the table as it shall appear in the browser. This can be a percentage of the browser width (eg 40%) or a fixed number of pixels. (eg 420) |
| HEIGHT | Sets the height of the table. Again this can be as a percentage of the browser's window or a fixed number of pixels. |
| BORDER | Sets the width (in pixels) of the border around the entire table. Also affects the border between cells. |
| BGCOLOR | Sets the background colour for the entire table. (But see row & column attributes later on.) |
| CELLPADDING | Specifies the number of pixels that are left blank around the contents of each cell. This may have the effect on increasing the cell height and width. |
| CELLSPACING | Specifies the number of pixels between adjacent cells. Basically makes the border between cells wider or narrower. |
| ALIGN | Specifies the horizontal alignment of the whole table. Values allowed are LEFT, CENTER & RIGHT. Most browsers are fine with left & right but many have problems with center. Additionally, any text following the table definition will 'flow' around the table on the opposite side to the tables ALIGN attribute. Text flows around the table on the RIGHT if the table has ALIGN=LEFT. To stop this wrapping use <BR CLEAR="left, right or all">. See Advanced Tables for more details. |
| BACKGROUND | Specifies the name of an image file to be drawn in the background of the table. |
Here is the code to extend our small table to be 40% of the browser width :
<TABLE BORDER="2" BGCOLOR="#FFC0CB" WIDTH="40%">
<TR><TH>Column Header</TH></TR>
<TR><TD>My Name (Sorry !)</TD></TR>
</TABLE>
And this is the result :
Column Header My Name (Sorry !)
As you might have guessed by now, row attributes are applied to the TR tag to alter how an individual row looks in the browser. You can change the colour, size, width etc
The following table is a list of commonly used table attributes which should be viewable on most browsers :
| Attribute | Description |
|---|---|
| ALIGN | Specifies the horizontal alignment for the contents of the cells in this row. If not specified, inherits the TABLE's ALIGN attribute - or the default which is LEFT. |
| BGCOLOR | Sets the background colour for each column in this row. Inherits the TABLE's BGCOLOR attribute if now specified. |
| VALIGN | Sets the vertical alignment for this row. The allowed values are TOP, BOTTOM & MIDDLE. |
Lets change our table so that the non-header row has a different coloured background, here is the code to do it :
<TABLE BORDER="2" BGCOLOR="#FFC0CB" WIDTH="40%">
<TR><TH>Column Header</TH></TR>
<TR BGCOLOR="#CCFFCC"><TD>My Name (Sorry !)</TD></TR>
</TABLE>
And this is the result :
Column Header My Name (Sorry !)
First I think we better extend the simple table so that it has a few more columns. Take your code and change it to the following :
<TABLE BORDER="2" BGCOLOR="#FFC0CB" WIDTH="80%">
<TR>
<TH>Column One</TH>
<TH>Column Two</TH>
<TH>Column Three</TH>
</TR>
<TR>
<TD>Cell 1</TD>
<TD>Cell 2</TD>
<TD>Cell 3</TD>
</TR>
<TR>
<TD>Cell 4</TD>
<TD>Cell 5</TD>
<TD>Cell 6</TD>
</TR>
</TABLE>
Which gives the following table
Column One Column Two Column Three Cell 1 Cell 2 Cell 3 Cell 4 Cell 5 Cell 6
So the whole table is a nice (?) pinky colour, lets change some of the columns to a different colour, first change column one to be a nice (?) light blue, as follows, you change the bits in red :
<TABLE BORDER="2" BGCOLOR="#FFC0CB" WIDTH="80%">
<TR>
<TH>Column One</TH>
<TH>Column Two</TH>
<TH>Column Three</TH>
</TR>
<TR>
<TD BGCOLOR="#99FFFF">Cell 1</TD>
<TD>Cell 2</TD>
<TD>Cell 3</TD>
</TR>
<TR>
<TD>Cell 4</TD>
<TD>Cell 5</TD>
<TD>Cell 6</TD>
</TR>
</TABLE>
Column One Column Two Column Three Cell 1 Cell 2 Cell 3 Cell 4 Cell 5 Cell 6
But that's not right, it has only changed the first cell, not cell 1 and cell 4 which are both in the first column - what's going on ?
When we set a whole row to a different colour, we set all cells in that row, however, when we do the cells, we have to do them individually - because there isn't a tag that encompasses the whole column - shame :o(. We have to specify the cell colour for each cell in the first column, as follows. Again, you add the bits in red :
<TABLE BORDER="2" BGCOLOR="#FFC0CB" WIDTH="80%">
<TR>
<TH>Column One</TH>
<TH>Column Two</TH>
<TH>Column Three</TH>
</TR>
<TR>
<TD BGCOLOR="#99FFFF">Cell 1</TD>
<TD>Cell 2</TD>
<TD>Cell 3</TD>
</TR>
<TR>
<TD BGCOLOR="#99FFFF">Cell 4</TD>
<TD>Cell 5</TD>
<TD>Cell 6</TD>
</TR>
</TABLE>
Column One Column Two Column Three Cell 1 Cell 2 Cell 3 Cell 4 Cell 5 Cell 6
So far I have only discussed the colouring of cells, there are other attributes that can be applied to TD tags, these are defined in the following table :
| Attribute | Description |
|---|---|
| ALIGN | Specifies the horizontal alignment for the contents of the cell. If not specified, inherits the TABLE's ALIGN attribute - or the default which is LEFT. |
| BGCOLOR | Sets the background colour for the cell. Inherits the TABLE's BGCOLOR attribute if now specified. |
| VALIGN | Sets the vertical alignment for this cell. The allowed values are TOP, BOTTOM & MIDDLE. |
| WIDTH | Allows you to specify the width of the cell as a percentage (of the table) or as a fixed size in pixels. |
Tables can be nested inside each other. I have saved this for a later exercise. Click here to go to it
A row can span more than one column. This is useful when you have a table is not a regular grid where each and every column has the same number of rows. Take a look at this table which is part of the lists lesson (click the back button on your browser to return here)
Did you notice how some columns are more than a single row deep ? This is called ROW SPANNING and occurs when you tell the browser, via your HTML, that this cell will span more than a single row. Lets do it to our example table which needs to be changed to look like the following. I have removed the BGCOLOR settings for the first column as well.
<TABLE BORDER="2" BGCOLOR="#FFC0CB" WIDTH="80%">
<TR>
<TH>Column One</TH>
<TH>Column Two</TH>
<TH>Column Three</TH>
</TR>
<TR>
<TD ROWSPAN=2>Cell 1</TD>
<TD>Cell 2</TD>
<TD>Cell 3</TD>
</TR>
<TR>
<TD>Cell 4</TD>
<TD>Cell 5</TD>
<TD>Cell 6</TD>
</TR>
</TABLE>
Column One Column Two Column Three Cell 1 Cell 2 Cell 3 Cell 4 Cell 5 Cell 6
Notice how that has seriously screwed up our nice neat layout ? When you specify that a cell spans more than a single row, you have to make sure that there are still the same amount of rows & columns in the table. As I said that our cell will span 2 columns, I need to remove a cell from that (or a following) row to keep things even, as follows :
<TABLE BORDER="2" BGCOLOR="#FFC0CB" WIDTH="80%">
<TR>
<TH>Column One</TH>
<TH>Column Two</TH>
<TH>Column Three</TH>
</TR>
<TR>
<TD ROWSPAN=2>Cell 1</TD>
<TD>Cell 2</TD>
<TD>Cell 3</TD>
</TR>
<TR>
<TD>Cell 5</TD>
<TD>Cell 6</TD>
</TR>
</TABLE>
Column One Column Two Column Three Cell 1 Cell 2 Cell 3 Cell 5 Cell 6
You can see now that cell 4 (the first cell in the following row) has been removed and the table is once more looking normal.
COLUMN SPANNING is also allowed. This specifies that a single cell spans more than one column. Lets pick on cell 2 and make it bigger to take up the room allocated for cell 3. This means that cell 3 has to be evicted from the table. Here's the code :
<TABLE BORDER="2" BGCOLOR="#FFC0CB" WIDTH="80%">
<TR>
<TH>Column One</TH>
<TH>Column Two</TH>
<TH>Column Three</TH>
</TR>
<TR>
<TD ROWSPAN=2>Cell 1</TD>
<TD COLSPAN=2>Cell 2</TD>
</TR>
<TR>
<TD>Cell 5</TD>
<TD>Cell 6</TD>
</TR>
</TABLE>
Column One Column Two Column Three Cell 1 Cell 2 Cell 5 Cell 6
Had enough yet ? Why not try combining BGCOLOR and spanning to get really fancy tables. As they say, I shall leave this as an exercise for the reader. Me, I'm off to do some lists. Read on.
There are a few list types allowed in HTML, they are :
Interestingly enough, the above is an example of an unordered list. To show the same data as an ordered list would look like this :
As you can see, we have numbering on this list - and that is the only difference. As with many things, the lists can be configured to suit all tastes and each individual item can have its appearance changed to differ from the list as a whole. Lets now, see a definition list in action. Actually, the very first page of this tutorial has a good example, but here is another :
The tags used to define these three list types are as follows :
| List type | Opening tag | Closing tag | List item tags |
|---|---|---|---|
| Unordered | <UL> | </UL> | <LI> </LI> |
| Ordered | <OL> | </OL> | <LI> </LI> |
| Definition | <DL> | </DL> | <DT> </DT> & <DD> </DD> |
To define a list you first decide which list type you want, then for each type you choose a bullet type. The following tables outlines each list type and the allowed bullet types and shows an example of each. I have discovered that the TYPE parameter must be specified in lower case or you always get disc bullets - beware.
| List type | Bullet type | Example list |
|---|---|---|
| Unordered | Disc |
|
| Circle | ||
| Square | ||
| Ordered | 1 (numbers) |
|
| A (Capital letters) | ||
| a (letters) | ||
| I (Capital Roman numerals) | ||
| i (Roman numerals) | ||
| Definition | None | A Definition list has no options. |
Internet explorer tends to show the square bullet as a very small square - this can make it look like a disc - a though to ponder as you go through life.
So having chosen the bullet type for your list, and a list type, start typing the tags as follows :
<UL TYPE="disc">
<LI> List item 1</LI>
<LI> List item 2</LI>
<LI> List item 3</LI>
<LI> List item 4</LI>
</UL>
Which displays as the following in the browser :
- List item 1
- List item 2
- List item 3
- List item 4
Obviously, your list might be different from mine, you may have chosen an ordered list with a different type. It all depends upon the list really !
You can, if you wish, make each bullet in the list different, or just a few of them. If the bullet is to be predominantly the same for all items, simply generate the list as above with the TYPE="whatever" on the opening tag for the whole list (UL in my example). To change a specific list item put the required type on the opening LI tag, for example, I shall modify the above list to use a circle bullet on list item number 4. The code will now be as follows :
<UL TYPE="disc">
<LI> List item 1</LI>
<LI> List item 2</LI>
<LI> List item 3</LI>
<LI TYPE="circle"> List item 4</LI>
</UL>
Which displays as the following in the browser :
- List item 1
- List item 2
- List item 3
- List item 4
This can be done for the ordered list items as well.
Lists can be nested. The process is quite simple and these are the steps you must take :
Using our original 4 item list above, I shall nest it inside itself. In list item 1 I shall insert a copy of the list. Don't panic, I shall do it in simple stages as described above.
Step 1 - create the outer list
<UL TYPE="disc">
<LI> Insert another list here</LI>
<LI> List item 2</LI>
<LI> List item 3</LI>
<LI> List item 4</LI>
</UL>
Our first step gives the following list :
- Insert another list here
- List item 2
- List item 3
- List item 4
Step 2 - create the inner list
<UL TYPE="disc">
<LI>Inner list goes here
<UL TYPE="circle">
<LI> Inner list item 1</LI>
<LI> Inner list item 2</LI>
<LI> Inner list item 3</LI>
<LI> Inner list item 4</LI>
</UL>
</LI>
<LI> List item 2</LI>
<LI> List item 3</LI>
<LI> List item 4</LI>
</UL>
Our second step gives the following list :
- Inner list goes here
- Inner list item 1
- Inner list item 2
- Inner list item 3
- Inner list item 4
- List item 2
- List item 3
- List item 4
Some good advice: when nesting lists, I would advise strongly, the use of tabs to set out your source code - it makes life a lot easier ! Another thing, I have specified the list type (circle) on my inner list. The browser will use a default for the inner list if you don't specify one. This is usually a different type from the outer list.
These are different from the ordered & unordered lists I have described above and need to be defined differently. There are two parts in a definition list, the term to be defined and the definition. These parts have different tags. A step by step guide follows :
Start with the list tags :
<DL>
</DL>
Then add a DT pair of tags for each term to be defined :
<DL>
<DT> Term to be defined </DT>
</DL>
Then add a DD pair of tags for each term definition :
<DL>
<DT> Term to be defined </DT>
<DD> Definition of the term </DD>
</DL>
Then repeat for all other terms to be defined :
<DL>
<DT> Term to be defined </DT>
<DD> Definition of the term </DD>
<DT> Second term to be defined </DT>
<DD> Second definition </DD>
<DT> Another term to be defined </DT>
<DD> Another definition </DD>
</DL>
This results in the following table :
- Term to be defined
- Definition of the term
- Second term to be defined
- Second definition
- Another term to be defined
- Another definition
Now I don't know about you, but I prefer my definition lists to look more like this :
- Term to be defined
- Definition of the term
Second term to be defined- Second definition
Another term to be defined- Another definition
So how do we convert the original table to the new format ? Simple, here is the code to make the defined terms (DT) bold. (Note I have used the <B> tag rather than the <STRONG> tag)
<DL>
<DT> <B>Term to be defined </B></DT>
<DD> Definition of the term </DD>
<DT> <B>Second term to be defined </B></DT>
<DD> Second definition </DD>
<DT> <B>Another term to be defined </B></DT>
<DD> Another definition </DD>
</DL>
This results in the following table :
- Term to be defined
- Definition of the term
- Second term to be defined
- Second definition
- Another term to be defined
- Another definition
Then I add a couple of line break tags (BR) at the start of the second & subsequent DT tags :
<DL>
<DT> <B>Term to be defined </B></DT>
<DD> Definition of the term </DD>
<DT> <B><BR>Second term to be defined </B></DT>
<DD> Second definition </DD>
<DT> <B><BR>Another term to be defined </B></DT>
<DD> Another definition </DD>
</DL>
This results in the following table :
- Term to be defined
- Definition of the term
Second term to be defined- Second definition
Another term to be defined- Another definition
Which to my mind looks a lot nicer, and finishes off lists and indeed, this lesson for now.
| Previous Lesson | Home Page | Next Lesson |