Tables tutorial in HTML – HTML Tables part 2

Tables tutorial in HTML – HTML Tables part 2 cover image
  1. Home
  2. HTML
  3. Tables tutorial in HTML – HTML Tables part 2

Now that you have understood the basic tags that comprise a HTML table let us look at some of their important attributes.

Attributes of <TABLE>

Attributes of <TD> Tag

Attributes of <TR> Tag

Okay, enough theory, here are a few examples.

We will make a single row table with 2 cells. We shall put a background color to add spice to an otherwise drab table.

<TABLE BORDER="3" BGCOLOR="#00CC00">
<TR>
<TD>Cell 1</TD>
<TD>Cell 2</TD>
</TR>
</TABLE>
Cell 1 Cell 2

The BGCOLOR attribute in <TABLE> defines a background green color.

What if we wanted to put a different background color in these cells? For this, we use the BGCOLOR attribute of the <TD> tag.

<TABLE BORDER="3">
<TR>
<TD BGCOLOR="#FF0000">Cell 1</TD>
<TD BGCOLOR="#0000FF">Cell 2</TD>
</TR>
</TABLE>
Cell 1 Cell 2

The first cell has a red background while the second has blue. If you use a background color for the table as a whole (specified by its BGCOLOR attribute) its value will be overridden by the BGCOLOR of <TD>.

Table cell contents need not be text. They can be images and other multimedia files. Let us work on the width and border of the table and try our hands at aligning cell contents.

<TABLE WIDTH="400" BORDER="2" BORDERCOLOR="#000000" 
BORDERCOLORDARK="#000000" BORDERCOLORLIGHT="#000000">

<TR>

<TD ALIGN="LEFT"><IMG SRC="apple1.gif" WIDTH="32" 
HEIGHT="32" ALT="Full Apple"></TD>

<TD ALIGN="CENTER"><IMG SRC="apple2.gif" WIDTH="32" 
HEIGHT="32" ALT="Bite taken"></TD>

<TD ALIGN="RIGHT"><IMG SRC="apple3.gif" WIDTH="32" 
HEIGHT="32" ALT="Nothing left"></TD>

</TR>

</TABLE>
Full Apple Bite taken Nothing left

The default value for CELLSPACING is 2, if we make this as 0, our table borders look neater. Don’t you think? (See below)

<TABLE WIDTH="400" BORDER="2" BORDERCOLOR="#000000"
CELLSPACING="0" BORDERCOLORDARK="#000000"
BORDERCOLORLIGHT="#000000">

--- The rest of the code same as above --

</TABLE>
Full Apple Bite taken Nothing left

BORDERCOLOR, BORDERCOLORDARK, and BORDERCOLORLIGHT are supported well only by Internet Explorer. We should not use them, if we want our pages to look similar in all browsers.

Setting the BORDER value to 0, we can create an invisible table. The table below has been centrally aligned by placing it inside a <DIV> tag (<DIV align=”center”>)

<DIV ALIGN="CENTER">
<TABLE WIDTH="400" BORDER="0">

<TR>

<TD ALIGN="LEFT"><IMG SRC="apple1.gif" WIDTH="32" 
HEIGHT="32" ALT="Full Apple"><BR>I'm hungry</TD>

<TD ALIGN="CENTER"><IMG SRC="apple2.gif" WIDTH="32" 
HEIGHT="32" ALT="Bite taken"><BR>Yum!</TD>

<TD ALIGN="RIGHT"><IMG SRC="apple3.gif" WIDTH="32" 
HEIGHT="32" ALT="Nothing left"><BR>Burp!</TD>

</TR>

</TABLE>
</DIV>
Full Apple
I’m hungry
Bite taken
Yum!
Nothing left
Burp!
HTML