|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Using tables to
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<table border="2" bgcolor="#99CC99"> <tr> <th>Header</th> <td>COLUMN 2</td> </tr> <tr> <td>One Cell</td> <td>Another Cell</td> </tr> <tr> <td>Last Row</td> <td>Final Cell</td> </tr> </table> |
The code presented on the left will produce the table shown below. This is an extremely simple table with just six cells, but you can easily see how to extend the code to produce a table of larger dimensions. Note that there are two attributes specified within the <table> tag. These two define the border width and the background colour, and are explained below, along with other table attributes.
|
There are quite a number of table attributes, which give you excellent control over the presentation of your tables. These are all implemented in Netscape and Internet Explorer, though these browsers have other nonstandard attributes too. The following attributes should all be placed within the <table> tag, and you can of course include as many as you want at once. Here's a list of the most common ones you'll need for normal presentation:
| border="x" | specifies the width of the table's outside border in pixels, as seen above |
| bgcolor="#RRGGBB" | specifies the default background colour of the table, as seen above |
| cellspacing="x" | specifies the amount of space between the edges of adjoining cells in pixels |
| cellpadding="x" | specifies the size of the cell's text surround space in pixels |
Similarly, there are lots of attributes which can be placed within the tags <tr> and <td>, in order to give you control over individual rows and cells within your table. Here are the most common attributes:
| align="position" | specifies whether cell text is positioned to the left, right or center |
| valign="position" | alters the vertical alignment of text to top, middle or bottom of the row or cell |
| bgcolor="#RRGGBB" | overrides the table's background colour to create individual cells or rows of another background colour |
| background="filename" | modern browsers allow you to tile images across the background of individual table cells, which can be a useful trick |
| colspan="x" | specifies how many columns a cell should span, which is useful for table titles running across the entire table width |
| rowspan="x" | similarly, this specifies the numbers of rows a particular cell should span |
As stated before, almost all modern web pages use advanced table layout techniques to offer control their overall presentation. It's very common to see information and pictures neatly aligned against invisible vertical lines, or chunks of text positioned all over the place, with no obvious clue as to how it was done. The answer is simple: almost all of these effects can be produced using tables in specilised ways. This very guide features plenty of these techniques, as you should be able to see. The simplest example to start with is the creation of simple columns. To do this, simply use tables with particular attributes, in the way shown below. Modifying this basic syntax allows you to create any number and size of columns:
<table cols="2" border="0" cellspacing="5"> <tr valign="top"> <td width="50%"> . . . </td> <td width="50%"> . . . </td> </tr> </table> |
The code to the left will split the browser window into two usable columns, into which you can then place everything you'd normally put on your page, including text, lists, images, and further tables. There is one pair of <td> tags, for each column. In this piece of code, the valign attribute ensures that the text will start at the top of the "row", rather than being centred vertically. Here, a row will in fact be the entire contents of both columns, since each entire column of information is effectively a single cell within a giant, invisible table. The width attribute specifies the percentage width of the browser window that each column (or cell) should occupy. If you use the "cols" attribute, they're not strictly necessary, since the browser will automatically create equal width columns. |
Remember that when using columns, all specified HTML effects will take place within the context of their own column only, rather than the entire browser window. For example, <hr> lines will only span the current column, and align=right attributes will align items to the right of the column, not the entire window. In effect, each column becomes an independent working space, within which you're arranging your information. The previous section of code could have been expanded to include another row, and this row could have had three columns of differing relative widths. This is the essence of nesting: you can have a giant table laying out the whole page, and within each cell of that table, further tables laying out their own areas. To achieve a certain effect, you might need quite a complex nested table structure, and it's easy to get confused, so go slowly as you're developing such pages!
For a more interesting example, let's take a look at how this very page is structured using tables. Because we wanted the upper and lower link sets to be in a column of their own, and a split column at that, we had to create a table with multiple rows and columns, but with one giant cell (the main body of the page text) that spanned two rows. Below, we've presented the outline code for the table structure of this page (with comments in green), along with a diagram highlighting the structure for you, with each cell in a different colour:
<table width="100%" border="0"> <tr> <td> </td> <td width="85%" align="right"> insert the logo (aligned RIGHT) </td> </tr> <tr> <td width="15%" valign="top"> upper links (vertically aligned TOP) </td> <td rowspan="2" width="85%" valign="top"> main body of the page occupies this cell, which spans two rows, and thus effectively occupies the right cell of the NEXT row </td> </tr> <tr> <td valign="bottom"> lower links (vertically aligned BOTTOM) occupy the ONLY cell in a new row, because the right cell is already taken </td> </tr> </table> footer and credits are added at the bottom, outside the page's layout table |
|
|||||||
So now the secret is out, and you can see that the flexibility is enormous. In theory, you'll be able to lay out your information almost exactly as you please, using just a small number of HTML elements, and probably quite a lot of testing as you go along! Indeed, it is hard to include further useful examples on this, so the best way to learn is to experiment yourself, or to go hunting for more comprehensive documentation on using tables and columns. The pointers in the "More Help" section will help you. Alternatively, look at the source of this document to see some relatively simple table layout in action.
<caption align="position"> . . . </caption>
Finally, this adds a name or title to a table, which is clearly more useful for "traditional" data tables, rather than those used to layout an entire page! By using top or bottom with the align attribute, you can put the title above or below the table itself. Caption text is automatically rendered in bold type, and centered horizontally with the table.
| Welcome | |
| Overview | |
| Structure | |
| Text | |
| Images & Links | |
| Lists | |
|
Tables | |
| Frames | |
| Style Sheets | |
| More Help | |
| Credits |
HTML Guide © 1996-2000
Alastair Stevens
Developed at the MRC Biostatistics
Unit, Cambridge, UK