|
Before we get to the other stuff, let's take
a look at how to define the width of your table to help you
get the table the
size you would like it to be. To do this, add the width=" " command
to your <TABLE> tag. Place the number of pixels wide you would
like the table to be between the quote marks. So, if you wanted
a table 600 pixels long, you would do this:
<TABLE width="600" border="2">
<TD>
This table really long!
</TD>
</TABLE>
This table has just a little text, but stretches all the way
to 600 pixels, because we told it to!
In the last section we left off wondering how
to align the contents of your table cells. What if you wanted
your contents
to be aligned to the center or to the right? To do this, you
add the align=" " command to your <TD> tag. You can use the
command three ways:
- align="left"
Aligns your cell contents to the left.
- align="center"
Aligns your contents to the center.
- align="right"
Aligns your cell contents to the right.
Let's take a look at a table using these commands:
<TABLE width="450" border="2" cellspacing="7" cellpadding="7">
<TD align="center">
I'm in the center!
</TD>
<TD align="right">
I'm aligned to the right!
</TD>
</TABLE>
The first cell will have the text in the center, while the
second cell has text aligned to the right.
| I'm in the center! |
I'm aligned to the right! |
You can also use another alignment command for
your cells, the vertical alignment command. It looks like this:
valign=" " .
Here's what it can do:
- valign="top"
Aligns contents to the top of the cell.
- valign="middle"
Aligins contents halfway between the top and bottom of the cell.
- valign="bottom"
Aligns contents to the bottom of the cell.
Here is a sample table using the vertical alignment commands:
<TABLE width="550" border="2" cellspacing="7" cellpadding="0">
<TD align="center" valign="top">
I'm on top! <br>
So I start on top! </TD>
<TD align="center" valign="middle">
I'm in the middle
</TD>
<TD align="center" valign="bottom">
I start at the bottom.
</TD>
</TABLE>
The table looks like this:
I'm aligned to the top!
So I start on top! |
I'm in the middle. |
I start at the bottom. |
The vertical alignment commands only come in useful if your
table cells don't have the same number of lines inside each cell.
Since I had 2 lines of text in the first cell while the others
had one line, the vertical alignment made a difference in how
the table looked. If I had only used one line of text in each
cell, the vertical alignment commands wouldn't make any difference.
So, how about stretching out the rows and columns?
Well, now you get two more commands for the <TD> tag, rowspan
and colspan.
- rowspan=" "
Defines the number of vertical table rows the cell should take up. Place
your number inside the quote marks.
- colspan=" "
Defines the number of horizontal columns the cell should take up.
Suppose you wanted your table to do this:
| Nice guy, isn't he? |
|
| Met him at the zoo. |
To make it happen, you use the rowspan command
in your table cell containing the image. You set rowpan="2" and
the cell with the picture takes up 2 table rows. Here is the
code:
<TABLE border="2">
<TD align="center">
Nice guy, isn't he?
</TD>
<TD rowspan="2" align="center">
<IMG SRC="../images/monkey.jpg">
</TD>
<TR>
<TD align="center">
Met him at the zoo.
</TD>
</TR>
</TABLE>
Now, suppose you want a table like this:
| Star Wars Characters |
| Luke Skywalker |
Princess Leia |
Han Solo |
This time you use the colspan command and set
colspan="3".
You get the first cell to stretch across the other three on the
second row below it. Here is the table code for this:
<TABLE border="2" cellpadding="5">
<TD colspan="3" align="center">
<B>Star Wars Folks</B>
</TD>
<TR>
<TD align="center">
Luke Skywalker
</TD>
<TD align="center">
Princess Leia
</TD>
<TD align="center">
Han Solo
</TD>
</TR>
</TABLE>
Well, now here's the part everyone has been
waiting for.....changing the background color of a table cell!
You can change the background
color of the entire table, each row, or each cell. To change
the background color of the table, add the bgcolor=" " command,
just like in the body tag:
<TABLE border="5" bgcolor="red">
<TD>
Red Rules!
</TD>
</TABLE>
Now the whole table has a red background:
To change the color of each cell, add the bgcolor
command to each <TD> tag you want to change:
<TABLE width="75" border="2">
<TD bgcolor="red">
red
</TD>
<TD bgcolor="blue">
blue
</TD>
</TABLE>
Now we have two different coloured cells:
And finally, to change the colour of a table
row, add the bgcolor command to the <TR> tag:
<TABLE width="200" border="2" >
<TR bgcolor="red">
<TD> red </TD>
<TD> red again... </TD>
</TR>
<TR bgcolor="blue">
<TD> blue </TD>
<TD> blue again! </TD>
</TR>
</TABLE>
And now the rows are different colours:
| red |
red again... |
| blue |
blue again! |
Pretty neat stuff, isn't it? You can design
calendars, price lists, or whatever comes to mind using tables.
|