Professional Documents
Culture Documents
Colspan and Rowspan: Production
Colspan and Rowspan: Production
Table cells can span across more than one column or row. The attributes COLSPAN ("how many across") and ROWSPAN ("how many down") indicate how many columns or rows a cell should take up. For example, we might want to create header cells for each department in our table of names and phone numbers. In this table, the header cells in the first and fifth rows span across two columns to indicate the department for each group of names.
<TABLE BORDER=2 CELLPADDING=4> <TR> <TH COLSPAN=2>Production</TH> </TR> <TR> <TD>Raha Mutisya</TD> <TD>1493</TD> <TR> <TD>Shalom Buraka</TD> <TD>3829</TD> <TR> <TD>Brandy Davis</TD> <TD>0283</TD> <TR> <TH COLSPAN=2>Sales</TH> </TR> <TR> <TD>Claire Horne</TD> <TD>4827</TD> <TR> <TD>Bruce Eckel</TD> <TD>7246</TD> <TR> <TD>Danny Zeman</TD> <TD>5689</TD> </TABLE>
Shalom Buraka 3829 Brandy Davis Sales Claire Horne Bruce Eckel Danny Zeman 4827 7246 5689 0283
It often happens with multiple-column cells that a little color helps to set off the headers, giving the table a more visually organized look. Let's add some color to the headers using BGCOLOR.
<TABLE BORDER=2 CELLPADDING=4> <TR> <TH COLSPAN=2 BGCOLOR="#99CCFF">Production</TH> </TR> <TR> <TD>Raha Mutisya</TD> <TD>1493</TD> </TR> <TR> <TD>Shalom Buraka</TD> <TD>3829</TD> </TR> <TR> <TD>Brandy Davis</TD> <TD>0283</TD> </TR> <TR> <TH COLSPAN=2 BGCOLOR="#99CCFF">Sales</TH> </TR> <TR> <TD>Claire Horne</TD> <TD>4827</TD> </TR>
Shalom Buraka 3829 Brandy Davis Sales Claire Horne Bruce Eckel Danny Zeman
ROWSPAN
0283
sets how many rows a cell spans. ROWSPAN can get a little confusing because it requires you to think through how the cell affects the rows after the row it starts in. It's particularly useful in this situation to add borders to the table during the design process, even if the table won't ultimately use borders. This table code creates two header cells which span three rows each:
<TABLE BORDER=2 CELLPADDING=4> <TR> <TH ROWSPAN=3 BGCOLOR="#99CCFF">Production</TH> <TD>Raha Mutisya</TD> <TD>1493</TD> </TR> <TR> <TD>Shalom Buraka</TD> <TD>3829</TD> </TR> <TR> <TD>Brandy Davis</TD> <TD>0283</TD> </TR> <TR> <TH ROWSPAN=3 BGCOLOR="#99CCFF">Sales</TH> <TD>Claire Horne</TD> <TD>4827</TD> </TR> <TR> <TD>Bruce Eckel</TD> <TD>7246</TD> </TR> <TR> <TD>Danny Zeman</TD> <TD>5689</TD> </TR> </TABLE>
which creates
Raha Mutisya
1493
Production Shalom Buraka 3829 Brandy Davis Claire Horne Sales Bruce Eckel Danny Zeman 0283 4827 7246 5689
Note that in the two rows after each header, the first cell in the row ends up in the second column because the first column is taken up by the multi-column cell.
which gives us watermelon grapes peaches bananas To give the table borders we add the BORDER attribute to the <TABLE ...> tag:
<TABLE BORDER>
which gives us this table watermelon grapes peaches bananas sets the width of code creates a table with
BORDER <TABLE BORDER=10>
the outer border. So, for example, this an outside border width of 10:
which gives us this table watermelon grapes peaches bananas Note that BORDER sets the width of the outside border. The width of the inside borders are set with CELLSPACING.
this code
<TABLE BORDER>
produces this peaches cherries walnuts almonds peaches cherries walnuts almonds peaches walnuts cherries almonds
sets the width of the table. The width can be expressed either as an absolute value in pixels, or as a percentage of the screen width. If the width is not given, the browser
WIDTH
will use built in routines to determine what width looks best (according to the browser programmer's tastes). Some examples: A common value for WIDTH is 100%, which gives the table a nice full-page look.
<TABLE BORDER WIDTH=100%> <TR><TD>peaches</TD><TD>cherries</TD></TR> <TR><TD>walnuts</TD><TD>almonds</TD></TR> </TABLE>
peaches walnuts
<TABLE BORDER WIDTH=65%>
peaches walnuts
For tables that are less than 100%, putting the table inside <CENTER ...> often looks better than leaving the table left aligned:
<CENTER> <TABLE BORDER WIDTH=50%> </CENTER>
peaches walnuts
cherries almonds
sets the background color of the entire table. For example, this code creates a table with a background color of yellow:
BGCOLOR
this code
<TABLE BGCOLOR=YELLOW> <TR> <TD>lemons</TD> <TD>grapefruit</TD> </TR> <TR> <TD>bananas</TD> <TD>pineapple</TD> </TR> </TABLE>
You can also set the background colors of rows with <TR BGCOLOR="..."> and of individual cells with <TD BGCOLOR="...">.
Netscape and MSIE have different ways of displaying the background color. MSIE fills the entire table with the background color. Netscape only fills the cells, but not the spaces between the cells, resulting in an annoying block of individual colored cells:
MSIE
Netscape
You can kludge around the Netscape bug by enclosing the table in a one-cell outer table: this code
<TABLE BGCOLOR="#CCCC99"><TR><TD> <TABLE> <TR> <TD>lemons</TD> <TD>grapefruit</TD> </TR> <TR> <TD>bananas</TD> <TD>pineapple</TD> </TR> </TABLE> </TD></TR></TABLE>
Usage Recommendation
This effect is done better with styles. See Table Background Colors and Images sets a background image for the table. Suppose, for example, that you want to set this image as the background image for your table:
BACKGROUND
which gives us this table: blah blah groovy dude yeah yeah right on
Notive that we run into the classic problem with background images: the letters don't show up against the background. To fix this you could go to a lot of trouble using the <FONT ...> tag to set the letters to white in every table cell, but that's a lot of messy work. Instead let's do it the easy way and just use styles. First, we'll put a set of style rules in the <HEAD> section of the document:
<STYLE TYPE="text/css"> <!-.deepsea, .deepsea TD, .deepsea TH { background-image: url('deepsea.gif'); background-color:blue; color:white; font-family: sans-serif; font-weight:600; } --> </STYLE>
These rules state that for any element with its class set to deepsea, or any <TD ...> or <TH ...> within a deepsea element, there are several rules:
the background color is blue the font color is white the font style is sans-serif (which shows up better against dark backgrounds) the font weight is 600 (which makes it bolder to show up against the dark
background) Now we set the table to the deepsea class. Notice in the following code that we don't need to set the font colors for each individual cell:
<TABLE CELLPADDING=8 CELLSPACING=0 CLASS="deepsea">
<TR> <TD>blah blah</TD> <TD>yeah yeah</TD> </TR> <TR> <TD>groovy dude</TD> <TD>right on</TD> </TR> </TABLE>
which gives us this table: blah blah groovy dude yeah yeah right on
For more information about table background colors and images see Table Background Colors and Images.
and VSPACE indicate the amount of horizontal and vertical space to put between the table and surrounding text. They must be used in conjunction with ALIGN=LEFT or ALIGN=RIGHT. These attributes are only recognized by Netscape.
HSPACE
sets the amount of horizontal space between the table and surrounding text. The following examples demonstrate HSPACE when it is absent, when it is set to 10, and when it is set to 50.
HSPACE
this code
<TABLE BORDER ALIGN=LEFT>
produces this Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. fruit state watermelon Georgia apples Washington blueberries New Hampshire Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada
(no HSPACE)
<TABLE BORDER
ALIGN=LEFT HSPACE=10>
yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. fruit state watermelon Georgia apples Washington blueberries New Hampshire Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. fruit state watermelon Georgia apples Washington blueberries New Hampshire
sets the amount of vertical space between the table and surrounding text. VSPACE can be confusing because the space below the table can't break in the middle of a line. The space below the table will almost never be the actual value of VSPACE, it will usually be a little more. The following examples demonstrate HSPACE when it is absent, when it is set to 10, and when it is set to 50.
VSPACE
this code
<TABLE BORDER ALIGN=LEFT>
produces this Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. fruit state watermelon Georgia apples Washington blueberries New Hampshire Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. fruit state watermelon Georgia apples Washington blueberries New Hampshire Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. Yada yada yada. fruit state watermelon Georgia apples Washington blueberries New Hampshire
Usage Recommendation don't use it sets the height of the table. HEIGHT should almost never be used. It's best to let the browser determine the necessary height of the table based on the table's contents.
HEIGHT
Netscape and MSIE have very different ways of rendering BORDERCOLOR. Netscape maintains the 3-D appearance. MSIE renders all borders as the same color, making the border appear flat. MSIE also sets the color of the inner borders. Browser How The Table Appears
MSIE
Netscape
In the next page we'll look at how get a little more control over the border colors.
We set the light and dark borders with BORDERCOLORLIGHT and BORDERCOLORDARK. So, for example, this code sets the light borders to yellow and the dark to blue:
<TABLE BORDER=10 BORDERCOLORLIGHT=YELLOW BORDERCOLORDARK=BLUE> <TR> <TD>blah blah blah</TD> <TD>yeah yeah yeah</TD> </TR> <TR> <TD>whatever whatever</TD> <TD>right on!</TD> </TR> </TABLE>
which gives us this table: blah blah blah yeah yeah yeah whatever whatever right on!