|
|
Building Web Pages From Ground Zero By Professor Nibble ã Nibble University 2000-2007 HyperJump to Captions, Borders, Spanning, Align/Valign, Cell Color, Background Image, No Wrap, Width & Height, Assignments TABLES - Lesson 9 |
||
|
|
BASIC TABLE TAGS Tables are used for organizing data in rows and columns. You may also use tables to organize your page layout. In the last lesson you saw an example of how to use a table along with a background that produced a graphical left margin. By using a table I was able to place text to the right of the left margin so it could be clearly read. Let's start out, however, with some simple tables that align some data. All table tags and data must come between these tags: <TABLE> </TABLE> The other basic tags you will use are: <TR> </TR> <TD> </TD> <TH> </TH> They define the rows, columns and any headings. This is what a simple 2 column 3 row table would look like: <TABLE> |
||
|
|
This is the result: |
||
|
Row 1 Column 1 |
Row 1 Column 2 |
||
|
Row 2 Column 1 |
Row 2 Column 2 |
||
|
Row 3 Column 1 |
Row 3 Column 2 |
||
|
Now, let's break this little guy down. First, notice the table begins and ends with the <TABLE> </TABLE> tags. You use <TR> </TR> to define each row. Notice there are three sets of these tags - so, three rows. For every <TD> </TD> set nested inside a <TR> </TR> set, you have a column created. In our example, we have two <TD> </TD> sets within each <TR> </TR> set. Therefore, each row has two columns defined. Now, that wasn't too bad was it? But, what is the <TH> </TH> tag I mentioned earlier? So good of you to ask. The <TH> </TH> tag is for a heading. Any <TD> </TD> tag set can be replaced by a <TH> </TH> tag set and become a BOLD style heading. Watch me change the columns in row one into headings: <TABLE> |
|||
|
Headings Result: |
|||
|
Row 1 Column 1 |
Row 1 Column 2 |
||
|
Row 2 Column 1 |
Row 2 Column 2 |
||
|
Row 3 Column 1 |
Row 3 Column 2 |
||
|
OK. Challenge question - What would you do to make all the data in column one a heading? Creating more complicated tables can be a real challenge and great fun. I have some fantastic exercises planned for you at the end of this lesson. |
|||
|
|
Some people like to include a descriptive caption with their table to help explain what the data is all about. To do this, place a <CAPTION> </CAPTION> tag right after the starting <TABLE> tag and before row one. <CAPTION> has an attribute to help you have a little more control over the placement of the caption. <CAPTION ALIGN=TOP, BOTTOM, RIGHT, LEFT> You'll have to experiment with the 4 different align choices. Browsers may treat them differently. If you don't use right or left the caption will be centered. I think the two most useful attributes are "top" and "bottom". |
||
|
|
Example: |
||
|
|
<TABLE> |
||
|
|
Row 1 Column 1 |
Row 1 Column 2 |
|
|
|
Row 2 Column 1 |
Row 2 Column 2 |
|
|
|
Row 3 Column 1 |
Row 3 Column 2 |
|
|
|
This is a caption for the table. |
|
|
|
|
|
||
|
If you wish to include borders around all your cells, you will need to add the BORDER attribute to the <TABLE> tag. It is quite simple: <TABLE BORDER> This will give you a border of one pixel. It is the same thing as putting: <TABLE BORDER=1> If you DO NOT want a border simply do not include a border attribute or put: <TABLE BORDER=0> To increase the width of your borders, increase the number in the border attribute: <TABLE BORDER=5> |
|||||||
|
Look at this example:
<TABLE BORDER=1> | |||||||
|
|
Tip: More advanced tables can be a bit of a challenge when creating them from scratch. I think it helps to create your table on paper before attempting to code it. When you create a table, your column widths are going to be determined by the data inside them. That may leave you with an undesirable affect like this: |
|
<TABLE BORDER> <CAPTION>Points for Read A Thon Contest</CAPTION> <TR> <TH>9TH GRADERS</TH> <TH>10TH GRADERS</TH> </TR> <TR> <TD>Mon</TD> <TD>Tue</TD> <TD>Total</TD> <TD>Mon</TD> <TD>Tue</TD> <TD>Total</TD> </TR> <TR> <TD>6</TD> <TD>4</TD> <TD>10</TD> <TD>7</TD> <TD>2</TD> <TD>9</TD> </TR> </TABLE> |
| 9TH GRADERS | 10TH GRADERS | ||||
|---|---|---|---|---|---|
| Mon | Tue | Total | Mon | Tue | Total |
| 6 | 4 | 10 | 7 | 2 | 9 |
|
By using the COLSPAN attribute in the <TD> or <TH> tag you can fix this sort of problem. In the second row the first three columns have 9th grade data and the last three columns have 10th grade data. Therefore, our data in row one should span the appropriate columns. Look at the example below - in particular how COLSPAN is used. The number inserted after the = sign corresponds to the number of columns you wish to span. Look at this example: |
|
<TABLE BORDER> <CAPTION>Points for Read A Thon Contest</CAPTION> <TR> <TH COLSPAN="3">9TH GRADERS</TH> <TH COLSPAN="3">10TH GRADERS</TH> </TR> <TR> <TD>Mon</TD> <TD>Tue</TD> <TD>Total</TD> <TD>Mon</TD> <TD>Tue</TD> <TD>Total</TD> </TR> <TR> <TD>6</TD> <TD>4</TD> <TD>10</TD> <TD>7</TD> <TD>2</TD> <TD>9</TD> </TR> </TABLE> |
| 9TH GRADERS | 10TH GRADERS | |||||
|---|---|---|---|---|---|---|
| Mon | Tue | Total | Mon | Tue | Total | |
| 6 | 4 | 10 | 7 | 2 | 9 | |
|
ROWSPAN This attribute is handled exactly like COLSPAN. The only difference is that you create cells that span more than one row rather than columns. Here is an example of the ROWSPAN attribute: |
|
<TABLE BORDER> <CAPTION>Points for Read A Thon Contest</CAPTION> <TR> <TD ROWSPAN="3">Data</TD> <TH COLSPAN="3">9TH GRADERS</TH> <TH COLSPAN="3">10TH GRADERS</TH> </TR> <TR> <TD>Mon</TD> <TD>Tue</TD> <TD>Total</TD> <TD>Mon</TD> <TD>Tue</TD> <TD>Total</TD> </TR> <TR> <TD>6</TD> <TD>4</TD> <TD>10</TD> <TD>7</TD> <TD>2</TD> <TD>9</TD> </TR> </TABLE> |
| Data | 9TH GRADERS | 10TH GRADERS | |||||
|---|---|---|---|---|---|---|---|
| Mon | Tue | Total | Mon | Tue | Total | ||
| 6 | 4 | 10 | 7 | 2 | 9 | ||
|
Basically, I added a seventh column, put it in as column number 1, and had it span all three rows. When I try to get fancy with tables I usually find that there is a lot of trial and error involved until I get the desired affect. |
|
The ALIGN attribute is used in <TR><TH><TD> and <TABLE> tags. You can align text to the left, right, center or justify. You do not need to put the align attribute in every tag of a row. Just put it in the initial tag. Using align in the <TABLE> tag is a bit different. This aligns the entire table in relation to the page - to the left, right or center. The default alignment for ALIGN is LEFT.
VALIGN is used in the <TR> <TD> <TH> tags. It may be used along with an ALIGN attribute. Its job is to vertically align the cell data. Your most common choices are top, middle or bottom. If you don't use VALIGN, the default is MIDDLE. When you use VALIGN in the <TR> tag all the columns in that row will be affected.
Study this example: |
|
<TABLE BORDER ALIGN=CENTER> |
| HORIZONTAL | ||||
|---|---|---|---|---|
| Left | Center | Right | ||
| V | Top | Top Left | Top Center | Top Right |
| Middle | Middle Left | Middle Center | Middle Right | |
| Bottom | Bottom Left | Bottom Center | Bottom Right | |
|
Take a look at the last row of the source code. Notice I put a VALIGN attribute in the <TR> tag instead of each column tag. I did this to show you a slightly different way of handling the desired results. Also notice the second column in the bottom row (Bottom) changes the VALIGN for that one column. Attributes placed in <TH> and <TD> take precedent over <TR>. |
|
<TABLE BORDER ALIGN="CENTER" BGCOLOR="RED"> <CAPTION><FONT COLOR=#FFFFFF SIZE=2>BGCOLOR="RED" IN THE <TABLE> TAG</FONT></CAPTION> <TR> <TD ROWSPAN="3"><FONT COLOR=#FFFFFF>Data</FONT></TD> <TH COLSPAN="3"><FONT COLOR=#FFFFFF>9TH GRADERS</FONT></TH> <TH COLSPAN="3"><FONT COLOR=#FFFFFF>10TH GRADERS</FONT><TH> </TR> <TR> <TD><FONT COLOR=#FFFFFF>Mon</FONT></TD> <TD><FONT COLOR=#FFFFFF>Tue</FONT></TD> <TD><FONT COLOR=#FFFFFF>Total</FONT></TD> <TD><FONT COLOR=#FFFFFF>Mon</FONT></TD> <TD><FONT COLOR=#FFFFFF>Tue</FONT></TD> <TD><FONT COLOR=#FFFFF>Total</FONT></TD> </TR> <TR> <TD><FONT COLOR=#FFFFFF>6</FONT></TD> <TD><FONT COLOR=#FFFFFF>4</FONT></TD> <TD><FONT COLOR=#FFFFFF>10</FONT></TD> <TD><FONT COLOR=#FFFFFF>7</FONT></TD> <TD><FONT COLOR=#FFFFFF>2</FONT></TD> <TD><FONT COLOR=#FFFFFF>9</FONT></TD> </TR> </TABLE> Notice I needed to change the font color in each cell to white so it would show up better on a red background. |
| Data | 9TH GRADERS | 10TH GRADERS | |||||
|---|---|---|---|---|---|---|---|
| Mon | Tue | Total | Mon | Tue | Total | ||
| 6 | 4 | 10 | 7 | 2 | 9 | ||
|
<TABLE BORDER ALIGN="CENTER"> <CAPTION><FONT COLOR=#FFFFFF SIZE=2>BGCOLOR COMBOS</FONT></CAPTION> <TR> <TD ROWSPAN="3"><FONT COLOR=#FFFFFF>Data</FONT></TD> <TH COLSPAN="3" BGCOLOR=GREEN><FONT COLOR=#FFFFFF>9TH GRADERS</FONT></TH> <TH COLSPAN="3" BGCOLOR="#FFFF00"><FONT COLOR=#000000>10TH GRADERS</FONT><TH> </TR> <TR BGCOLOR="#00A5C6"> <TD><FONT COLOR=#FFFFFF>Mon</FONT></TD> <TD><FONT COLOR=#FFFFFF>Tue</FONT></TD> <TD><FONT COLOR=#FFFFFF>Total</FONT></TD> <TD><FONT COLOR=#FFFFFF>Mon</FONT></TD> <TD><FONT COLOR=#FFFFFF>Tue</FONT></TD> <TD><FONT COLOR=#FFFFFF>Total</FONT></TD> </TR> <TR> <TD BGCOLOR=PURPLE><FONT COLOR=#FFFFFF>6</FONT></TD> <TD BGCOLOR=PURPLE><FONT COLOR=#FFFFFF>4</FONT></TD> <TD BGCOLOR=PURPLE><FONT COLOR=#FFFFFF>10</FONT></TD> <TD BGCOLOR="#FF8429"><FONT COLOR=#FFFFFF>7</FONT></TD> <TD BGCOLOR="#FF8429"><FONT COLOR=#FFFFFF>2</FONT></TD> <TD BGCOLOR="#FF8429"><FONT COLOR=#FFFFFF>9</FONT></TD> </TR> </TABLE> |
| Data | 9TH GRADERS | 10TH GRADERS | |||||
|---|---|---|---|---|---|---|---|
| Mon | Tue | Total | Mon | Tue | Total | ||
| 6 | 4 | 10 | 7 | 2 | 9 | ||
| Data | 9TH GRADERS | 10TH GRADERS | |||||
|---|---|---|---|---|---|---|---|
| Mon | Tue | Total | Mon | Tue | Total | ||
| 6 | 4 | 10 | 7 | 2 | 9 | ||
|
<TABLE BORDER ALIGN="CENTER"> <CAPTION><FONT COLOR="#FFFFFF">Points for Read A Thon Contest</FONT></CAPTION> <TR> <TD ROWSPAN="3" BACKGROUND="fuji.jpg"<Data</TD> <TH COLSPAN="3" BACKGROUND="fuji.jpg">9TH GRADERS</TH> <TH COLSPAN="3" BACKGROUND="fuji.jpg">10TH GRADERS<TH> </TR> <TR ALIGN=CENTER> <TD BACKGROUND="fuji.jpg">Mon</TD> <TD BACKGROUND="fuji.jpg">Tue</TD> <TD BACKGROUND="fuji.jpg">Total</TD> <TD BACKGROUND="fuji.jpg">Mon</TD> <TD BACKGROUND="fuji.jpg">Tue</TD> <TD BACKGROUND="fuji.jpg">Total</TD> </TR> <TR ALIGN=CENTER> <TD BACKGROUND="fuji.jpg">6</TD> <TD BACKGROUND="fuji.jpg">4</TD> <TD BACKGROUND="fuji.jpg">10</TD> <TD BACKGROUND="fuji.jpg">7</TD> <TD BACKGROUND="fuji.jpg">2</TD> <TD BACKGROUND="fuji.jpg">9</TD> </TR> </TABLE> |
| Data | 9TH GRADERS | 10TH GRADERS | |||||
|---|---|---|---|---|---|---|---|
| Mon | Tue | Total | Mon | Tue | Total | ||
| 6 | 4 | 10 | 7 | 2 | 9 | ||
|
Notice where the BACKGROUND attribute was placed in the first table. It actually doesn't look too bad. In the second table I put a BACKGROUND attribute in each cell. Microsoft Explorer shows the top upper left of the image in each cell. If I were to make a cell really big, you would be able to see much of the image. Netscape Navigator is reported to show the entire tiny image in each cell. I haven't tested this. Versions of the two major browsers change so often that what may be true today is a falsehood tomorrow.
In the first example I included a WIDTH and HEIGHT attribute that we will talk about soon. By creating a table with approximately the same dimensions as the image, I took advantage of showing as much graphic as possible in my table background. Just remember what we talked about ages ago - you need to have good contrast between background and text. |
|
Table & Cell Width & Height
I saved this attribute for last because we are going to use it two ways. The first is to take control of a table's size. Later, we will use these attributes to control our page layout. The attributes go inside the <TABLE> <TH> <TD> tags and look like this:
WIDTH=X
The X can be size in pixels or the % of full window size. Should you design a table that is too small for the included data, the browser will ignore your stupidity and create more room for its occupants. Go back up to the table with the background of MT. Fuji. Above it you will notice in the code that I placed a WIDTH and HEIGHT attribute in the <TABLE> tag. I gave the width a value of 400 and the height a value of 200. By doing this all the cells were made wider and higher and allowed the entire image background to be displayed. Here is an example of using WIDTH and HEIGHT in a cell tag. Notice what happens to the other cells in the row. Also, the cell that spans three rows is affected. I suppose there are some rare occasions when you would want to do this sort of thing, like possibly including small pictures in a table and adjusting rows to fit the pictures. However, I personally find these attributes more useful when used on the entire table - like the Mt. Fuji table you previously studied.
Well, the more I think about this, the more I'm convinced I should include the TABLES AS PAGE LAYOUT in its own chapter. I've never seen a lesson like I intend to bless you with anywhere on the net. That doesn't mean it is no where to be found. I've hardly been everywhere on the Internet. I want to give you lots of examples and plenty of practice, so lets end this lesson on TABLES and get on with the practice assignments.
For your first assignment you are to create a simple table from the following data. The actual data for the table will be in yellow. The red heading should not be typed. Include a border of size two. Besides Row 1, make each last name a heading as well. Center your table on the page. Print out your table and source code to hand in.
TOP CENTERED CAPTION: 1998 Seattle Mariners Statistics
Create the following table exactly as shown. The top row of table cells are headings. Three cells have links. The link can be to tomato.htm, carrot.htm and lettuce.htm. Three cells have images. Three cells have lists. You will be graded on how well you make a table that looks like the one shown. Print out your table and source code to hand in.
Assignment 3 - Lesson 9
Create the following table exactly as shown. Notice that 6 of the table cells are headings. You will be graded on how well you make a table that looks like the one shown. Print out your table and source code to hand in.
Assignment 4 - Lesson 9
Create the following table exactly as shown. The first column, last column, first row and caption should all be bolded. You will be graded on how well you make a table that looks like the one shown. Print out your table and source code to hand in.
| |||||||||||||||||||||||||||||||||