Internet Course Website

Class 6: Tables

Class # 1 , Class # 2 ,Class # 3 , Class # 4 , Class # 5 , Class # 6

A sample of a table with an image in the background.

Some samples of what can be done with bordercolor.

A test page with several tables using no BG color, some with a BG color or an image and playing with cellpadding and cellspacing.

A TEXT file with the tags for a table (4 rows, 4 columns and 16 cells) along with many of the table tags for you to experiment with.

Please go and read this very good table tutorial as well as this page.

META Tags are at th ebottom of the page.

Using TABLES in your page layout has several key advantages:

  • an excellent format for displaying certain types of data.
  • they provide more control in the placement of text and images.
  • the page layout will display more consistently and uniformly on different browsers.

TABLES are all about control.

You can place text, images and links within each cell.

Table attributes

You can change the size and color of your table, specify a border size, and add space between the table cells. You determine all of these things by using several different attributes in your TABLE tag

 

width Available to all of the table tags. Determine the tag's width, either in px or %
height Available to all of the table tags. Determine the tag's height, either in px or %
border Available to the <table> tag. Defines its border size
bgcolor Available to all of the table tags. Defines the background color.
cellpadding Available to the <table> tag. Defines the padding width between each of it's cells.
cellspacing Available to the <table> tag. Defines the spacing width between each of it's cells.
background Available to all of the table tags. Specifies the background image of the table.

rowspan

Available to the <TD> tag only. It merges cells together from different columns (up and down). It spans rows instead of columns.
colspan Available to the <TD> tag only. It merges cells together in a row, across columns (side to side).

ALSO:

[ Alignment ] [ Nesting Tables ] [ META Tags ] [ Broken Tags ]

Boxes Inside Of Boxes

It's probably easiest to think of a TABLE as a flat, two dimensional box you can put on your page.

Inside of this box you can place other boxes, or CELLS, and inside of these boxes, you put text, images, or even more boxes still.


All tables involve using the below three tags:
<table></table> Defines a table. All tables begin and end with this tag.
<tr></tr> Defines a row. A table must have at least one row (think about it).
<td></td> Defines a cell within a row. All tables must also have a least one row.

 

CELL 1 CELL 2
CELL 3 CELL 4
<TABLE border="1" >

<TR>
<TD>CELL 1</TD>

<TD>CELL 2</TD>

</TR>

<TR>
<TD>CELL 3</TD>

<TD>CELL 4</TD>

</TR>


</TABLE>

This is a very simple table which contains four cells (2 rows and 2 columns).

 

The opening tag for a table is <TABLE> and the closing tag is </TABLE>

The TABLE CELL, or <TD> is the basic unit of a cell. You can have as many or as few cells in your table as you wish.

A horizontal row of cells is called a TABLE ROW or <TR>.

Always remember that cells go into rows, and rows go into a table.

 

This is the most simple table with one row and one cell.

Your Data

Go here for a kewl trick for images.

<TABLE border="1" >

<TR>
<TD>Your Data</TD>

</TR>

</TABLE>


Notice how the opening and closing TD tags are nested inside of the TR tags, and likewise, the TR tags are nested inside the TABLE tags.

CELL 1 CELL 2
CELL 3 CELL 4
<TABLE border="1" >

<TR>
<TD>CELL 1</TD>

<TD>CELL 2</TD>

</TR>

<TR>
<TD>CELL 3</TD>

<TD>CELL 4</TD>

</TR>


</TABLE>

In the example above, Cell 1 and cell 2 form a table row, and cells 3 and 4 form another table row.

In this example, I've outlined a table with two Table Rows. Each row contains two Cells, which gives us a four cell table.

The Width and Height

<TABLE WIDTH=33%> or <TABLE WIDTH=250>

Use the WIDTH attribute inside the table tag to specify how much of the screen you want your table to span.

You can use a percentage, like I just did, or you can use pixels for the value.

You can also use the WIDTH attribute within the <TR> and <TD> opening tags.

Width=33%
Width=50%

Width=75%

Width=90%
Width=100%

<TABLE HEIGHT=33%> or <TABLE HEIGHT=250>

Use the HEIGHT attribute inside the table tag to specify how much of the screen you want your table to span.

You can use a percentage, like I just did, or you can use pixels for the value.

You can also use the HEIGHT attribute within the <TR> and <TD> opening tags.

Borders

You can control the thickness of your table border by using the BORDER attribute. <TABLE BORDER=5>
You can even lose the border altogether by indicating a value of 0 for the BORDER attribute.

If you use HTML tables to control the layout of your Web pages, here's one of our best browser compatibility tips: always include the attribute BORDER="0". Do this even though the default for the BORDER attribute is zero.

