CpSc 130 : Introduction to Programming and Information Systems

Tables


These examples use the defaults or standard definition of table elements.

HTML Code Displays as HTML Code Displays as
    
<table>
  <tr>
    <td>Fruit</td>
    <td>Vegetable</td>
  </tr>
  <tr>
    <td>apple</td>
    <td>corn</td>
  </tr>
  <tr>
    <td>blue berry</td>
    <td>peas</td>
  </tr>
</table> 
Fruit Vegetable
apple corn
blue berry peas
<table border="1">
Fruit Vegetable
apple corn
blue berry peas
Use indentation for readability
    
<table><tr><td>Fruit</td><td>Vegetable</td></tr><tr><td>apple</td>
<td>corn</td></tr><tr><td>blue berry</td><td>peas</td></tr></table> 

As you can see, even a simple table requires lots of HTML code. The worst part is that you cannot enter data graphically, by pointing to a cell and typing. (You could if we were using a high-level tool.) Instead, you have to keep track of which column you are entering data for. Not too bad if the data is organized by rows, but very hard if the data is organized by columns.

It should be clear from the simple example above that the lines or "borders" help to understand the table format. You should use these when setting up a table, even if you later turn them off to make the table invisible.


More Table Features

In the following example, I change the first row by making these cells Table Headers <TH> </TH> and by adding a background color. The header cells are automatically made bold face and centered.

HTML Code Displays as HTML Code Displays as
    
<table border="1">
  <tr>
    <th>Fruit</th>
    <th>Vegetable</th>
  </tr>
  <tr>
    <td>apple</td>
    <td>corn</td>
  </tr>
  <tr>
    <td>blue berry</td>
    <td>peas</td>
  </tr>
</table> 
Fruit Vegetable
apple corn
blue berry peas
<table border="1" bgcolor="yellow">






same table with
<td>blue<BR>berry</td>

Fruit Vegetable
apple corn
blue berry peas

 

Fruit Vegetable
apple corn
blue
berry
peas

Looking carefully at the example, you will notice that the example itself is in an invisible table. For clarity, I added some background color and allowed more space between the edge of a cell and the enclosed text than is normal. From this example, you can gather that individual cells of a table may be modified by attributes and, even more surprising, a table can be placed inside of another table. We won't do that for a while yet.

Imagine what the code would look like without indentation! You can see what it looks like with reasonable indentation by viewing the source code for this page and searching for table name="more"

Try this: Save the source code on your H drive or desktop. Open the file with Notepad (Start: Run: notepad). Change corn to corny in the second table with yellow background.

Simple Table Attributes

There are lots of attributes or properties that we can modify for tables. We will start with color and alignment. First recall that tables are organized first by the TABLE tag, then by row (TR), then by cell data (TD). When we apply attributes to the TABLE it applies to the entire table. When applied to the row(TR), it applies to the entire row. Cells (TD) apply only to that cell. Attributes for the TABLE can be overridden by attributes for a row or cell. Attributes for a row may be overridden by attributes for a cell.

HTML Code Displays as
    
<table border="1" bgcolor="#DADADA">
  <tr>
    <th>Fruit</th>
    <th>Vegetable</th>
  </tr>
  <tr bgcolor="#FF8888">
    <td>apple</td>
    <td bgcolor="#ffff88" align="right">corn</td>
  </tr>
  <tr>
    <td bgcolor="#8888ff">blue<br>berry</td>
    <td bgcolor="#88ff88" align="center" valign="top">peas</td>
  </tr>
</table> 
Fruit Vegetable
apple corn
blue
berry
peas

Compare text alignment with the table above.

In this example


Summary of Table Tags & Attributes

Tags Comments
<TABLE> </TABLE Encloses the entire table. Any attributes added here apply to the whole table, but can be overridden in rows or individual cells.
Note that align="center" here is supposed to refer to the data within the table, not the table itself. My experiments show that not to be the case!
You should use <DIV align="center"> <TABLE> </TABLE> </DIV> to align the overall table.
<TR> </TR> Encloses all of the cells (or columns) in a particular row.
<TH> </TH> Encloses one data cell. This is assumed to be a "header" and is made bold and centered by default, although you can override the alignment as I did in this table:
<th align="left">Comments</th>
<TD> </TD> Encloses the data that goes in one cell. Left-aligned, vertically centered by default.

 

Attributes, with the exception of BORDER, may be applied to the entire table, an entire row, or individual cells.
Attribute Values Comments
BORDER="8" 1, 2, 3, 4,... (in pixels) This attribute is used only with the TABLE tag and applies to the box around the table.
BGCOLOR="WHITE" named color (WHITE) or a hexadecimal RGB specification Colors can be used to separate data cells instead of borders. BY default the color is the page color specified in the BODY tag.
ALIGN="RIGHT" LEFT, RIGHT, CENTER By default headers are centered and all other data is left-aligned. Right-alignment is useful for numbers.
VALIGN="TOP" TOP, CENTER, BOTTOM By default data is centered vertically. Many of my examples force the information to the top of the cell so you can see it immediately even with a large cell next to it.