Is it ok to use cellpadding="2" cellspacing="2" in <table>?

Jitendra Vyas picture Jitendra Vyas · Mar 10, 2010 · Viewed 11k times · Source

Is it ok to use cellpadding="2" cellspacing="2" in <table>? Or are these not recommended by W3C and not right according to web standards?

What are alternatives in CSS?

Update: and is it also ok to use <td align="right" valign="top">?

My question is in terms of separation of content and presentation and W3C recommendations.

Update: According to this chart in <table> only align and bgcolor are not allowed in Strict version. So is it ok to allow other properties of <table>?

alt text http://shup.com/Shup/293811/11021055643-My-Desktop.png

Answer

DisgruntledGoat picture DisgruntledGoat · Mar 10, 2010

No, the attributes are not officially deprecated but they are generally frowned upon, since you should use CSS for presentation.

For cellpadding you can easily replace it with padding in CSS:

table.classname td {
  padding: 4px;
}

For cellspacing, first decide if it's really necessary. If you don't have any borders on the table cells, or you don't want spacing between the borders of each cell then it isn't. (Personally I think cell spacing looks bad design-wise, but it may be useful in some circumstances.)

It's quite nice to do this:

table {
  border-collapse: collapse;
}

Then each table cell shares the border with its neighbour, meaning you can add, say, 1px top and bottom borders and you just get 1px separating each row.

To separate borders, however, you can use this CSS, though it probably doesn't work in IE6.

table.data td {
  border-collapse: separate;
  border-spacing: 4px;
}