【问题标题】:How to make borders collapse (on a div)?如何使边框折叠(在 div 上)?
【发布时间】:2013-07-28 18:15:19
【问题描述】:

假设我有如下标记: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>

然后是 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;
}

我有带有display: table; border-collapse: collapse; 的外部div 和带有display: table-cell 的单元格,为什么它们仍然没有崩溃?我在这里想念什么?

顺便说一句,一列中可能有可变数量的单元格,所以我不能只在一侧有边框。

【问题讨论】:

  • 嗯,你为什么不用&lt;table&gt; 和朋友来吃饭?
  • @muistooshort,因为可能有可变数量的单元格,我不希望在特定设计中有空单元格

标签: css


【解决方案1】:

这是demo

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

display: table-cell;

不是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;
}

【讨论】:

  • 嗯,在实际代码中,我确实拼写正确......嗯......我想它一定是实际代码中的其他东西不起作用......我会试着找到找出可能导致这种情况的原因
  • 我纠正了我的错误,这是真的
  • @Hushme 是的,很多人都知道,但有时会因为小问题挂断,不是吗?反正继续,我没问题。
  • 投票系统不会影响我,但是当有人帮助我时我可能会很高兴。
  • 你不是唯一一个有 css 能力的人检查我的个人资料我有 7 年的前端开发经验
【解决方案2】:

您需要在您的专栏中使用display: table-row 而不是float: left;,显然@Hushme 将您的diaplay: table-cell 更正为display: 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;
}

demo

【讨论】:

  • 这里检查我已经使用的链接,但我忘记删除浮动:left jsfiddle.net/R8eCr/2 查看链接号
  • 是的,没关系。离得这么近。
【解决方案3】:

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

在小提琴中更新 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;
}

【讨论】:

  • 谢谢,它对我有用。边距:-1px 0 0 -1px;
【解决方案4】:

而不是使用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;

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

【讨论】:

  • 这实际上是我找到的最佳解决方案。如果您的数据是动态生成的,并且它不能保证完美,比如说网格中有 9 个项目,而是 2 个,如果您有其他解决方案,那么它会解决问题,您会丢失边框。干得好!
  • 响应式布局的好技巧,避免更改显示属性。
  • 这适用于屏幕媒体。但在印刷媒体上,box-shadow 被视为背景而不是印刷。
  • 这对我来说效果很好,但请注意前三个阴影可以通过以下方式完成:1px 1px 0 1px #888 散布和偏移处理角落。
  • box-shadow 在 Windows 中使用高对比度主题时会被忽略(消失),因此如果您关心可访问性,请始终提供替代方案。
【解决方案5】:

为什么不使用大纲?这就是你想要的 轮廓:1px 纯红色;

【讨论】:

  • 添加一些解释,说明此答案如何帮助 OP 解决当前问题
  • 这里我展示了边界折叠的许多变体。 (使用引导网格):codepen.io/leonardo1024/pen/KgbNGr
【解决方案6】:

您也可以使用负边距:

.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;
}
<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>

【讨论】:

  • 我使用了相同的方法,只是使用了border-top/left: 0;。边距创建偏移量。
  • @toster-cx 我想这比重叠边界还要好。好想法:-)
【解决方案7】:

使用border-collapse:separate;作为

的例子
  • 容器显示为表格:
    ol[type="I"]>li{
      display: table;
      border-collapse: separate;
      border-spacing: 1rem;
    }
    
  • 【讨论】: