Lesson-5 (Tables in HTML)

You may want to consider using HTML tables in your website. In addition to creating HTML tables to present data in rows and columns, you can also create HTML tables to organize information on your web page. 

The process of creating an HTML table is similar to the process that you used to create your web page and any elements that you may have already included in your page, such as links or frames. Coding HTML tables into your web page is fairly easy since you need only understand a few basic table codes.

5.1 Creating a basic table

The basic structure of an HTML table consists of the following tags:

Constructing an HTML table consists of describing the table between the beginning table tag, <TABLE>, and the ending table table tag, </TABLE>. Between these tags, you then construct each row and each cell in the row. To do this, you would first start the row with the beginning row tag, <TR>, and then build the row by creating each cell with the beginning cell tag, <TD>, adding the data for that cell, and then closing the cell with the ending cell tag, </TD>. When you finish all of the cells for a row, you would then close the row with the ending row tag, </TR>.Then, for each new row, you would repeat the process of beginning the row, building each cell in the row, and closing the row.

The following table is an example of a basic table with three rows and two columns of data. 

Data 1

Data 2

Data 3

Data 4

Data 5

Data 6

The codes that generated this table look like this:

<TABLE>
   <TR>
      <TD>
Data 1</TD>
      <TD>
Data 2</TD>
   </TR>
   <TR>
      <TD>
Data 3</TD>
      <TD>
Data 4</TD>
   </TR>
   <TR>
      <TD>
Data 5</TD>
      <TD>
Data 6</TD>
   </TR>
</TABLE>

This table contains no border, title, or headings.  If you wish to add any of these elements to your table, you need to include additional HTML codes.  The codes for these elements are explained in the next section.

5.2 Adding a border, title, and headings

In addition to the basic table tags, several options are available for adding additional elements to your table. For example, if you add a border, title, and column headings to the table in the previous section, the table would then resemble the following:


TABLE TITLE

Column A

Column B

Data 1

Data 2

Data 3

Data 4

Data 5

Data 6

The following codes generated the border, TABLE TITLE, and Column A and Column B headings for this table:

<TABLE  BORDER="5">
   <TR>
      <TH COLSPAN="2">
         <H3><BR>
TABLE TITLE</H3>
      </TH>
   </TR>
      <TH>
Column A</TH>
      <TH>
Column B</TH>

Note:  If you wish to view the codes that generated the Data 1 through Data 6 cells, refer to the previous section. 

Notice that the beginning table tag, <TABLE>, now includes the border tag, BORDER="5", which places a border around the table and frames each cell. The number that you ascribe to the border tag, BORDER=n, sets the width of the table border. Depending on how you design your table, you can then determine the border size that best suits your table and the overall design of your web page.

To add a title to your table, you would place the title and the attributes of that title between the row commands, <TR> and </TR>. The heading codes, <TH> and </TH>, define a heading cell and, by default, these codes center the heading and set it in bold type.  However, if you want the title to span across the columns below it, you need to include the COLSPAN=n code. Since this table has two columns, the COLSPAN="2" code was necessary. To add emphasis to the header, you can use the header commands to make the text larger. In this table, notice that the <H3> and </H3> commands made the title larger. Finally, the <BR> tag created a space above the title.

The individual column headings are also described by the heading codes, <TH> and </TH>. Since these codes, by default, center the heading and set it in bold type, no additional commands or attributes were included in the heading commands.

5.3 Polishing your table

To give your table a more polished look, you can include commands that will adjust the size of your table, add space in the cell, add space between rows, and align the data in a cell. Working with these commands is basically a process of trial and error to create the most appealing presentation of your information. The type of table that you create and the overall design of your web site will help you determine what works best for your table. 

Some of the commands that enable you to customize your table include:

In addition to the codes that were explained in the previous sections, the table below now includes some of these commands.


TABLE TITLE

Column A

Column B

Data 1

Data 2

The following codes, along with codes previously discussed, created this table:

<TABLE BORDER="5"    WIDTH="50%"   CELLPADDING="4" CELLSPACING="3">
   <TR>
      <TH COLSPAN="2"><BR><H3>
TABLE TITLE</H3>
      </TH>
   </TR>
   <TR>
      <TH>
Column A</TH>
      <TH>
Column B</TH>
   </TR>
   <TR ALIGN="CENTER">
      <TD>
Data 1</TD>
      <TD>
Data 2</TD>
   </TR>
</TABLE>

Notice that the TABLE command now includes the WIDTH="50%" command. This command extends the table across one half of the width of the text. Also, the CELLPADDING="4" command increases the vertical dimension of the cells, and the CELLSPACING="3" command increases the border around the cells. Finally, the ALIGN="CENTER" command places Data 1 and Data 2 in the center of the cell.

5.4 ROWSPAN AND COLSPAN attributes of <TD>

There are two ways to look at this. We can consider the table to consist of two columns with the second column divided into three rows OR, the other way is to understand that the first column spans three rows. HTML uses the second line of thought for laying out the table.

<TABLE BORDER="1" CELLPADDING="0" CELLSPACING="0"

WIDTH="300" HEIGHT="200">

 

<TR>

<TD ALIGN="CENTER" ROWSPAN="3" WIDTH="100">Column 1</TD>

<TD ALIGN="CENTER" WIDTH="200">Row 1</TD>

</TR>

 

<TR>

<TD ALIGN="CENTER">Row 2</TD>

</TR>

 

<TR>

<TD ALIGN="CENTER">Row 3</TD>

</TR>

 

</TABLE>

Column 1

Row 1

Row 2

Row 3

As the code shows, The table consists of three rows. The first contains two cells while the other two have only one cell.
The first cell of the first row has ROWSPAN attribute set to 3. The ROWSPAN attribute forces this cell to span 3 rows. Get it?

 

Now lets look at COLSPAN

<TABLE BORDER="1" CELLPADDING="0" CELLSPACING="0"

WIDTH="300" HEIGHT="70">

 

<TR>

<TD ALIGN="CENTER" COLSPAN="3"><B>Satisfying hunger?

</B></TD>

</TR>

 

<TR>

<TD ALIGN="CENTER" WIDTH="100" BGCOLOR="#DDFFDD"><IMG

SRC="apple1.gif" WIDTH="32" HEIGHT="32" ALT="..."></TD>

<TD ALIGN="CENTER" WIDTH="100" BGCOLOR="#AAFFAA"><IMG

SRC="apple2.gif" WIDTH="32" HEIGHT="32" ALT="..."></TD>

<TD ALIGN="CENTER" WIDTH="100" BGCOLOR="#00C000"><IMG

SRC="apple3.gif" WIDTH="32" HEIGHT="32" ALT="..."></TD>

</TR>

 

</TABLE>

Satisfying hunger?

Full apple

Bite taken

Nothing left

The code shows that the table consists of two rows. The first row contains only one cell which carries COLSPAN="3" attribute. Thus, this cell will be stretched over three columns. The next row has three cells that make three columns. As an added bonus, cells of the second row have a different background color.

Actually the second row HAS to consist of three cells for the first row to span them correctly. Browsers generally will not display tables properly if there is difference between the number of columns specified in COLSPAN and the actual number of columns made via <TD> tag.