I have here two divs:
<div style="display:table-cell" id="div1"> content </div> <div style="display:table-cell" id="div2"> content </div>
Is there a way to make space between these two divs (that have display:table-cell
)?
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.
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. This removes all the space between the cells of our table (see Figure 9).
HTML Table - Cell Padding. Cell padding is the space between the cell edges and the cell content. By default the padding is set to 0.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With