Home
What is HTML
Getting Started
Lesson 1 - Text
Lesson 2 - Links
Lesson 3 - Images
Lesson 4 - Tables
Lesson 5 - Forms
Lesson 6 - Color
Lesson 7 - CSS
Testing
Ready To Go Public?
Advanced Topics
Where to Learn More

Lesson 4 - Tables

The table is a very important HTML element. It serves many purposes for web developers. They can be used for anything from breaking up images into smaller pieces to organizing large amounts of information into a readable and understandable form to laying out entire pages. For all that HTML tables can do, they are actually relatively easy to code and to manipulate. The table has several tags that we will discuss in this lesson and a couple other which we will not. We are going to discuss the table, tr, td and the th tags and some of their popular attributes.

<table>
This is arguably the most important tag needed to create an HTML table. Everything in a table (rows, columns, cells) is enclosed in an opening and closing table tag. The border attribute accepts a numeric value for the size of border to surround each cell in the default border size is 1 pixel. The cellpadding attribute specifies the space in pixels between the content of a cell and its cell border. The cellspacing attribute specifies the space in pixels between adjoining cells, the default is 2 pixels. Other important attributes which are self explanatory are width and height.

<tr>
This is the table row tag. The table row element groups all table data cells of a row into one unit. It can be used to specify certain attributes that pertain the all cell in that row such as align and valign (vertical align; values can be top, middle, bottom and baseline). The table row element tells the browser when to create a new row as it renders the table.

<td>
This is the table data cell tag. This tag is used to specify the smallest unit of the table (the actual data cell). The content of the cell is specified between the correlating opening and closing table data cell tags. This tag has width and height attributes, as well as a couple of new ones that we have not yet seen in any other elements called colspan and rowspan. The colspan attribute allows a single cell to span across multiple columns, the value of the colspan attribute should be set to a number equal to the desired amount of columns to be stretched across. The rowspan attribute is the same as the colspan attribute except that it specifies the number of columns for the cell to be stretched across vertically.

<th>
This is the table header tag. This tag can substitute any table data cell tag. Individual browser may render this tag differently, but for the most part it will be displayed as bold horizontally and vertically centered text. It shares all of the table data cells attributes.

Example:
Please look over the following example (source and formatted page). Feel free to experiment with the source code. I attempted to address some of the problems beginners tend to have when first learning how to use tables, particularly with gaps between pictures that they wanted to appear to be a single picture.

View Source
View Formatted Page