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!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Heidi Woof

PC World
Show Comments

Brand Post

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Emily Tyson

MSI GE63 Raider

If you’re looking to invest in your next work horse laptop for work or home use, you can’t go wrong with the MSI GE63.

Laura Johnston

MSI GS65 Stealth Thin

If you can afford the price tag, it is well worth the money. It out performs any other laptop I have tried for gaming, and the transportable design and incredible display also make it ideal for work.

Andrew Teoh

Brother MFC-L9570CDW Multifunction Printer

Touch screen visibility and operation was great and easy to navigate. Each menu and sub-menu was in an understandable order and category

Louise Coady

Brother MFC-L9570CDW Multifunction Printer

The printer was convenient, produced clear and vibrant images and was very easy to use

Edwina Hargreaves

WD My Cloud Home

I would recommend this device for families and small businesses who want one safe place to store all their important digital content and a way to easily share it with friends, family, business partners, or customers.

Walid Mikhael

Brother QL-820NWB Professional Label Printer

It’s easy to set up, it’s compact and quiet when printing and to top if off, the print quality is excellent. This is hands down the best printer I’ve used for printing labels.

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?