What is this Session about?
By the end of this session you should...- Know how tables work
- Know some clever tricks with tables
- Know when not to use tables
How do tables work?
Tables are more than just a single tag, they are a set of tags. <table>, <tr>, <th> and <td>. Each has a specific meaning and purpose in the grand scheme of all things table related.| Tag | Purpose |
|---|---|
| <table> | It is the block of space that the table takes up and thus used to show where the table is |
| <tr> | Table row, each pair of <tr> tags is one horizontal row on the table |
| <th> | Table Heading, used (optionally) in the first row of cells on the table, they look a little different from the other cells, they are placed inside a pair of <tr> tags along with the other cells in their row |
| <td> | Table cell, each pair of <td> tags holds the data of one cell, they are placed inside a pair of <tr> tags along with the other cells in their row |
Lets look at an example table shall we?
<table border="1" cellspacing="5" cellpadding="5">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1:1</td>
<td>Data 2:1</td>
</tr>
<tr>
<td>Data 1:2</td>
<td>Data 2:2</td>
</tr>
</table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1:1</td>
<td>Data 2:1</td>
</tr>
<tr>
<td>Data 1:2</td>
<td>Data 2:2</td>
</tr>
</table>
| Header 1 | Header 2 |
|---|---|
| Data 1:1 | Data 2:1 |
| Data 1:2 | Data 2:2 |
<table border="1" cellspacing="5" cellpadding="5">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Data 1:1</td>
<td>Data 2:1</td>
<td>Data 3:1</td>
</tr>
<tr>
<td>Data 1:2</td>
<td>Data 2:2</td>
<td>Data 3:2</td>
</tr>
</table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Data 1:1</td>
<td>Data 2:1</td>
<td>Data 3:1</td>
</tr>
<tr>
<td>Data 1:2</td>
<td>Data 2:2</td>
<td>Data 3:2</td>
</tr>
</table>
| Header 1 | Header 2 | Header 3 |
|---|---|---|
| Data 1:1 | Data 2:1 | Data 3:1 |
| Data 1:2 | Data 2:2 | Data 3:2 |
Some clever table tricks
If at this point you cannot work out roughly how to write a table with 4 rows (including header) and 5 columns then I advise you stop reading and come back to this bit later as without that understanding, this will almost certainly confuse you more.Merging Cells
Using the colspan or rowspan attributes you can cause a cell to grow into the one next to it. Both are by default a value of 1, if you give a <td> tag a colspan value of "2" then it will count as being two cells, it in effect absorbs the one next to it (so you also need to remove the now surplus <td>/<th> tag). It's probably best shown by example
<table border="1" cellspacing="5" cellpadding="5">
<tr>
<th colspan="2">WIDE LOAD!</th>
<th>Normal Load</th>
</tr>
<tr>
<td rowspan="2">Tall Data</td>
<td>Data</td>
<td>Data</td>
</tr>
<tr>
<td>Data</td>
<td>Data</td>
</tr>
</table>
<tr>
<th colspan="2">WIDE LOAD!</th>
<th>Normal Load</th>
</tr>
<tr>
<td rowspan="2">Tall Data</td>
<td>Data</td>
<td>Data</td>
</tr>
<tr>
<td>Data</td>
<td>Data</td>
</tr>
</table>
| WIDE LOAD! | Normal Load | |
|---|---|---|
| Tall Data | Data | Data |
| Data | Data | |
You can combine colspan and rowspan together to make a cell both taller and wider at the same time. You can also use the colspan and rowspan attributes in <th> tags just as you can in <td> tags.
When not to use tables
Some bright spark came up with a very cunning use for tables, you can use them to layout a page in a manner similar to this one. You can get a nice menu-bar on the side with the main page content not running into it, spaced as you wish. However, there are problems with doing this.- It's very easy to get wrong if you have a complicated table
- If you use Tables inside the page in addition to a table for layout it can cause problems
- A table cell only displays when all the content inside it is loaded, normally a page will display as it loads
- Partially sighted users have a very hard time reading the page with tables
- Maintaining the page is very hard


