Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

space between divs - display table-cell

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)?

like image 415
System-x32z Avatar asked Aug 20 '13 22:08

System-x32z


People also ask

How do you put space between cells in HTML 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 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. This removes all the space between the cells of our table (see Figure 9).

What is cell padding in table tag?

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.


2 Answers

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.
like image 149
Hashem Qolami Avatar answered Sep 22 '22 21:09

Hashem Qolami


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.

like image 44
Max Avatar answered Sep 23 '22 21:09

Max