I have here two divs:
content
<div style="display:table;width:100%" >
<div style="display:table-cell;width:49%" id="div1">
content
</div>
<!-- space between divs - display table-cell -->
<div style="display:table-cell;width:1%" id="separated"></div>
<!-- //space between divs - display table-cell -->
<div style="display:table-cell;width:50%" id="div2">
content
</div>
</div>
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 to display: table-cell
elements.padding
property doesn't create space between edges of the cells.float
property destroys the expected behavior of table-cell
elements which are able to be as tall as their parent element. Use transparent borders if possible.
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;
}
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.
Make a new div with whatever name (I will just use table-split) and give it a width, without adding content to it, while placing it between necessary divs that need to be separated.
You can add whatever width you find necessary. I just used 0.6% because it's what I needed for when I had to do this.
.table-split {
display: table-cell;
width: 0.6%
}
<div class="table-split"></div>
Well, the above does work, here is my solution that requires a little less markup and is more flexible.
.cells {
display: inline-block;
float: left;
padding: 1px;
}
.cells>.content {
background: #EEE;
display: table-cell;
float: left;
padding: 3px;
vertical-align: middle;
}
<div id="div1" class="cells"><div class="content">My Cell 1</div></div>
<div id="div2" class="cells"><div class="content">My Cell 2</div></div>