Lectures 11 and 12: Tables

Overview:

  1. Tables Fundamentals
  2. The <table> Tag
  3. The <tr> Tag
  4. The <th> and <td> Tags
  5. Colors in (X)HTML
  6. Table Examples
  7. Spanning Columns
  8. Spanning Rows
  9. Using Graphics in Layouts
  10. Approaches to Coding a Graphical Navigation Bar
  11. Recommendations When Coding Tables

Tables Fundamentals

The <table> Tag

Attribute Usage and Effect Values Accepted Default Deprecated?
align Horizontally aligns the table within its parent element (the tag holding it; if this is <body> then the horizontal alignment is based on the entire browser window). left | center | right left Deprecated
bgcolor Background color for the table. One of the pre-defined color names or a hexadecimal value   Deprecated
border Border surrounding the table on all outer sides.

Sizing is in pixels.
Number 0 Not deprecated
cellpadding Space inside the cells; one value is used for all cells and affects all four sides within each cell.

Content in the cell is pushed in, based on the amount of cellpadding.

Sizing is in pixels.
Number Defaults to a few pixels Not deprecated
cellspacing Space between the cells; one value is used for all cells and affects all sides of each cell.

Sizing is in pixels.
Number Defaults to a few pixels Not deprecated
frame Used in conjunction with the border attribute, this determines what areas of the outer border are shown.

Old browsers lack support for this attribute.
Mouse over the value for details:

above | below | border | box | hsides | lhs | rhs | void | vsides
border Not deprecated
rules Used in conjunction with the border attribute, this determines how inner borders are shown.

Rendering and support varies (even some modern browsers lack support), so exercise caution when using this attribute and be sure to test cross-browser and cross-platform.
Mouse over the value for details:

all | cols | groups | none | rows
all Not deprecated
width Sets the width of the table; accepts pixel values as well as percentages. Number (for pixels) or percentage Table is as wide as its content requires, if width is omitted Not deprecated

Attributes not in specification (browsers still render but will not validate; I recommend against using these but you will still see them in websites):

The <tr> Tag

Attribute Usage and Effect Values Accepted Default Deprecated?
align Horizontally aligns content within the cells in that row.

This can be a very quick, efficient way to center or right-align content in a number of cells.

Table data cells (<td>) left-align by default, so they do not need that specified. Table header cells (<th>), however, center their content by default so specifying a left alignment would impact their rendering.
left | center | right | justify left Not deprecated
bgcolor Background color for the cells in that row.

Specifying a bgcolor inside a cell will override this row-level setting.
One of the pre-defined color names or a hexadecimal value   Deprecated
valign Vertically aligns the content inside each cell in that row.

Baseline text is positioned on a line common to all baselined cells in the row.
top | middle | bottom | baseline middle Not deprecated

The <th> and <td> Tags

Attribute Usage and Effect Values Accepted Default Deprecated?
align Horizontally aligns content within the cell. left | center | right | justify left (for <td>) and center (for <th>) Not deprecated
bgcolor Background color for the cell. This will override the bgcolor set for that row. One of the pre-defined color names or a hexadecimal value   Deprecated
height This sets the minimum height for the cell; if content pushes the cell taller the height will be ignored.

Pixels tend to be more reliable than percentages when setting heights.
Number (for pixels) or percentage   Deprecated
nowrap If this attribute is set, content will not wrap to a new line unless forced by a tag (such as a line break tag or a paragraph tag).

Be careful with this attribute; it can cause significant layout issues.
nowrap="nowrap"   Deprecated
valign Vertically aligns the content inside the cell.

Baseline text is positioned on a line common to all baselined cells in the row.
top | middle | bottom | baseline middle Not deprecated
width Sets the width for that cell. Number (for pixels) or percentage   Deprecated

Attribute not in specification (browsers still render but will not validate; I recommend against using this but you will still see it used in websites):

Colors in (X)HTML

Table Examples

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Tables: Example 1</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <table cellpadding="5" cellspacing="5" border="1" width="600"
    rules="rows">
    <tr>
        <th width="150">Header 1</th>
        <th width="150">Header 2</th>
        <th width="150">Header 3</th>
        <th width="150">Header 4</th>
    </tr>
    <tr bgcolor="#cccccc" align="center">
        <td>Data 1</td>
        <td>Data 2</td>
        <td>Data 3</td>
        <td>Data 4</td>
    </tr>
    <tr align="center">
        <td>Data 5</td>
        <td>Data 6</td>
        <td>Data 7</td>
        <td>Data 8</td>
    </tr>
    <tr bgcolor="#cccccc" align="center">
        <td>Data 9</td>
        <td>Data 10</td>
        <td>Data 11</td>
        <td>Data 12</td>
    </tr>
    <tr align="center">
        <td>Data 13</td>
        <td>Data 14</td>
        <td>Data 15</td>
        <td>Data 16</td>
    </tr>
    </table>
</body>
</html>

See how this example renders

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Tables: Example 2</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <table cellpadding="5" cellspacing="0" border="5" width="750"
    frame="hsides">
    <tr bgcolor="#eeeeee">
        <th width="30%" align="left">Header 1</th>
        <th width="50%" align="left">Header 2</th>
        <th width="20%" align="left">Header 3</th>
    </tr>
    <tr valign="top">
        <td height="80">Data 1</td>
        <td>Data 2</td>
        <td>Data 3</td>
    </tr>
    <tr valign="bottom">
        <td height="80">Data 4</td>
        <td>Data 5</td>
        <td>Data 6</td>
    </tr>
    <tr>
        <td height="80">Data 7</td>
        <td>Data 8</td>
        <td>Data 9</td>
    </tr>
    <tr>
        <td height="80">Data 10</td>
        <td>Data 11</td>
        <td>Data 12</td>
    </tr>
    </table>
</body>
</html>

See how this example renders

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Tables: Example 3</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <table cellpadding="5" align="center" cellspacing="0" border="5"
    width="75%">
    <tr>
        <td width="10%">&nbsp;</td>
        <th width="30%">Option 1</th>
        <th width="30%">Option 2</th>
        <th width="30%">Option 3</th>
    </tr>
    <tr align="center">
    	<th align="right">Item A</th>
        <td>$565.00</td>
        <td>$123.15</td>
        <td>$25.22</td>
    </tr>
    <tr align="center">
        <th align="right">Item B</th>
        <td>$1333.15</td>
        <td>$215.22</td>
        <td>$12.65</td>
    </tr>
    <tr align="center">
        <th align="right">Item C</th>
        <td>$1205.15</td>
        <td>$250.10</td>
        <td>$125.35</td>
    </tr>
    <tr align="center">
        <th align="right">Item D</th>
        <td>$11,100.50</td>
        <td>$2500.00</td>
        <td>$1250.35</td>
    </tr>
    </table>
</body>
</html>

See how this example renders

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Tables: Example 4</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <table cellpadding="5" align="center" cellspacing="0" border="5"
    width="100%">
    <tr>
        <th width="350">Header 1</th>
        <th width="100%">Header 2</th>
        <th width="350">Header 3</th>
    </tr>
    <tr>
        <td>Data 1</td>
        <td align="center">Data 2</td>
        <td align="right">Data 3</td>
    </tr>
    <tr>
        <td>Data 4</td>
        <td align="center">Data 5</td>
        <td align="right">Data 6</td>
    </tr>
    <tr>
        <td>Data 7</td>
        <td align="center">Data 8</td>
        <td align="right">Data 9</td>
    </tr>
    <tr>
        <td>Data 10</td>
        <td align="center">Data 11</td>
        <td align="right">Data 12</td>
    </tr>
    </table>
</body>
</html>

See how this example renders

Spanning Columns

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Colspan Example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <table cellpadding="5" cellspacing="0" border="5" width="100%">
    <tr>
        <td width="20%">An individual cell</td>
        <td width="20%">An individual cell</td>
        <td width="20%">An individual cell</td>
        <td width="20%">An individual cell</td>
        <td width="20%">An individual cell</td>
    </tr>
    <tr>
        <td colspan="2" bgcolor="#eeeeee">A cell spanning 2 columns</td>
        <td colspan="3" bgcolor="#eeeeee">A cell spanning 3 columns</td>
    </tr>
    <tr>
        <td>An individual cell</td>
        <td colspan="3" bgcolor="#eeeeee">A cell spanning 3 columns</td>
        <td>An individual cell</td>
    </tr>
    <tr>
        <td>An individual cell</td>
        <td>An individual cell</td>
        <td>An individual cell</td>
        <td>An individual cell</td>
        <td>An individual cell</td>
    </tr>
    <tr>
        <td colspan="5" bgcolor="#eeeeee">A cell spanning 5 columns</td>
    </tr>
    </table>
