space between divs - display table-cell

后端 未结 5 1080
盖世英雄少女心
盖世英雄少女心 2020-11-29 01:56

I have here two divs:

content
相关标签:
5条回答
  • 2020-11-29 02:36
    <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>
    
    0 讨论(0)
  • 2020-11-29 02:46

    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.
    0 讨论(0)
  • 2020-11-29 02:53

    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.

    0 讨论(0)
  • 2020-11-29 02:56

    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>

    0 讨论(0)
  • 2020-11-29 02:57

    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>

    0 讨论(0)
提交回复
热议问题