< Styling Horizontal Rules | Fonts >
21 Nov 2006 > 0 Comments
Tables are used to display tabular data.
They are also used for website layouts, but this practice is semantically incorrect, and (nowadays) completely unnecessary. See chapter one of this tutorial.
You can give your tables a small amount of styling, by adding a border to the <table> and the <td> elements, like this:
table, td {border: 1px solid #000;}
A simple table consists of three main elements.
Note: Other Table tags, including, <th>, <thead>, and <tfoot>, go beyond the range of this tutorial.
<table> TagThe <table> tag defines the table element:
<table> </table>
<tr> TagThe <tr> tag defines a horizontal table row:
<table>
<tr>
</tr>
</table>
<td> TagThe <td> tag defines a table cell. All tabular data is contained within <td>
tags:
<table>
<tr>
<td> table data goes here </td>
</tr>
</table>
The above markup will render a basic single cell table:
| table data goes here |
The <td> tag has two important attributes:
The colspan attribute is used when you want a table cell (within a <tr>) to span two or more table cells (within another <tr>):
<table>
<tr><td colspan="2">Colspan</td></tr>
<tr><td>1</td><td>2</td></tr>
</table>
| Colspan | |
| 1 | 2 |
rowspan is a little more complicated, but easy to get the hang of.
If you want a table cell (<td>) on the left to span two table cells on the right, table cell 1 must be contained within the same table row (<tr>). Then create another table row to contain table cell two.
<table>
<!-- Table Row One -->
<tr><td rowspan="2">Rowspan</td><td>1</td></tr>
<!-- Table Row Two -->
<tr><td>2</td></tr>
</table>
| Rowspan | 1 |
| 2 |
If you want one table cell (<td>) on the right to span two table cells on the left, you need to place table cell 1 (first) and the table cell containing the rowspan attribute (second) on the same table row (<tr>), and then create another row for table cell 2.
<table>
<!-- Table Row One -->
<tr><td>1</td><td rowspan="2">Rowspan</td></tr>
<!-- Table Row Two -->
<tr><td>2</td></tr>
</table>
| 1 | Rowspan |
| 2 |
Rowspan drives me nuts, but makes sense when you think about it
< Back to everything
Keep going… >
Mahud Version 5 © 2006-2008 > powered by Mr Whippy Wordpress