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


81

假设我有这样的标记: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; border-collapse: collapse;和单元格display: table-cell为何仍不崩溃?我在这里想念什么?

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


嗯,你为什么不<table>和朋友一起用桌子呢?
亩太短了

@muistooshort,COS有可能的细胞数量可变的,我不想让空单元格中的特殊设计
Jiew萌

Answers:


36

这是一个演示

首先,您需要更正其语法错误

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

嗯,在实际代码中,我确实将其拼写正确了……嗯……我想这可能是实际代码中的其他内容无法正常工作……我将尽力找出可能导致这种情况的原因
杰伊Meng

我更正了我的错误,但我检查了您的回答,但我删除了float:left和width和height我已经知道了边框单元
Hushme 2013年

1
@Hushme是的,很多人都知道,但是有时候挂断电话没什么问题,不是吗?无论如何,我没问题。
Bhojendra Rauniyar

1
投票系统不会影响我,但是当有人得到我的帮助时,我可能会很高兴。
Bhojendra Rauniyar

您不是唯一拥有CSS能力的人。检查我的个人资料我有7年的前端开发人员经验
Hushme 2013年

92

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

在小提琴中更新了 JS小提琴

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

83

代替使用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;

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


5
这实际上是我找到的最佳解决方案。如果您的数据是动态生成的,并且不能保证完美,那么它可以解决问题。比如说网格中有9个项目,而网格中有2个项目(如果您在其他解决方案中拥有项目的话)会失去边界。做得好 !
2015年

3
响应式布局的好窍门,避免更改显示属性。
kosmos

3
这适用于屏幕媒体。但是在印刷媒体上,阴影框被认为是背景并且未被印刷。
Marcello Nuccio

这对我来说效果很好,但是请注意,可以使用以下方法完成前三个阴影:1px 1px 0 1px#888散布和偏移会照顾到角落。
Matt Schuette

1
box-shadow在Windows中使用高对比度主题时会被忽略(消失),因此,如果您关心可访问性,请始终提供替代方法。
tomasz86 '17

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

演示


在这里检查我已经在使用的链接,但是我忘了删除float:left jsfiddle.net/R8eCr/2看到链接号
Hushme

是的,没关系。就这么近
Bhojendra Rauniyar

4

您还可以使用负边距:

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


1
我使用了与相同的方法border-top/left: 0;。边距创建偏移量。
toster-cx

@ toster-cx我想这比重叠边界更好。好的想法:-)
mpen


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.