如何使边框崩溃(在div上)?

Jie*_*eng 71 css

假设我有以下标记:http://jsfiddle.net/R8eCr/1/

<div class="container">
    <div class="column">
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
    </div>
    <div class="column">
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
    </div>
    <div class="column">
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
    </div>
    ...
</div>
Run Code Online (Sandbox Code Playgroud)

然后是CSS

.container {
    display: table;
    border-collapse: collapse;
}
.column {
    float: left;
    overflow: hidden;
    width: 120px;
}
.cell {
    display: table-cell;
    border: 1px solid red;
    width: 120px;
    height: 20px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
Run Code Online (Sandbox Code Playgroud)

我有外部div display: table; border-collapse: collapse;和细胞,display: table-cell他们为什么还没有崩溃?我在这里错过了什么?

顺便说一下,列中可能有不同数量的单元格,因此我不仅可以在一侧有边框.

iam*_*der 82

使用简单的负边距而不是使用显示表.

在小提琴JS Fiddle中更新

.container { 
    border-style: solid;
    border-color: red;
    border-width: 1px 0 0 1px;
    display: inline-block;
}
.column { 
    float: left; overflow: hidden; 
}
.cell { 
    border: 1px solid red; width: 120px; height: 20px; 
    margin:-1px 0 0 -1px;
}
.clearfix {
    clear:both;
}
Run Code Online (Sandbox Code Playgroud)


use*_*559 71

而是border使用box-shadow:

  box-shadow: 
    2px 0 0 0 #888, 
    0 2px 0 0 #888, 
    2px 2px 0 0 #888,   /* Just to fix the corner */
    2px 0 0 0 #888 inset, 
    0 2px 0 0 #888 inset;
Run Code Online (Sandbox Code Playgroud)

演示:http://codepen.io/Hawkun/pen/rsIEp

  • 这实际上是我找到的最佳解决方案.如果您的数据是动态生成的并且它不能保证完美地说出网格中的9个项目,那么它会对问题进行排序,而不是2,如果您有其他解决方案,则会丢失边框.做得好 ! (4认同)
  • 响应式布局的好方法,避免更改显示属性. (3认同)
  • 这适用于屏幕媒体.但在打印介质上,盒阴影被视为背景而不是打印. (3认同)

Hus*_*hme 35

这是一个演示

首先你需要纠正你的语法错误

display: table-cell;
Run Code Online (Sandbox Code Playgroud)

diaplay: table-cell;

   .container {
    display: table;
    border-collapse:collapse
}
.column {
    display:table-row;
}
.cell {
    display: table-cell;
    border: 1px solid red;
    width: 120px;
    height: 20px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
Run Code Online (Sandbox Code Playgroud)


小智 11

为什么不使用outline?这就是你想要的:

outline: 1px solid red;
Run Code Online (Sandbox Code Playgroud)

在这里,我展示了边框折叠的多种变体。(使用 Bootstrap 网格):https ://codepen.io/leonardo1024/pen/KgbNGr


mpe*_*pen 7

您还可以使用负边距:

.column {
  float: left;
  overflow: hidden;
  width: 120px;
}
.cell {
  border: 1px solid red;
  width: 120px;
  height: 20px;
  box-sizing: border-box;
}
.cell:not(:first-child) {
  margin-top: -1px;
}
.column:not(:first-child) > .cell {
  margin-left: -1px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="column">
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
  </div>
  <div class="column">
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
  </div>
  <div class="column">
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
  </div>
  <div class="column">
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
  </div>
  <div class="column">
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


Bho*_*yar 5

你需要使用display: table-row而不是float: left;你的专栏,显然正如@Hushme更正你diaplay: table-celldisplay: table-cell;

 .container {
    display: table;
    border-collapse: collapse;
}
.column {
    display: table-row;
    overflow: hidden;
    width: 120px;
}
.cell {
    display: table-cell;
    border: 1px solid red;
    width: 120px;
    height: 20px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
Run Code Online (Sandbox Code Playgroud)

演示