Home : Tutorial Nine : Next Tutorial

Tutorial Nine: Tables

Why Use Tables?
Building A Table
Size - Width and Height
Borders
Cellpadding and Cellspacing
Cell and Row Spanning
Aligning Table Content
Adding Background Color
Adding an Background Image
Tables Within Tables
Blank Cells
Cell height
Captions
Table Headers
Adding Images as Content
Preventing Text Wrapping
Table Rules
Conclusion

As previously mentioned, tables are simply containers which can assist the challenging task of presenting your document in an appealing manner, providing more precise control in the placement of images and text, or even creating newspaper style columns. The majority of HTML tutorials class tables as 'advanced', whereas they are now one of the most basic methods of good page creation - not difficult to learn they are nonetheless a lengthy topic . Most HTML editing software allows you to create tables quickly via table 'wizards', but it is important to understand the correct table syntax. HTML 4.0 has introduced a number of new tags that are not consistently implemented across browsers and therefore will not be covered in this tutorial.

Why use Tables?

Because of the limitations of HTML - remember it was never designed to be a layout tool - tables have moved away from their original purpose which was to contain tabulated data, although they are still useful in that regard. It is possible to create a simple one cell table - like the one used to contain this tutorial - or a more complex structure such as the main CDL page.

One of the main reasons for introducing a table is to create margins for text. HTML text will naturally spread out to the edges of the browser window. This can be controlled somewhat by the marginwidth, marginheight, leftmargin and topmargin attributes of the <body> tag, but the right hand margin is governed by the width of the browser. It is an uncontrollable element. We can force-contain text by inserting it into a table, its width being set to either a percentage of the browser window - allowing some resizing - or a specific pixel width - allowing no resizing. This can be a crucial requirement for sites which are design-intensive. It is also much easier to read text that is justified and aligned, rather than floating off into the page margins.

This simple 2 cell table allows us to align some text with a graphic in the cell to the right. You do not see the cells as the table borders are invisible. The entire table has been aligned to the centre of the page. We could not do this without a table. Anything can go inside a table.

It is sometimes necessary to slice a graphic into seperate pieces - to incorporate javascript image flips or to specify different hyperlinks for each section. This is another topic in itself, however tables are used to recompile the image to create the illusion that it is whole - rather like putting a jigsaw together.

Building a Table

Because they can end up being quite complex, it it always a good idea to plan your table carefully on paper as a first step - consider how many rows <tr> and columns <td> you may require. Alterations can be made at any time:

Tables are built from left to right, beginning with <table> and ending with </table> - this is the actual container for the entire table. Next we create a row <tr> and within that row a data cell <td> or column. Each data cell must be closed by </td> and the row by </tr> - ommiting an end tag is asking for trouble and can affect CSS. Netscape in particular will not know what to do with a table if the ending </table> is omitted and will not display much of the page. The following example is a table consisting of 2 rows, each containing 2 data cells.

Cell 1
Each cell is created separately. There is no limit to the number of rows and columns you may add.

Cell 2
It is a good idea to specify a set width for your table, dependent on the target browser window size.

Cell 3
Any kind of formatting can apply to the text within a table, and cell content can be aligned in a variety of ways.
Cell 4
Font formatting must be specified within every cell, as the table contents are separated from the body of your document.

 

Let's look at the markup for this simple table:

<table>The table is opened


<tr>The first row is begun


<td>A data cell is created. Text for cell 1 is typed here
</td>The first data cell is closed

<td>Another cell is created. Text for cell 2 is typed here
</td>The second data cell is closed


</tr>The first row is closed
<tr>A new row is begun


<td>A third data cell, in this new row. Text for cell 3
</td> The third cell is closed

<td>
The fourth and final cell is opened. Text for cell 4
</td>
The final cell is closed


</tr>
The second row is closed


</table>
The table is completed

Indenting your table elements in this way is imperative when dealing with more complex structures, and it is a good habit to create HTML in a 'tidy' manner, keeping tags on seperate rows for instance - finding an incorrect or missing tag can otherwise be a nightmare task. Any extra spaces or breaks will not appear on the page itself: as previously discussed they are ignored by the browser. Good HTML software will do this automatically for you and will even color code different elements.

Table Size

A size can be set for an entire table or for individual cells, either in fixed pixels or as a percentage of the browser window or table - whatever size that may be. The invisible table containing this tutorial is set to 600 pixels, <table width="600"> - no matter how big or small you make the browser window the table width remains set. If you wish the table to resize itself automatically depending on the browser window, percentage must be used instead, <table width="90%"> - in this case the browser will calculate 90% of it's current size and adjust the table width accordingly. Not specifying a width defeats the purpose of using tables in the first place, as the table will simply fill the browser window anyway.