The reason for this is that Netscape Navigator puts additional white space around each table cell unless you explicitly set BORDER="0". If you're using your TABLE to control the positioning of other elements within the page, this little bit of extra space can affect alignment.

<TABLE Border=0>
<TR>
<TD>image</TD>
<TD>image</TD>
</TR>
</TABLE>

<table BORDER="5">

<table BORDER="10">

<table BORDER="15">

Combining several attributes to create an interesting effect.

Here is a kewl trick you can do by nesting a table inside a table and then placing an image at the center. You create a multi colored frame.

<TABLE BGCOLOR="#990B00" BORDER="5" CELLPADDING="5" CELLSPACING="5">
<TR ALIGN="CENTER">
<TD>


<TABLE BGCOLOR="#000000" BORDER="0" CELLPADDING="10" CELLSPACING="10">
<TR ALIGN="CENTER">
<TD>
<IMG SRC="starry.jpg" WIDTH="150" HEIGHT="138" BORDER="0">
</TD>
</TR>
</TABLE>


</TD>
</TR>
</TABLE>

Coloring Table

<TABLE BGCOLOR="#000000"> or <TABLE BGCOLOR=black>

BGCOLOR in a table works just like the BGCOLOR attribute in a body tag.

Use the color's name or it's Hexadecimal code.

The BGCOLOR can be used in the opening <TABLE>, <TD> or <TR> tags to change the color (using hexcodes or color names).


Placing it in the <TABLE> tag colors the whole table the same color.
CELL 1 CELL 2
CELL 3 CELL 4
<TABLE border="1" bgcolor="#9999FF">

<TR>
<TD>CELL 1</TD>

<TD>CELL 2</TD>

</TR>

<TR>
<TD>CELL 3</TD>

<TD>CELL 4</TD>
</TR>


</TABLE>

 

Using it in the <TR> tag colors the whole row one color.
CELL 1 CELL 2
CELL 3 CELL 4
CELL 5 CELL 6

<TABLE border="1">

<TR bgcolor="#9999FF">
<TD>CELL 1</TD>

<TD
>CELL 2</TD>
</TR>


<TR>
<TD>CELL 3</TD>

<TD>CELL 4</TD>
</TR>

<TR bgcolor=green>
<TD>CELL 5</TD>

<TD>CELL 6</TD>
</TR>


</TABLE>

 

Placing it in the <TD> tag colors only that cell.
CELL 1 CELL 2
CELL 3 CELL 4
CELL 5 CELL 6

<TABLE border="1">

<TR>
<TD bgcolor="#9999FF">CELL 1</TD>

<TD>CELL 2</TD>
</TR>

<TR>
<TD>CELL 3</TD>

<TD bgcolor=green>CELL 4</TD>
</TR>


<TR>
<TD bgcolor="#CCFFFF">CELL 5</TD>

<TD>CELL 6</TD>
</TR>


</TABLE>

SPACING

CELLPADDING and CELLSPACING will let a little air into your table and make it easier to view.

 

CELLSPACING is the amount of space you want between your table cells.In the example above, I specified that I want five pixels of space between my cells. The cell are empty in this example.

<table border=1 cellpadding=0 CELLSPACING=0>

   
   
   

<table border=1 cellpadding=0 CELLSPACING=5>

   
   
   

<table border=1 cellpadding=0 CELLSPACING=10>

   
   
   

<table border=1 cellpadding=0 CELLSPACING=20>

   
   
   

 

CELLPADDING is how many pixels of space you want on the inside of your cell between your text and the border. The image of the blue-black button take up the entire space of the cell. The yellow space around the button is the CELLPADDING.

<TABLE border=1 CELLPADDING=0 cellspacing=0 bgcolor =yellow>
<TR>
<TD>image</TD>
<TD>image</TD>
</TR>

<TR>
<TD>image</TD>
<TD>image</TD>
</TR>
</TABLE>

<table border=1 CELLPADDING=5 cellspacing=0>

<table border=1 CELLPADDING=10 cellspacing=0>

<table border=1 CELLPADDING=20 cellspacing=0>

 

Merging Cells within Columns and Rows

Now we will play with COLSPAN (Column Span) and ROWSPAN (Row Span).

Both are placed in the opening <TD> tag (which defines a cell within a row)

COLSPAN
--spans across columns

 

<TD COLSPAN=2>

 

   
     
   
     

COLSPAN
--spans across columns

 

<TD COLROWSPAN=3>
 
     
     
 

ROWSPAN
-spans rows instead of columns

 

 

<TD ROWSPAN=2>

     
   
     
   

