Table


Basic, Attributes, Cell Spanning, Tricks and Tips, Formatting Web Page

Basic

go to top

<Table>, <TR>, and <TD> tags

<Table> and </Table> define a table.
<TR> (</TR>) defines a row in a table.
<TD> (</TD>) defines a cell in a row.
The end tags for <TR> and <TD> are optional.
<Table border="5">
 <TR>
   <TD> 1 <TD> 2 <TD> 3
 <TR>
   <TD> 4 <TD> 5 <TD> 6
</Table>
gives the following table:
1 2 3
4 5 6

The content of a cell can be any html objects.

<Table align="center" border="0">
 <TR>
   <TD> <image src="NorthAm.jpg" height=160 width=200>
   <TD> <image src="peace.jpg"> <TD> 3
 <TR>
   <TD> 4 <TD> 5 <TD> <image src="dandlion.gif">
</Table>
gives the following table:
3
4 5

<Table align="center" border="5">
 <TR>
   <TD>
    <table border="0">
      <TR> <TD> A <TD> B <TD> C
      <TR> <TD> D <TD> E <TD> F
    </table>
   <TD> 2 <TD> Dandelion
 <TR>
   <TD> 4 <TD> 5 <TD> <image src="dandlion.gif">
</Table>
gives the following table:
A B C
D E F
2 Dandelion
4 5

<TH> tags is similar to <TD> but will render the text as a headline.

<Table align="center" border="5">
 <TR>
   <TD>
    <table border="0">
      <TR> <TH> A <TD> B <TD> C
      <TR> <TD> D <TD> E <TD> F
    </table
   <TH> 2 <TH> Dandelion
 <TR>
   <TH> 4 <TD> 5 <TD> <image src="dandlion.gif">
</Table>
gives the following table:
A B C
D E F
2 Dandelion
4 5

<Caption> tag

<Table border="5" width="80%" align="center">
 <caption align="bottom">
   The caption text is shown at the bottom
 </caption>
 <TR align="center">
   <TD> 1 <TD> 2 <TD> 3
 <TR>
   <TD> 4 <TD> 5 <TD> 6
</Table>
gives the following table:
The caption text is shown at the bottom
1 2 3
4 5 6

Attributes

go to top

Attributes of <Table> tag

Common Attributes of <Table> tag
Attribute name value
align left, center, right
bgcolor value for color
bordercolor
border number of pixels
cellspacing
cellpadding
width number of pixels or % of the window size
height
rules all, cols, rows
frame border, above, below, RHS, LHS, void

Attributes of <TR>, <TH> and <TD> tags

Attributes of <TR> tag
Attribute name value
height number of pixels or % of the table height
align left, center, right
valign top, middle, bottom
bgcolor value of color

Attributes of <TH> and <TD> tag
Attribute name value
height number of pixels
width number of pixels or % of the table width
align left, center, right
valign top, middle, bottom
colspan number of columns this cell spans (down)
rowspan number of rows this cell spans (to the right)

Cell Spanning

go to top


Examples of using colspan and rowspan attributes

<table ......>
  <caption> Cell 1 spans 2 columns </caption>
  <tr> <th colspan="2"> 1 <th> 2
  <tr> <th> 3 <th> 4 <th> 5
  <tr> <th> 6 <th> 7 <th> 8
</table>
Cell 1 spans 2 columns
1 2
3 4 5
6 7 8


<table ......>
  <caption> Cell 2 spans 3 rows </caption>
  <tr> <th> 1 <th rowspan="3"> 2 <th> 3
  <tr> <th> 4 <th> 5
  <tr> <th> 6 <th> 7
</table>
Cell 2 spans 3 rows
1 2 3
4 5
6 7


<table ......>
  <caption> Cell 2 spans 3 rows, Cell 5 spans 2 rows </caption>
  <tr> <th> 1 <th rowspan="3"> 2 <th> 3
  <tr> <th> 4 <th rowspan="2"> 5
  <tr> <th> 6
</table>
Cell 2 spans 3 rows, Cell 5 spans 2 rows
1 2 3
4 5
6


<table ......>
  <caption> Cell 5 spans 2 rows and 2 columns </caption>
  <tr> <th> 1 <th > 2 <th> 3
  <tr> <th> 4 <th colspan="2" rowspan="2"> 5
  <tr> <th> 6
</table>
Cell 5 spans 2 rows and 2 columns
1 2 3
4 5
6

Tricks and Tips

go to top


Suppose you want to create a table with cells spanning in the following way:

1 2 3
4
5 6 7

We first extend the rules of each cell as follows:

1 2 3
4
5 6 7
So we can easily see that cell 1 spans 2 rows and 2 columns, and cell 2 spans 3 columns.

Tricks

How to generate the following table? No Problem!!

1 0 2
0 4
3 0

Suppose we want to remove the central column. How to generate the following table?

  1   2
  4
  3
The above table uses some dirty tricks, but this is not the best thing we can do!!

The following code doesn't work. Think why!!

<table .....>
   <tr> <td rowspan="2"> 1 <td> 2
   <tr> <td> 3 <td rowspan="2"> 4
</table>

The code above will give:
1 2
3 4

To see why this is so, let's increase cellspacing to 20 pixels:
1 2
3 4

The following code can't work perfectly, although it is logically correct.

<table .....>
   <tr> <td rowspan="2"> 1 <td> 2
   <tr> <td rowspan="2"> 4
   <tr> <td> 3 </table>
gives:
1 2
4
3
The following is same as the above table except cellspacing="20".
1 2
4
3

Solution (but a bit artificial)

<table .....>
   <tr height="33%"> <td rowspan="2"> 1 <td> 2
   <tr height="33%"> <td rowspan="2"> 4
   <tr height="34%"> <td> 3 </table>
gives:
1 2
4
3

Using Table to Format Web Page

go to top


Chung-Chih Li

My Statements

Ph.D., Syracuse University

Resume: (ps) (pdf)

Visiting Assistant Professor,
Computer Science Department, Colgate University
Office: 317C, McGregory Hall
Office Hours: MW 9:30 -- 11:00 AM
Tel: (315) 228-7249
E-mail: chungli@cs.colgate.edu
Home  Address: 
48 Payne Street
Hamilton, NY 13346
USA
E-mail: dli@twcny.rr.com

IF we show the borders and rules of the tables in the document above, we will see:

Chung-Chih Li

My Statements

Ph.D., Syracuse University

Resume: (ps) (pdf)

Visiting Assistant Professor,
Computer Science Department, Colgate University
Office: 317C, McGregory Hall
Office Hours: MW 9:30 -- 11:00 AM
Tel: (315) 228-7249
E-mail: chungli@cs.colgate.edu
Home  Address: 
48 Payne Street
Hamilton, NY 13346
USA
E-mail: dli@twcny.rr.com