Setting a minimum table size relies on the actual content - if the longest word in a cell is supercalifragilisticexpealidocious, the cell can be no smaller. Despite the fact that we may set the table width to 10 pixels, the width of the content will not be overridden. The widest cell widths will dictate the widths of other cells in columns below or above. <td width="100"> or <td width=50%> are examples.

Table height is normally not so important as cells expand automatically to accomodate content, however Netscape 4.0+ allows you to specify the height of the table in pixels and Internet Explorer 4.0+ allows both pixels and percentages

Table Borders

The example table above has a 1-pixel 3D border. This is a table default. Borders can be set to any thickness by including a simple tag - <table border="10">

a ten pixel border


This is not particularly useful or attractive, and most designers would avoid using table borders unless displaying lengthy amounts of tabulated data. Because tables are normally used to manipulate text and image placement, borders are generally not required or desired. In this case, <table border="0"> solves the problem - as in this page, the borders are non-existent. A good developer's trick is to set border="1" while building a table. Once it is complete and any problems ironed out, reset the border to "0".

The bordercolor attribute is currently supported by version 4+ browsers and is an HTML 4.0 standard. <table bordercolor="#cc3300"> Internet Explorer (version 3+) colors the entire table border, including cell borders, flattening the border; Netscape only colors the outer border of the table, preserving the 3D effect. It is possible to color individual cell borders <td bordercolor="#cc3300"> but this has limited support across browsers.

a ten pixel colored border

 

Cellpadding and Cellspacing

It is easy to confuse these two table attributes, both of which offer control of the space around cells. Both operate across the entire table. Cellpadding refers to the amount of space between the wall of the cell and its contents - you can 'pad out' a cell by adding more space:

<table cellpadding="10" border="10">

a ten pixel border with ten pixel cellpadding

This is exactly the same table as above, with the cellpadding attribute set to 10 pixels. Note how the border is 'pushed away' by ten pixels around each side of the text. The text has been padded by ten pixels. Unless cellpadding is set to zero, a default value of 2 applies. (this may vary with different browsers)

<table cellspacing="10" border="10">

a ten pixel border with ten pixel cellspacing

This is exactly the same table as above, with no cellpadding and cellspacing set to 10 pixels. The text remains close to the data cell border, but the cell itself is buffered from the border - or other cells by ten pixels. Unless cellspacing is set to zero, a default value of 2 applies. (this may vary with different browsers)

<table cellspacing="10" border="0">

a zero border with ten
pixel cellpadding

When border="0", the resulting effect of either cellspacing or cellpadding appears identical, only because the border is no longer visible and you cannot see where the padding is taking place - outside the cell or inside it.You may still wish to specify one or the other to seperate cells.

<table cellspacing="10" cellpadding="10" border="10">

a ten pixel border with ten pixel cellpadding and ten pixel cellspacing

Any of the 3 attributes can be used in combination for desired effect

Column and Row Spanning

Spanning refers to the ability to create a cell that stretches across a number of rows or columns. This can be another confusing set of attributes - colspan allows a cell to span the width of a set number of columns and rowspan allows a cell to span the width of a set number of rows. The best explanation is a visual example:

   
1 This cell is spanning or stretching across 4 cells in the left column. Its attribute is rowspan="4"
2
3
4

 

This cell spans or stretches across 2 columns. Its attribute is colspan="2"
1 2
   
   
   

 

Aligning Table Content

An entire table can be aligned left (default), right or center to the page by enclosing everything surrounded by <table> </table> with either a <p align="center"> or <div align="center"> or <center> tag. Whichever you chose to use, remember to end the alignment with a closing tag. Alignment is important in creating a consistent and neat appearance.

Content within cells can also be aligned by adding a series of attributes. Both <tr> and <td> support the align and valign attributes. Align causes cell data to align itself horizontally inside a cell - left (default), right or center. Valign causes cell data to align itself vertically inside a cell - top, center (middle) or bottom. Any combination of align and valign can be specified.

align=left align=center align=right

valign=top

 

valign=middle valign=bottom

align=left
valign=top

 

align=center
valign=center
align=right
valign=bottom

Adding Background Color

Table cells can be filled with a solid hex color - either individually or as an entire table by using <table bgcolor="#color"> or <tr bgcolor="#color"> or <td bgcolor="#color">. Specifying bgcolor for a row overrides any color set for the table, and specifying bgcolor for a cell will override both.

bgcolor="#cccc00" bgcolor="#ff9900"
bgcolor="#993300" bgcolor="#cccc00"

or

<table bgcolor="#cccc00">

