如何使边框折叠(在 div 上)?

新手上路,请多包涵

假设我有这样的标记: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;
}

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

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

原文由 Jiew Meng 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 720
2 个回答

这是一个 演示

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

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 发布,翻译遵循 CC BY-SA 3.0 许可协议

使用简单的负边距而不是使用 display: table

在 fiddle 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;
}

原文由 iamjustcoder 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题