|
HTML Is Your Friend/ pg. 13 CHAPTER 10: TABLES
Frames are not the only way in HTML to create complex layouts. Tables are more versatile, if more code-intensive. They can be as simple a chart, to as complex as a layout-scaffold, like the one I use to make this lovely book. Table Basics Tables are a great tool for dividing up an area of space into smaller sections. Think of tables as egg-cartons. There's a bunch of space, divied up into 12 smaller units.
This is the essential tag for any table. It marks off an area of code as information for a table. Wow. Whatever goes in between the <table> tag and its respective end tag must be either table-orientated tags -- <tr>, <td>, <th&g
, and <caption> -- or content.
|
||||||||||||||||||||||||||||||||||||||
| Quick Information: <table> | ||||||||||||||||||||||||||||||||||||||
| Start Tag | End Tag | |||||||||||||||||||||||||||||||||||||
| Is it needed? | required | required | ||||||||||||||||||||||||||||||||||||
| How it looks: | <table> | </table> | ||||||||||||||||||||||||||||||||||||
| What it does | Delimits an area of code as a table | |||||||||||||||||||||||||||||||||||||
| Attributes:Name | Values | What it does | ||||||||||||||||||||||||||||||||||||
| border= | pixel values | Controls the thickness of the border that surrounds the table and the cells | ||||||||||||||||||||||||||||||||||||
| width= | pixel values | Controls the width of the table | ||||||||||||||||||||||||||||||||||||
| cellspacing= | pixel values | Determines how many much space between the content and the border. | ||||||||||||||||||||||||||||||||||||
| cellpadding= | pixel values | Defines the thickness of the 3D parts of the border. | ||||||||||||||||||||||||||||||||||||
| bgcolor= | #rrggbb name of the color |
Controls the color of the entire table. | ||||||||||||||||||||||||||||||||||||
|
Rows - The <tr> Tag Sorry there was no demo; you don't know enough yet. And the second thing you'll learn is the <tr> tag. The row is the first level of the table. Before you can define anything else, you must define a row. Data cells -- columns -- are the second
ayer, and are fully dependent upon the <tr> tag. This tag goes within the <table> tag's area of code.
|
||||||||||||||||||||||||||||||||||||||
|
<html> <head> <title>Tables</title> </head> <body> <table> <tr> <!--this is a row--> <!--the information for columns goes here--> </tr> <tr> <!--this is another row--> </tr> </table> </body> </html> |
||||||||||||||||||||||||||||||||||||||
|
I hope that snippet of code helps you visualize what I'm talking about. As you can see, I've coded for two rows. That's it. Right now, that's not much. Row's don't support content. Content goes into the second layer, the data cells. Think of these c
ls as a combination of rows and columns. You can't have a cell without it. And you can't display content without a cell. And you always must have rows first! Otherwise, the table doesn't work.
| ||||||||||||||||||||||||||||||||||||||
| Quick Information: <tr> | ||||||||||||||||||||||||||||||||||||||
| Start Tag | End Tag | |||||||||||||||||||||||||||||||||||||
| Is it needed? | required | required | ||||||||||||||||||||||||||||||||||||
| How it looks: | <tr> | </tr> | ||||||||||||||||||||||||||||||||||||
| What it does | Defines a row. The code for a column/data cell is enclosed by the start and end tag of the <tr> tag. | |||||||||||||||||||||||||||||||||||||
| Attributes:Name | Values | What it does | ||||||||||||||||||||||||||||||||||||
| align= | left center right |
Defines the horizontal alignment of content in the entire row. | ||||||||||||||||||||||||||||||||||||
| valign= | top middle right |
Defines the vertical alignment of content in the entire row. | ||||||||||||||||||||||||||||||||||||
| bcgolor= | #rrggbb color name |
Controls the background color of the entire row. | ||||||||||||||||||||||||||||||||||||
|
Columns and Data Cells -- The <td> Tag Now I've talked about the first layer of the table, the rows. Now I'll have to clarify some things about the <td> tag, which comprise the second layer.
|
||||||||||||||||||||||||||||||||||||||
|
<table> <tr> <td>This is some content in this cell</td> <td>Content in the second cell</td> </tr> <tr> <td>second row, first column</td> </tr> </table> |
||||||||||||||||||||||||||||||||||||||
I hope that little demo helps you see the two layers. Those <td> tags go in between the <tr> tags, as I have been saying all along. In the demo (which is a fleshing-out of the first <tr> tag demo) there are two rows, the first row th two data cells, and the second row has one data cell. I have intentionally left one of those little cells blank, so you can see that a <td> tag on one row will affect the other rows. As you can see, the content in the second row's data cell doe 't take up all of the space. There's a blank area underneath the first row's second data cell that's reservered for another cell. That's what i meant by columns. Even if you don't include the corresponding <td> tag, the browser will reserve a spac above and below the cell you declared. So be careful! One additional <td> tag can completely mess up your ideal table layout. Now onto attributes. We'll start with the newer and possibly more difficult ones that allow you much versatility. These two are rowspan= and colspan=. Both of these attributes allow a data cell
to break out of its confines of a single row and a single column, and expand -- via hostile takeover -- into the surrounding rows and columns, respectively. Actually, the two attributes only allow you to expand into rows underneath the cell, and into co
mns to the right of the cell. Perhaps it would be more difficult to control whichever direction the data cell would expand into it.
|
||||||||||||||||||||||||||||||||||||||
|
<table> <tr> <td>This is some content in this cell</td> <td rowspan=2>Content in the second cell</td> </tr> <tr> <td>second row, first column</td> </tr> </table> |
||||||||||||||||||||||||||||||||||||||
When I added the rowspan=2 attribute into the second <td> of the first <tr>, that cell took up the space that was previously blank. It spanned two rows. You can span as many rows as you want. But remember, the attribute works only on rows beneath the cell that you have added the attribute to. As you see, the reverse of the code I showed you has no effect on rowspanning. |
||||||||||||||||||||||||||||||||||||||
|
<table> <tr> <td>This is some content in this cell</td> </tr> <tr> <td>second row, first column</td> <td rowspan=2>Content in the second cell</td> </tr> </table> |
||||||||||||||||||||||||||||||||||||||
The sibling tag of the rowspan= attribute, the colspan=, attribute performs the same function, except it affects columns instead of rows. The idea is the same, except this attribute forces the cell o take up space on the cells to its right. |
||||||||||||||||||||||||||||||||||||||
|
<table> <tr> <td>This is some content in this cell</td> <td>Content in the second cell</td> </tr> <tr> <td colspan=2>second row, first column</td> </tr> </table> |
||||||||||||||||||||||||||||||||||||||
Now there is the same effect: it seems that the borders between the cells has disappeared. Thgis time, however, the borders that seperate cells on the same row are gone, instead of the border between cells in the same column. By using rowspan= and colspan= together, you can produce some very complex and very infomative tables, like the one below:
Now on to the more standard attributes of the <td> tag. We'll start off with the height= and width= attributes. As you know, this will set the height and width of the individual cell. Values
e once again in pixels.
Instead of wrapping the text, as in the cell on the left, the long email address is not forced to wrap.
|
||||||||||||||||||||||||||||||||||||||
| <table bgcolor=#dd0000> <tr bgcolor=#0000dd> <td>a</td> <td>b</td> <td bgcolor=#009900>c</td> </tr> <tr> <td>d</td> <td>e</td> <td>f</td> </tr> </table> |
|
|||||||||||||||||||||||||||||||||||||
|
There you go. The bgcolor= attribute affects the data cell first, then the row, then the table. And that ends your lesson on the <td> tag. |
||||||||||||||||||||||||||||||||||||||
| Quick Information: <td> | ||||||||||||||||||||||||||||||||||||||
| Start Tag | End Tag | |||||||||||||||||||||||||||||||||||||
| Is it needed? | required | required | ||||||||||||||||||||||||||||||||||||
| How it looks: | <td> | </td> | ||||||||||||||||||||||||||||||||||||
| What it does | Defines a data cell in a row. Note though that adding a cell will create a corresponding space in all other rows. | |||||||||||||||||||||||||||||||||||||
| Attributes:Name | Values | What it does | ||||||||||||||||||||||||||||||||||||
| rowspan= | # of rows to span | Allows a data cell to take space from rows beneath the cell. It takes up the space reserved for a column. | ||||||||||||||||||||||||||||||||||||
| colspan= | # of columns to span | Allows a data cell to take space from data cells to the right of the cell. It takes up the space reserved for a other cells, which was defined in other rows. | ||||||||||||||||||||||||||||||||||||
| height= | pixel values | Controls the height of the cell. | ||||||||||||||||||||||||||||||||||||
| width= | pixel values | Controls the width of the cell. | ||||||||||||||||||||||||||||||||||||
| align= | left center right |
Defines the horizontal alignment of content in the entire row. | ||||||||||||||||||||||||||||||||||||
| valign= | top middle bottom |
Defines the vertical alignment of content in the entire row. | ||||||||||||||||||||||||||||||||||||
| nowrap | none | Prevents content from wrapping. | ||||||||||||||||||||||||||||||||||||
| bcgolor= | #rrggbb color name |
Controls the background color of the entire row. | ||||||||||||||||||||||||||||||||||||
|
Table Headers -- <th> Sure, the data cell and its <td> tag are great, but what if you want to make a cell with bold text, so that it's similar to a title? Well, you could just use the <b> tag to make the text bold, but of course, there's a more efficient way do it with the <th> tag. Let's see it in action: |
||||||||||||||||||||||||||||||||||||||
| <table> <tr> <th>This is a header cell.</th> </tr> <tr> <td>This is a data cell.</td> </tr> </table> |
|
|||||||||||||||||||||||||||||||||||||
|
Other than that, the <th> tag behaves exactly like the <td> tag. That is why this is the easiest tag to learn. And since it's so similar, the header tag has all the same attributes as the data cell tag, so I won't bore you with another c
rt. Just look above, and substitue in <th> where you see <td>.
Remember in the introduction to this chapter how I included a sample table? Well, if you look carefully, you will see that there is text below the table. That's the caption. You can place content (mostly text, but also lists, l
ks, etc.) above or under a table, as a caption.
|
||||||||||||||||||||||||||||||||||||||
| <table> <caption align=bottom>This is a caption </caption> <tr> <th>This is a header cell. </th> </tr> <tr> <td>This is a data cell.</td> </tr> </table> |
|
|||||||||||||||||||||||||||||||||||||
| Quick Information: <caption> | ||||||||||||||||||||||||||||||||||||||
| Start Tag | End Tag | |||||||||||||||||||||||||||||||||||||
| Is it needed? | required | required | ||||||||||||||||||||||||||||||||||||
| How it looks: | <caption> | </caption> | ||||||||||||||||||||||||||||||||||||
| What it does | Creates a caption. This tag must go in between the <table> tag and the first <tr> tag. | |||||||||||||||||||||||||||||||||||||
| Attributes:Name | Values | What it does | ||||||||||||||||||||||||||||||||||||
| align= | top bottom |
Defines whether the cacption goes above or below the table. Default if bottom. | ||||||||||||||||||||||||||||||||||||
|
And now we conclude this chapter about tables. Tables are a great, and more versatile tool than frames. Tables can be used for simple things, such as the straightout table of information, to more complex functions, such as controlling the layout of an entire webpage, such as this book. But now it is time to add even more pizazz to your website with multimedia. |
||||||||||||||||||||||||||||||||||||||