space between divs - display table-cell
Solution 1:
You can use border-spacing
property:
HTML:
<div class="table">
<div class="row">
<div class="cell">Cell 1</div>
<div class="cell">Cell 2</div>
</div>
</div>
CSS:
.table {
display: table;
border-collapse: separate;
border-spacing: 10px;
}
.row { display:table-row; }
.cell {
display:table-cell;
padding:5px;
background-color: gold;
}
JSBin Demo
Any other option?
Well, not really.
Why?
-
margin
property is not applicable todisplay: table-cell
elements. -
padding
property doesn't create space between edges of the cells. -
float
property destroys the expected behavior oftable-cell
elements which are able to be as tall as their parent element.
Solution 2:
Use transparent borders if possible.
JSFiddle Demo
https://jsfiddle.net/74q3na62/
HTML
<div class="table">
<div class="row">
<div class="cell">Cell 1</div>
<div class="cell">Cell 2</div>
<div class="cell">Cell 3</div>
</div>
</div>
CSS
.table {
display: table;
border: 1px solid black;
}
.row { display:table-row; }
.cell {
display: table-cell;
background-clip: padding-box;
background-color: gold;
border-right: 10px solid transparent;
}
.cell:last-child {
border-right: 0 none;
}
Explanation
You could use the border-spacing
property, as the accepted answer suggests, but this not only generates space between the table cells but also between the table cells and the table container. This may be unwanted.
If you don't need visible borders on your table cells you should therefore use transparent
borders to generate cell margins. Transparent borders require setting background-clip: padding-box;
because otherwise the background color of the table cells is displayed on the border.
Transparent borders and background-clip are supported in IE9 upwards (and all other modern browsers). If you need IE8 compatibility or don't need actual transparent space you can simply set a white border color and leave the background-clip
out.