Cell One | Cell Two |
Or tables can be used as a grand formatting tool for your entire page.
There is a group of tags associated with tables. The first is the <table> tag. It has a corresponding end tag, and it indicates that the content inside will be formatted according to table rules.
<table>
</table>
Each Table is then divided into a series of rows. You must first divide the table into rows before you create individual cells. To create rows, we use the TR (for Table Row) tag:
<table>
<tr>
</tr>
</table>
You can create as many rows as you like. In the colored table above we have two rows
Within each row, we can then divide the rows into cells. The tag for creating a cell is <td> (TD for Table Data). You can create as many cells within a row as you like. Content (i.e. text, images, etc) must be in a cell. You cannot just put it in a row without a cell.
<table>
<tr>
<td>This is the First Cell</td>
<td>This is the Second Cell</td>
</tr>
</table>
If you have multiple rows, there must be the same number of cells in each rows. If you want to have fewer cells in one row, you can use the colspan attribute. This means that a cell will span several columns. In the First Table example above, our first row spans two columns. The code looks like this:
<table> <tr> <td colspan=2><center>Our First Table</td> </tr> <tr> <td>Cell One</td> <td>Cell Two</td> </tr> </table>
Notice that there are two sets of TDs in the second row, and only one in the first row. The number of columns in a table is determined by the number of columns in the row with the most columns. Every other row must have that many columns made up by the colspans of each cell (default is one).
There is also a rowspan option where you can make a cell stretch vertically over multiple rows. An example would look like this:
This is the first TD | This is the second TD |
This is the third TD |
The code for that looks like this:
<table border=1> <tr> <td rowspan=2>This is the first TD</td> <td>This is the second TD</td> </tr> <tr> <td>This is the third TD</td> </tr> </table>Notice that we only have one TD in the second row. This is because the first cell is filled by the cell from the first row that stretches down into the second row.
With that out of the way, we can look at some simpler attributes.
<table border="1">. Be cautious about using borders. They tend to look very dated if used in the default way. CSS gives more options for table borders which may help you look a bit more modern.
This is the First Cell | cell 2 |
comes from this code
<table border="1"> <tr height="100"> <td width="50%">This is the First Cell</td> <td width="50%">cell 2</td> </tr> </table>
You CANNOT create cells in the same column that have different widths, nor cells in the same row with different heights.
<table><tr background="red"><td>...
<table><tr><td background="images/bg.gif">
Bottom | top |
<table border=1> <tr> <td valign="bottom" height="100">Bottom</td> <td valign="top">top</td> </tr> </table>