Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to add cellspacing="0" cellpadding="0" in <table>?

Tags:

html

xhtml

w3c

Eric meyer reset css is suggesting "tables still need 'cellspacing="0"' in the markup". Is it necessary? and what is the benefit of border-collapse: collapse; and border-spacing: 0;?

and it's only suggesting to use cellspacing, while table has another property called cellpadding?

/* tables still need 'cellspacing="0"' in the markup */
table {
        border-collapse: collapse;
        border-spacing: 0;
}
like image 678
Jitendra Vyas Avatar asked Jan 16 '10 17:01

Jitendra Vyas


People also ask

What is Cellspacing and cellpadding in table?

Cellpadding basically defines the space present between a table cell's border and the content present in it. Cellspacing basically defines the space present between individual adjacent cells.

What is the importance of Cellspacing and cellpadding attributes?

Cellpspacing is another important attribute for table tag in HTML. As cellpadding provides space inside the cells. Oppositely cellspacing provides space outside the cells. For example if in a single row you have two cells and you have given 5px cellspacing to table.

What is the need of cell padding?

Cellpadding (along with cellspacing) is a term used in the computer language HTML which stands for Hypertext Markup Language. When used in conjunction with the table element, it specifies the amount of space between the border of a table cell and its contents.

What is the default value of cellspacing attribute?

The default value for cellspacing is 2; therefore, if no cellspacing is specified, browsers will automatically place two pixels of space between cells.


3 Answers

cellpadding is not suggested because the padding css property sufficiently overrides the default properties for the cellpadding table attribute. As the other answer says, there is no compatible CSS property for cellspacing in older browsers, leaving an HTML attribute as the only way to completely "reset" this setting to 0. border-spacing: 0; takes care of this for browsers which do support it.

As for border-collapse — by default, table cells each have their own border, and collapse will merge the borders between adjacent cells together, giving the appearance of a (usually single-pixel) grid, which isn't achievable any other way when cellspacing="0". Before border-collapse was commonly supported, this is why you'd see tables with cellspacing="1" and a background color on the table, and white backgrounds on table cells.

border-collapse:collapse; is in the reset.css simply because it is the most common desired result. If you don't want this mode, you'd be fine removing this from the reset.css.

like image 199
Nicole Avatar answered Oct 02 '22 21:10

Nicole


Internet Explorer 6 and 7, and probably other early browsers, do not recognize the border-spacing attribute and as such, he suggests you still supply the values in the HTML as well.

Check out the compatibility table on SitePoint

like image 39
Doug Neiner Avatar answered Oct 02 '22 22:10

Doug Neiner


There are two types of borders in a table, the table itself can have borders (outer borders and borders between cells), and each cell can have borders around them.

Using border-collapse: collapse; means that two cells with the same border settings next to each other will only get a single set of bordering instead of a double, e.g. the right border of one cell will collapse with the left border on the next cell in the row.

There wes no CSS style for the table border between cells until CSS 2, so it has to be disabled using the HTML attribute cellspacing="0" on the table to support older browsers like IE 7*. If there is table borders between the cells, the border collapsing will naturally not work as the borders are not next to each other.

* I truly enjoyed the feeling of calling IE 7 an "older browser" ;)

like image 45
Guffa Avatar answered Oct 02 '22 22:10

Guffa