This is quite effective and can be used to compliment other design elements as in the CDL main page, but don't do it just because you can, only if it enhances what you have to say.

Adding a Background Image

Adding a background image to a table or table cell is similar to adding a background image to an entire page, the difference being that it is not so widely supported. A solution to this problem is to use a background image that does not have a crucial bearing on the page as a whole. A bgcolor can also be set, causing browsers who cannot read the background image to at least see the background color. The syntax would be <table background="image.gif" bgcolor="#"> or <td background="image.gif" bgcolor="#">. This attribute should be used with consideration - the hierarchy of table background verses table data cell color appears to present differently with different browsers. Test before committing to one particular method. Another problem is that Netscape Navigator will display a table background only in the cell space itself - this is only a problem if cellspacing has been specified, where the space between cells will not contain the image.

bgcolor="#cccc00" If you do not see a pattern
you will see color in this data cell bgcolor="#cccc00"

this table has a background image. if you do not see
it, your browser does not support it

Tables Within Tables

It is quite legitimate, and often necessary, to 'nest' one table inside another. Again, the CDL main page can be used as an example - a number of tables have been employed to create the desired effect. The main point to remember in doing so, however, is to end all tags appropriately and to write the HTML in a logical and readable manner - utilising comment tags to mark the start of one particular tables is advisable.

If you think of a table as a container, then you are simply placing a smaller container inside a larger one - like a tupperware set. The newly inserted table must exist in a logical place within the initial table - it must be inside an existing cell as it becomes the content of that cell.

This is an ordinary cell So is this one but it also has a table inside it!
And here is the table!

And here is the simplified HTML for this example:

<!-- a comment to show where the large table starts-->
<table>

<tr>

<td> This is an ordinary cell
</td>

<td> So is this one but it also has a table inside it!

<!-- comment where the small table starts-->
<table>
<tr>

<td>And here is the table!</td>

</tr>
</table>

</td>

</tr>

</table>

Browsers need to determine the entire table layout before displaying its contents - nested tables are a great aid to web page design but avoid lengthy tables and overuse.

 

Table Tips - General Table Information
Blank cells
Be careful with Blank table cells. Though you may wish to include no visible content in a cell, it is necessary to specify invisible content simply due to inconsistent browser behaviour. Visually empty cells should have an invisible &nbsp; or non-breaking space inserted, causing all browsers to recognise the empty cell as a structure: For example, the table below has content in certain cells, with other cells being completely empty. This is how it appears in a browser, and then how it appears with the inclusion of invisible content:

table example

Something   Something
Something    
    Something

 

Cell Height
The default cell height appears to be 12 pixels which is difficult to override. The only way to achieve a thinner cell is to insert an image or invisible gif to achieve a smaller height value. The cell contracts to the size of the image in this case.

Captions
A descriptive caption can easily be applied to a table with the <caption> element. The default attribute of caption is align="top" (above the table) or else specify align="bottom" (below the table).

<table>
<caption align="bottom">
caption text here</caption>

<tr>
<td>
text here
</td>
</tr>

</table>

And here it is
A table with a caption added

 

Table Headers
A little used table element is the <th> </th> or header tag. This simply creates a bold, centered 'heading' for a column - working exactly the same as <td> </td> otherwise. Any of the align attributes can be used.

<table>
<tr>
<th>
heading</th>
</tr>

<tr>
<td>
text here
</td>
</tr>

</table>

heading
 
A table with a caption added

Adding Images as Content
Just as text can be inserted into a table, images can easily be added using the image tag, which is covered in Tutorial 11. It is often necessary to use tables with images to achieve a nice arrangement. Here is a simple example using our custom bullet:

<table>
<tr>

<td>
<img src="bullet.gif">
</td>
<td>
This is a 2-cell ...
</td>

</tr>
</table>

This is a 2-cell table, one cell contains the bullet, the other this text

 

Preventing Text Wrapping
Normally, the browser obeys the set width of a table and wraps text to fit inside a cell. In the majority of occasions you will find it preferrable for the wrap to occur, however there may be a particular title or web address that would benefit from staying on one line. This is accomplished via the nowrap attribute, which applies to <td> and <th> :

<td nowrap>

http://www.sci.monash.edu.au/cdl/htmlcourse.

Table Rules
A recent addition to table attributes is the 'rules' declaration. This is not yet supported by all browsers but allows you to display only horizontal cell borders or vertical or none. The syntax is <table rules="rows,cols or none">.

Conclusion






Tables can create visual illusions - they do not take up as much bandwidth as graphics and are easily implemented





 

Home : Next Tutorial

This tutorial series was authored by Wendy Phillips It is copyrighted which means you may not pinch any of it