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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17915865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 23:42:46  来源:igfitidea点击:

How to make borders collapse (on a div)?

css

提问by Jiew Meng

Suppose I have markup like: http://jsfiddle.net/R8eCr/1/

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

Then CSS

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

I have the outer div with display: table; border-collapse: collapse;and cells with display: table-cellwhy do they still not collapse? What am I missing here?

我有外部 divdisplay: table; border-collapse: collapse;和单元格,display: table-cell为什么它们仍然没有崩溃?我在这里缺少什么?

By the way there maybe variable number of cells in a column so I can't only have borders on one side.

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

采纳答案by Hushme

here is a demo

这是一个演示

first you need to correct your syntax error its

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

display: table-cell;

not diaplay: 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;
}

回答by iamjustcoder

Use simple negative margin rather than using display table.

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

Updated in fiddle JS 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;
}

回答by user1032559

Instead using borderuse box-shadow:

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

Demo: http://codepen.io/Hawkun/pen/rsIEp

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

回答by Bhojendra Rauniyar

You need to use display: table-rowinstead of float: left;to your column and obviously as @Hushme correct your diaplay: table-cellto display: table-cell;

您需要使用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;
}

demo

演示

回答by mpen

You could also use negative margins:

您还可以使用负边距:

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

回答by Muslimbek

Why not use outline? it is what you want outline:1px solid red;

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

回答by Dmytro Yurchenko

Example of using border-collapse: separate;as

使用border-collapse的例子:separate;作为

  • container displayed as table:

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

    ol[type="I"]>li{
      display: table;
      border-collapse: separate;
      border-spacing: 1rem;
    }