</body>
</html>

See how this example renders

Spanning Rows

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Rowspan Example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <table cellpadding="5" cellspacing="0" border="5" width="100%">
    <tr>
        <td width="20%">An individual cell</td>
        <td width="20%">An individual cell</td>
        <td width="20%">An individual cell</td>
        <td width="20%">An individual cell</td>
        <td width="20%">An individual cell</td>
    </tr>
    <tr>
        <td rowspan="2" bgcolor="#eeeeee">A cell spanning 2 rows</td>
        <td>An individual cell</td>
        <td rowspan="2" bgcolor="#eeeeee">A cell spanning 2 rows</td>
        <td>An individual cell</td>
        <td rowspan="2" bgcolor="#eeeeee">A cell spanning 2 rows</td>
    </tr>
    <tr>
        <td>An individual cell</td>
        <td>An individual cell</td>
    </tr>
    <tr>
        <td>An individual cell</td>
        <td rowspan="3" bgcolor="#eeeeee">A cell spanning 3 rows</td>
        <td rowspan="3" bgcolor="#eeeeee">A cell spanning 3 rows</td>
        <td rowspan="3" bgcolor="#eeeeee">A cell spanning 3 rows</td>
        <td>An individual cell</td>
    </tr>
    <tr>
        <td>An individual cell</td>
        <td>An individual cell</td>
    </tr>
    <tr>
        <td>An individual cell</td>
        <td>An individual cell</td>
    </tr>
    </table>
</body>
</html>

See how this example renders

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Colspan and Rowspan Example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <table cellpadding="5" cellspacing="0" border="5" width="100%">
    <tr>
        <td width="20%">An individual cell</td>
        <td width="20%">An individual cell</td>
        <td width="20%">An individual cell</td>
        <td width="20%">An individual cell</td>
        <td width="20%">An individual cell</td>
    </tr>
    <tr>
        <td rowspan="2" colspan="2" bgcolor="#eeeeee">
        A cell spanning 2 rows and 2 columns</td>
        <td>An individual cell</td>
        <td rowspan="2" colspan="2" bgcolor="#eeeeee">
        A cell spanning 2 rows and 2 columns</td>
    </tr>
    <tr>
        <td>An individual cell</td>
    </tr>
    <tr>
        <td>An individual cell</td>
        <td rowspan="2" colspan="3" bgcolor="#eeeeee">
        A cell spanning 2 rows and 3 columns</td>
        <td>An individual cell</td>
    </tr>
    <tr>
        <td>An individual cell</td>
        <td>An individual cell</td>
    </tr>
    <tr>
        <td colspan="5" bgcolor="#eeeeee">
        A cell spanning 5 columns</td>
    </tr>
    </table>
</body>
</html>

See how this example renders

Using Graphics in Layouts

  1. Any space around the <img /> tag in the code can create gaps when the code is rendered, due to the inline nature of the tag (inline tags accept at most one space to either side). The best solution to this is to tighten up the code by removing all white space and line breaks between the tags. For an illustration of this, see the example below.
  2. It is always recommended to set cellpadding and cellspacing to zero for tables holding images, because of the gaps those create. Their default values are greater than zero, so leaving them unspecified is not an option.
  3. Specify heights and widths for images to speed up rendering and border="0" (if the image is linked) so no blue borders appear around linked navigation 'buttons'. Eventually we will use CSS to disable the borders and can then remove the attribute, which is deprecated.
  4. When deciding on what alt text to assign, keep in mind that alt text is a replacement for the image. If you are assigning alt text to a navigation 'button', use the text on the 'button' as your alt text.
  5. Netscape 6.x (and the corresponding version of Mozilla, which is so old it is not a concern) has the unfortunate tendency to put gaps around images when any doctype (Transitional or Strict) is specified, because it shifts to 'Full Standards Mode'.

    Netscape 7.x+, current versions of Mozilla, Firefox 1.x+, and Opera do not have this problem as long as Transitional doctype is used (this places them in 'Almost Standards Mode'). If Strict doctype is used (which is the case in this file) they use 'Full Standards Mode' and the images move apart.

    Under Strict doctype you would need to have those images display as block-level for rendering to be correct; this is done for the image below so that it renders as desired (we will cover modifying how an element displays when we get into CSS).

    If Netscape 6.x is a target browser either use browser detection to detect the browser and not send it a doctype, or set those images to display as block-level.

