Introduction to HTML tables

Tables are HTML structures that allow greater control over the positioning of elements in your Web pages. As an introduction. this column will explain how to specify the layout, cell dimensions, spacing, colour, and alignment of your tables. In next month's column, we shall investigate increasingly advanced techniques and explore the importance of tables in laying out your entire Web page.

Table commands and structure

In FIGURE 1 I have aligned the code in a certain way. Although not essential, spacing like this is extremely beneficial when troubleshooting your tables as it aids you in ensuring commands have been correctly opened and closed. (Note: this code would be inserted into the BODY of your Web page.)

The first tag required is <TABLE> and the last is </TABLE>. I have given the table a border property of one so you can see an outline of the rows and cells. You can see that the <TR> command (TR = 'Table Row') represents the start of a new row (going across the page). All table tags are coupled, so rows need to be closed off with </TR> when completed. Within a row, <TD> then creates columns (going down the page - TD = 'Table Data' or 'Table Down'). These must always be contained within an open <TR> to work. The intersection of the row and column creates cells, which contain your data.

With reference to FIGURE 1, consider that <TABLE> draws a box in the browser's memory. For each row (<TR>) the browser inserts a horizontal line, and each <TD> in that row creates a corresponding number of columns to contain data. Therefore, since we can count three coupled <TR> tags, this table must have three rows. Each of these rows has three cells because they each contain three coupled <TD> tags.

Table dimensions

Without definition, cells autowrap to accommodate their contents and tables autowrap to fit the cells. You can set the dimensions of both tables and individual cells by using the width and height properties. For example:

<TABLE border="1" width="300" height="250">

Or to set the dimensions of a particular cell :

<TD width="300" height="250"> cell contents here</TD>

Cellspacing and cellpadding

By adding the cellspacing property to the TABLE command, you can specify the space between the table border and the cells. By contrast, the cellpadding property inserts space within the cell, i.e., between the contained object and the cell border. When not specified, a default value of two pixels is automatically implemented. Consequently, to have no space whatsoever, you must include these properties as follows:

<TABLE border="1" cellspacing="0" cellpadding="0">

FIGURE 2 illustrates the difference between cellspacing and cellpadding.

Bgcolor and background

Inserting bgcolor and/or background properties into your TABLE command and/or your <TD> tags allows you to brighten things up. If you set the colour of a particular cell, it will appear on top of the table colour. The following example would create a blue table with one orange cell:

<TABLE border="1" bgcolor="blue">
<TR>
<TD>One</TD>
<TD bgcolor="orange">Two</TD>
<TD>Three</TD>
</TR>
</TABLE>

Similarly, if I were to give the table a background graphic, the colour of the cells (if specified) would overlay the coded image.

Note: be careful when using these properties in conjunction with cellspacing. Netscape will not colour the spaced area, whereas Internet Explorer will.

Align and valign

The align property refers to an item's horizontal position. The most common values are left, right or center (note the spelling). Valign refers to the vertical alignment, the associated values being top, bottom and middle. If not specified, objects automatically inherit the property of align="left" and valign="center".

By using these properties within your TABLE command, you can state the position of the entire table on the Web page. Using it within TD will set the location of data in individual cells.

The following example would place the table in the top-middle of the page:

<TABLE border="1" align="center" valign="top">

This example would force the contents of the cell to the bottom-right corner of the cell:

<TD align="right" valign="bottom">Cell contents here</TD>

Join the newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Heidi Woof

PC World
Show Comments

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Ben Ramsden

Sharp PN-40TC1 Huddle Board

Brainstorming, innovation, problem solving, and negotiation have all become much more productive and valuable if people can easily collaborate in real time with minimal friction.

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Kurt Hegetschweiler

Brother PocketJet PJ-773 A4 Portable Thermal Printer

It’s perfect for mobile workers. Just take it out — it’s small enough to sit anywhere — turn it on, load a sheet of paper, and start printing.

Featured Content

Product Launch Showcase

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?