Maddog's HTML for Real People Chapter 10, Lesson 2


[INDEX] Ch 10, L 2 - Cell Spacing and Cell Padding

If you can identify cellpadding and cellspacing features of a table's cell, and you know how to specify these attributes, you can skip this lesson.

Now that we have visible borders, and we understand what cells are, we have the potential to be marvelously creative (and just a little dangerous...wink!). In the last lesson, we saw that we could adjust the border width using the 'TABLE' tag's 'BORDER' attribute. But the 'TABLE' tag has other attributes that might be useful to us. Two of these attributes are 'CELLSPACING' and 'CELLPADDING'. To better understand these, consider this image:

cell stuff

In the diagram, we see our recently revealed borders: the table border (established by the 'TABLE' tag's 'BORDER' attribute), and the cell border (usually a thin line). Between the cell border and table border, the cellspacing establishes an empty space around the cell, which separates the cell not only from the table's border, but from other cells, too. The cellpadding establishes the distance from the cell's contents (i.e., text or images) to the cell's border (the thin one). Note that cellspacing and cellpadding are properties of the cell that exist even if the border is OFF or set to zero (i.e., invisible).

The browser establishes the default values for cellspacing and cellpadding, which you can override by specifying the respective attribute inside the opening 'TABLE' tag, like so:

<TABLE CELLSPACING=number-of-pixels>
or
<TABLE CELLPADDING=number-of-pixels>

As always, follow the guidelines for using embedded attributes, like using a space before the attribute name and no space on either side of the '=' sign. Because the value is numerical (respresenting screen pixels of width), you need not use quotation marks. Naturally, you can use these attributes singly or in conjunction with other attributes. For example, if we wanted to specify a table having a border 5 pixels wide, with cellpadding of 2 pixels and cellspacing of 5 pixels, you would use the following tag to open your table:

<TABLE BORDER=5 CELLPADDING=2 CELLSPACING=5>

Keep in mind that values established by including the 'CELLPADDING' and 'CELLSPACING' attributes are recommendations to the browser only. If, for example, we elect to specify a cellpadding of zero, the browser may elect to provide some minimum cellpadding anyway, to prevent the cell's contents from actually touching the border. When I specify 'CELLPADDING=0' on my browser, text fonts are given some padding above and below the text anyway, but none on either side.

Let's observe some HTML code using these attributes:

HTML Source Code Follows:

Here's a table with BORDER=10, and default cellpadding and cellspacing:
<P>
<TABLE BORDER=10>
<CAPTION>UFOs Sighted </CAPTION>
<TR ALIGN="CENTER"><TH>Date</TH><TH>Count</TH></TR>
<TR ALIGN="CENTER"><TD>3-22-97</TD><TD>17</TD></TR>
<TR ALIGN="CENTER"><TD>4-19-97</TD><TD>39</TD></TR>
</TABLE>
<P>
Here's a table with BORDER=10, CELLPADDING=0, and CELLSPACING=0:
<P>
<TABLE BORDER=10 CELLPADDING=0 CELLSPACING=0>
<CAPTION>UFO Sighted </CAPTION>
<TR ALIGN="CENTER"><TH>Date</TH><TH>Count</TH></TR>
<TR ALIGN="CENTER"><TD>3-22-97</TD><TD>17</TD></TR>
<TR ALIGN="CENTER"><TD>4-19-97</TD><TD>39</TD></TR>
</TABLE>
<P>
Here's a table with BORDER=10, CELLPADDING=10, and CELLSPACING=0:
<P>
<TABLE BORDER=10 CELLPADDING=10 CELLSPACING=0>
<CAPTION>UFO Sighted </CAPTION>
<TR ALIGN="CENTER"><TH>Date</TH><TH>Count</TH></TR>
<TR ALIGN="CENTER"><TD>3-22-97</TD><TD>17</TD></TR>
<TR ALIGN="CENTER"><TD>4-19-97</TD><TD>39</TD></TR>
</TABLE>
<P>
Here's a table with BORDER=10, CELLPADDING=0, and CELLSPACING=10:
<P>
<TABLE BORDER=5 CELLPADDING=0 CELLSPACING=5>
<CAPTION>UFO Sighted </CAPTION>
<TR ALIGN="CENTER"><TH>Date</TH><TH>Count</TH></TR>
<TR ALIGN="CENTER"><TD>3-22-97</TD><TD>17</TD></TR>
<TR ALIGN="CENTER"><TD>4-19-97</TD><TD>39</TD></TR>
</TABLE>
<P>
Here's a table with BORDER=25, CELLPADDING=25, and CELLSPACING=25:
<P>
<TABLE BORDER=10 CELLPADDING=10 CELLSPACING=10>
<CAPTION>UFO Sighted </CAPTION>
<TR ALIGN="CENTER"><TH>Date</TH><TH>Count</TH></TR>
<TR ALIGN="CENTER"><TD>3-22-97</TD><TD>17</TD></TR>
<TR ALIGN="CENTER"><TD>4-19-97</TD><TD>39</TD></TR>
</TABLE>
<P>
Here's a minimum size table:
<P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
<CAPTION>UFO Sighted </CAPTION>
<TR ALIGN="CENTER"><TH>Date</TH><TH>Count</TH></TR>
<TR ALIGN="CENTER"><TD>3-22-97</TD><TD>17</TD></TR>
<TR ALIGN="CENTER"><TD>4-19-97</TD><TD>39</TD></TR>
</TABLE>
(End of HTML Source Code)

Demonstration Follows:

Here's a table with BORDER=10, and default cellpadding and cellspacing:

UFOs Sighted
DateCount
3-22-9717
4-19-9739

Here's a table with BORDER=10, CELLPADDING=0, and CELLSPACING=0:

UFO Sighted
DateCount
3-22-9717
4-19-9739

Here's a table with BORDER=10, CELLPADDING=10, and CELLSPACING=0:

UFO Sighted
DateCount
3-22-9717
4-19-9739

Here's a table with BORDER=10, CELLPADDING=0, and CELLSPACING=10:

UFO Sighted
DateCount
3-22-9717
4-19-9739

Here's a table with BORDER=25, CELLPADDING=25, and CELLSPACING=25:

UFO Sighted
DateCount
3-22-9717
4-19-9739

Here's a minimum size table:

UFO Sighted
DateCount
3-22-9717
4-19-9739

(Demonstration Ends)

The demo provides several examples:

Here's an inconsistency I noticed in how the two most popular browsers handle cellspacing and cellpadding. In Netscape, cellspacing apparently applies to the table itself, as well as cells. Also (with Netscape), cellspacing and cellpadding are applied to the caption, even though it has no visible border. If you use Netscape, observe the table caption in the above examples, you'll notice that the caption got farther away from the table as cellspacing or cellpadding was increased. However, with Microsoft's Internet Explorer, cellspacing and cellpadding do not appear to apply outside the table's border, and the caption appears the same distance above the table in all the examples.
-PREVIOUS- -CHAPTER- -INDEX- -NEXT-

Overseer: Monty Northrup ... maddog@io.com ... leave e-mail ...
...to maddog 'n' miracles homepage...