The 'trouble spots' in the code below that create gaps in Internet Explorer are in italics:

<table width="171" cellpadding="0" cellspacing="0" border="0">
<tr>
    <td>
    <img alt="Browse" height="19" width="171" src="browse.gif" />
    </td>
</tr>
<tr>
    <td width="171">
    <table width="171" cellpadding="1" cellspacing="0" border="1">
    <tr>
        <td>Sample Text</td>
    </tr>
    </table>
    </td>
</tr>
</table>

The code renders as:

Browse
Sample Text

 

Tightening up the code, so that the <td> tags are on the same line and up against the <img /> tag solves this problem in Internet Explorer (note that this does not solve the problem for Gecko-based browsers and Opera; to fix rendering in those browsers I also instructed the image to display as block since this page uses Strict doctype).

From:

<td>
<img alt="Browse" height="19" width="171" src="browse.gif" />
</td>

To:

<td><img alt="Browse" height="19" width="171" src="browse.gif" /></td>

After Tightening Up the Code:

Browse
Sample Text

This navigation bar would contain a series of individual graphical 'buttons' and would either be vertical or horizontal. Note that combinations of the two approaches are possible.

  1. All code for images and (possibly) <br /> tags in a line, no spaces:

    Make sure every <img /> tag is touching, placed one after another in a long row of code.

    If the navigation bar is vertical, use <br /> tags between the <img /> tags, but again be sure to have no spaces at all.

    This requires less code than the second approach, but the long lines of code can be hard to update.

    One option that would make the code more readable (and easier to edit) would be to break each <img /> tag across two lines by putting a hard return somewhere inside the tag, between attributes. Just be sure to test cross-browser and cross-platform.


  2. Each image in its own table row(s) and/or cell(s):

    In this approach a table is used to hold the graphics.

    For a vertical navigation bar, this means each graphic is in its own <tr> </tr> and <td> </td>.

    For a horizontal navigation bar all the graphics are in separate <td> </td> tags but are in the same <tr> </tr>.

Recommendations When Coding Tables

  1. Indenting: Be sure to indent your code, as shown in the examples above - this will make troubleshooting any coding issues much easier.
  2. Sketching/Planning: Before you write any code, sketch on a piece of paper the layout of the table, so that you know ahead of time how many rows and cells you need.
  3. Closure: Always be sure to close your tags, especially the final </table> tag. If this is not closed, the table will not render in legacy browsers (NS 4.x). Modern browsers can run into rendering glitches as well.
  4. Accounting for cells: Once you create a cell, that cell will need to be accounted for in all the subsequent rows of the table. So if you have three sets of <td> tags in a <tr>, if you add another <tr> you will need to have those three sets of <td> tags incorporated.
  5. Multiple Tables: Multiple tables can be placed in an (X)HTML document, one after the other, going down the page, but be aware that each table will start on its own row (putting two tables side by side on the page usually requires nesting of tables). Splitting up content into multiple separate tables speeds up loading/rendering, because content will appear as soon as its table is completely read.
  6. Correct Addition and Table/Cell Width Arrangements: Make sure that the combined widths for your <th> and <td> tags add up to the same width specified in your <table> tag, if you are using pixel sizes at both levels. If you are using percentages for the <th> and <td> widths, those widths should equal 100%.

    It is fine to have a fixed-width table that has variable-width cells totaling 100%, but the opposite setup (variable-width table, fixed-width cells) is to be avoided because the fixed-width cells will rarely add up to the variable width, which changes as the window is resized.

  7. Troubleshooting: When troubleshooting tables, turn on the borders and specify background colors, so you can see what is happening with the rows and cells.
  8. Empty Cells: To 'hold open' an otherwise empty cell in a table (to prevent it from collapsing) put a non-breaking space (&nbsp;) between the <th> </th> or <td> </td> tags. If you do not, that cell may not display (it will collapse) and in tables used for layout the other cells may infringe upon the space set aside for that cell.