views:

32083

answers:

7

How can I control cellpadding and cellspacing in a CSS stylesheet to obtain the same effect as when putting the attributes on the table tag?

+2  A: 

Simply apply

padding:

and

margin:

css properties to individual components of the table, th, tr, td etc. as you see fit...

adam
ps. This is the recommended way of doing things, cellpadding and cellspacing are deprecated
adam
Use border-spacing, not margins. Margins don't work. Cellspacing and cellpadding are presentational and should be deprecated, but aren't: http://www.w3.org/TR/html4/index/attributes.html
David Dorward
That's true. Also border-collapse:collapse is often a useful one. Also, I meant cellspacing and cellpadding are deprecated in XHTML 1.1.
adam
A: 

Let's say you want 5 cellpadding and 10 cellspacing, do this:

HTML

<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>Cell</td>
    </tr>
</table>

CSS

td
{
    margin:10px;
    padding:5px
}
Andrew G. Johnson
Interesting what in this solution we have to set сellpadding and cellspacing attributes to zero anyway. :)
Alexander Prokofyev
Don't hate the player, hate the game
Andrew G. Johnson
No. Margins around table data cells are not how you set cellspacing. See other answers.
David Dorward
+7  A: 

Also, if you want cellspacing="0", don't forget to add border-collapse: collapse in your table's stylesheet.

mat
+22  A: 

Setting margins on table cells doesn't really have any effect as far as I know. The true CSS equivalent for cellspacing is border-spacing - but it doesn't work in IE. You can use border-collapse: collapse to reliably set cell spacing to 0 as mentioned, but for any other value I think the only cross browser way is to keep using the cellspacing attribute.

Will Prescott
In today's age that reality is suckage to the Nth degree.
John K
This is almost correct, but `border-collapse` only works in IE 5-7 if the table doesn't already have a `cellspacing` attribute defined. I've written a comprehensive answer that merges all the correct parts of the other answers on this page in case that's helpful.
Eric Nguyen
+7  A: 

table {
border-collapse: collapse; // 'cellspacing' equivalent
}
table td,
table th {
padding: 0; // 'cellpadding' equivalent
}

Pup
+2  A: 

TBH. For all the fannying around with CSS you might as well just use cellpadding="0" cellspacing="0" since they are not deprecated...

Anyone else suggesting margins on 's is a DONKEY and obviously has not tried this.

corrector
+14  A: 

None of the answers here are complete, so I'll merge into a comprehensive one.

First of all, you can control cellspacing by applying the border-spacing CSS property to your table. This will work in almost all popular browsers except for IE up through v7. For browsers which support it, this property will even allow separate horizontal and vertical spacing. If you need to support IE 5, 6, or 7, you're almost out of luck.

However, I say "almost" because these browsers still support the border-collapse property, which merges the borders of adjoining table cells. If you're trying to eliminate cellspacing (i.e. cellspacing="0") then border-collapse:collapse should have the same effect: no space between table cells. This support is buggy, though, as it does not override an existing cellspacing HTML attribute on the table element.

In short: for non-IE 5-7 browsers, border-spacing handles you. For IE, if your situation is just right (you want 0 cellspacing and your table doesn't have it defined already), you can use border-collapse:collapse.

Note: For a great overview of CSS properties that one can apply to tables and for which browsers, see this fantastic Quirksmode page.

Eric Nguyen