ROWSPAN
-spans rows instead of columns

 

<TD ROWSPAN=3>

     
   
 
   

You can't use colspan and rowspan in the same table cell tag. They must be in seperate <TD> tags.

<TD ROWSPAN=2>

<TD COLSPAN=2>

     
 
     
   

If you do place them in the same <TD> tag then they will merge together and instead of an "L" shaped box, it will create a square or rectangle.

<TD ROWSPAN=2 COLSPAN=2>

     
   
 
     

Row and Column spaning is useful for titles, layout, placing images and text, among other things. Be creative.

 

Alignment Attributes

You can position your text or image inside of the table cell itself by using the ALIGN and VALIGN attributes inside the <TD> tag.

It seems that the default for the alignment inside a cell is <TD ALIGN=left VALIGN=middle>

ALIGN
VALIGN
<TD align=left> <TD valign=top>
<TD align=center> <TD valign=middle>
<TD align=right> <TD valign=bottom>

Use only one ALIGN and one VALIGN attribute per table cell or they will not work.

<TD ALIGN=left VALIGN=top>

ALIGNMENT VALIGN=MIDDLE VALIGN=TOP VALIGN=BOTTOM
ALIGN=LEFT
---
---
---
ALIGN=CENTER
---
---
---
ALIGN=RIGHT
---
---
---
You can also place tables within tables. It's similar in nesting a list within a list.
1 2
5 6
7
9 10
11 12
4

The pale blue table is inside cell #3 and the violet table is inside cell #8.

You can nest as many tables within table as you like.

Notice that the nested table is between the opening <TD> cell tag and the closing cell </TD> tags of the table it's nested inside.

<TABLE border="1" >

<TR>
<TD>1</TD>
<TD>2</TD>
</TR>

<TR>
<TD><TABLE border="1" BGCOLOR="#66CCFF">

<TR>
<TD>5</TD>
<TD>6</TD>
</TR>

<TR>
<TD>7</TD>
<TD>
<TABLE border="1" BGCOLOR="#9999FF">

<TR>
<TD>9</TD>
<TD>10</TD>
</TR>

<TR>
<TD>11</TD>
<TD>12</TD>
</TR>

</TABLE></TD>
</TR>

</TABLE></TD>
<TD>4</TD>
</TR>

</TABLE>

Go here for a kewl trick using netsing tables.

Broken Table Tags

Broken Table tags.

The <center></center> tags must be inside the cell tags <TD></TD>.

<TABLE BORDER=3>

<TR>
<TD>Ed</TD>
<TD>Tom</TD>
<TD>Rick</TD>
</TR>

<TR><center>
<TD>Larry</TD></center>

<TD>Curly</TD>
<TD>Mo</TD>
</TR>

</TABLE>

<TABLE BORDER=3>

<TR>
<TD>Ed</TD>
<TD>Tom</TD>
<TD>Rick</TD>
</TR>

<TR>
<TD><center>Larry</center></TD>
<TD>Curly</TD>
<TD>Mo</TD>
</TR>

</TABLE>

Broken Table tags. The cell tags <TD></TD> must be inside the row tags <TR></TR>
<TABLE BORDER=3>

<TR>
<TD>Ed</TD>
<TD>Tom</TD>

</TR>
<TD>Rick</TD>

<TR>
<TD>Larry</TD>
<TD>Curly</TD>
<TD>Mo</TD>
</TR>

</TABLE>

<TABLE BORDER=3>

<TR>
<TD>Ed</TD>
<TD>Tom</TD>
<TD>Rick</TD>
</TR>

<TR>
<TD>Larry</TD>
<TD>Curly</TD>
<TD>Mo</TD>
</TR>

</TABLE>

Tables are very sensitive to having their tags broken and moved around. Extra attension must be given to them so that they work properly. Always look at the table in a browser to see if it's reacting properly and that all the information is readable.

I would suggest making a drawing of the table you wish to make. Organize the information inside each cell the way you want it to appear. It would be easier to code all the tags if you have a visual map, it's less confusing.

Meta Tags

<meta name="keywords" content="keywords here">

<meta name="description" content="description here">


<META name="description" content="We specialize in catering corporate events.">

<META name="keywords" content="catering, vegetarian, food, company, events">


Where "keywords here" is a list of keywords that you wish to be associated with your page, and "description here" the short description describing it.

The meta tag is inserted inside the <head> section. Keywords entered into the <meta> tag should each be separated by a comma.

[ HOME ] [ Tags ] [ Text ] [ Graphic ] [ General ] [ Links ]

This site is maintained by Georgia Panaritis.

For more information, send e-mail to spikeschip@javascrypt.com