Connect and share knowledge within a single location that is structured and easy to search. In an HTML table, the cellpadding and cellspacing can be set like this:. For controlling "cellpadding" in CSS, you can simply use padding on table cells. For "cellspacing", you can apply the border-spacing CSS property to your table.
This property will even allow separate horizontal and vertical spacing, something you couldn't do with old-school "cellspacing". This will work in almost all popular browsers except for Internet Explorer up through Internet Explorer 7, where you're almost out of luck.
I say "almost" because these browsers still support the border-collapse property, which merges the borders of adjoining 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-Internet Explorer browsers, border-spacing handles you. For Internet Explorer, 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. Note: If there is border-spacing set, it indicates border-collapse property of the table is separate.
Here you can find the old HTML way of achieving this. 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 Internet Explorer. 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.
For those who want a non-zero cellspacing value, the following CSS worked for me, but I'm only able to test it in Firefox. See the Quirksmode link posted elsewhere for compatibility details. It seems it may not work with older Internet Explorer versions. Wrap the contents of the cell with a div and you can do anything you want, but you have to wrap every cell in a column to get a uniform effect.
Yes, it's a hassle. Yes, it works with Internet Explorer. In fact, I've only tested this with Internet Explorer, because that's all we're allowed to use at work. Just use border-collapse: collapse for your table, and padding for th or td. This style is for full reset for tables - cellpadding , cellspacing and borders. However, it can create problems in older version of browsers like Internet Explorer because of the diff implementation of the box model.
Using it for anything other than container elements will keep you busy adjusting your website for future browser updates. You can easily set padding inside the table cells using the CSS padding property. It is a valid way to produce the same effect as the table's cellpadding attribute. Similarly, you can use the CSS border-spacing property to apply the spacing between adjacent table cell borders like the cellspacing attribute. However, in order to work border-spacing the value of border-collapse property muse be separate, which is default.
If margin didn't work, try to set display of tr to block and then margin will work. Now for cellspacing you have to use border-spacing. And for cellpadding you have to use border-collapse. And make sure you don't use this in your HTML5 code. Always try to use these values in a CSS 3 file. Say that we want to assign a 10px "cellpadding" and a 15px "cellspacing" to our table, in a HTML5-compliant way.
I will show here two methods giving really similar outputs. In both cases, the cellpadding is achieved by assigning paddingpx to the td s and, in both cases, the background-color assigned to them is only for the sake of a clearer demo. You can simply do something like this in your CSS, using border-spacing and padding :.
How about adding the style directly to the table itself? Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. If there are four values, they apply to the top, right, bottom, and left, respectively. In this case, instead of applying padding against a table cell, we apply it to a div element.
Also, this example uses inline style sheets to apply the padding the previous example uses embedded style sheets. As you can see, applying padding to an element can affect the size of that element. In the example above, both div elements are specified to be pixels wide. However, the padding on the first div pushes the size out, resulting in a larger div. You can't apply CSS padding to table-row-group, table-header-group, table-footer-group, table-row, table-column-group and table-column elements.
You can also use CSS border-collapse to collapse the border. Although this can have a similar effect to cellpadding in some cases, it isn't padding and shouldn't really be used as such.
CSS Cellpadding. This element has a padding of 70px. Report Error. Your message has been sent to W3Schools. W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
While using W3Schools, you agree to have read and accepted our terms of use , cookie and privacy policy. Copyright by Refsnes Data. All Rights Reserved.
0コメント