Adam's Advanced HTML Guide- Tables

Fortunately, HTML tables are a lot easier to create than their wooden counterpart. Tables are used extensively on many web sites to bring structure to a page. There are small tables and big tables- small ones are often used simply to encompass and organize a section of the page, such as a chart or spreadsheet-like data; large ones are used to bring organization to the entire document (as seen in the frontpage of Yahoo). A HTML table may look something like the below:

This is a table

leaf.gif (1184 bytes) How to create a table

Begin by getting a saw and a huge piece of log...just kidding! To create a HTML table, we have to first look at the basic tags necessary to create one:

Tag Function
<table></table> Defines a table
<tr></tr> Defines a row within the table
<td></td> Defines a cell within the table

Every single table you construct will consist of the above three tags, no matter the complexity.

The below shows the syntax of the most basic form of table, with only one row and cell:

<table>
<tr>
<td>Some text in the cell</td>
</tr>
</table>
Some text in the cell

We began our journey with the <table> tag. Continuing on, we used the <tr> tag to define a row, and used one <td> tag inside to specify that this row should contain only one cell. All content, such as text (in the above example), or otherwise, go into the <td> tag, and only the <td> tag.

If you're a little confused at this stage, its ok. Practice makes perfect. lets create a table now with one row, but two cells inside of it:

<table>
<tr>
<td>Cell #1</td>
<td>Cell #2</td>
</tr>
</table>
Cell #1 Cell #2

To continue on with this table madness, lets now create a table with two rows, each with two cells in them (4 cells in total). Take note of how the <tr> and <td> tags are positioned:

<table>
<tr>
<td>Cell #1</td>
<td>Cell #2</td>
<tr>
<td>
Cell #3</td>
<td>
Cell #4</td>
</tr>
</table>
Cell #1 Cell #2
Cell #3 Cell #4

Since we want two rows, there are two <tr> tags present. Since we want to cells in each of the rows, there are two <td> tags present in each <tr>. Simple as that!

Red_CurlyC035.gif (285 bytes) Table Attributes

Tables, like many tags in HTML, accepts various attributes that control how a table is to look. Lets list some of the basic table attributes:

attributes Function
border=? Specifies the border width of the table, in pixels.
width=? Specifies the width of the table or cell, in pixels or %.
height=? Specifies the height of the. table or cell, in pixels or %
cellpadding=? Specifies the distant between the cell and the content inside.
cellspacing=? Specifies the spacing between each cells, in pixels.

Here's an example that uses some of the above attributes:

<table border=3 width=200 cellpadding=20>
<tr>
<td>Cell #1</td>
<tr>
<td>
Cell #2</td>
</tr>
</table>
Cell #1
Cell #2

Tables are really quite easy to construct, once you get the hang of it.

 

Back Home

Recommended resources