Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get padding between table cells, but not around the entire table?

I want to have a table with padding between each of the cells, but not around the outer edge cells, that is, not around the table itself.

Using:

border-collapse : separate;
border-spacing  : 0.5em;

gives me padding everywhere, while using:

border-collapse : collapse;

gives no padding anywhere.

Attempting an alternate approach, I can get padding solely between the cells horizontally using a td + td selector. However I can't use a tr + tr selector because it seems tr ignores margin, padding and border rules.

And, of course, padding on a plain td selector applies to all cells, even the outer-edges of the outer cells.

This only has to work for current-generation browser - no IE 6 or 7 for me thank you very much. And I am not going to loose any sleep over IE 8, though it would be nice if that worked.

like image 308
Lawrence Dol Avatar asked Jun 15 '11 04:06

Lawrence Dol


People also ask

How do you control the space between table and cell borders?

The space between the table cells is controlled by the CELLSPACING attribute in the TABLE tag. By setting CELLSPACING to zero, you can remove all the space between the cells of your table.

How do you add padding between cells in a table?

Cell padding is the space between cell borders and the content within a cell. To set cell padding in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <table> tag, with the CSS property padding.

How do I remove border-spacing in a table?

This is a Default behavior of the table cells that there is some space between their Borders. To remove this space we can use the CSS border-collapsing Property. This Property is used to set the borders of the cell present inside the table and tells whether these cells will share a common border or not.

How do I add a space between cells in Word?

Under Settings, click Properties. Click the Table tab, and then click Options. Under Default cell spacing, select the Allow spacing between cells check box, and then enter the measurement that you want.


1 Answers

This isn't a perfect solution, as it uses padding instead of border spacing. Possibly it will work for your problem though.

HTML:

<table>
    <tr>
        <td>Data 1</td>
        <td>Data 2</td>
    </tr>
    <tr>
        <td>Data 3</td>
        <td>Data 4</td>
    </tr>
</table>

And the CSS:

table {
    border: 1px red solid;
    border-collapse: separate;
}

td {
    background-color: green;
    border: 1px black solid;
    padding: 10px;
}

td:first-child {
    padding-left: 0px;
}

td:last-child {
    padding-right: 0px;
}

tr:first-child td {
    padding-top: 0px;
}

tr:last-child td {
    padding-bottom: 0px;
}

Produces:

enter image description here

Also on jsFiddle.

like image 128
Kirk Woll Avatar answered Sep 27 '22 23:09

Kirk Woll