简体   繁体   English

使表格单元格的边框为表格宽度的100%

[英]Make border of table-cell 100% of table width

I have the following markup: 我有以下标记:

<div class="table">
  <div class="row">
    <div class="cell">1</div>
    <div class="cell">2</div>
  </div>
  <div class="row">
    <div class="cell">3</div>
    <div class="cell">4</div>
  </div>
  <div class="row">
    <div class="cell">5</div>
  </div>
</div>

With the following CSS: 使用以下CSS:

.table {
  display: table;
  border-collapse: collapse;
  width: 100%
}

.row {
  display: table-row;
  border-bottom: 1px solid black;
}

.cell {
  display: table-cell;
  width: 50%;
}

The problem is that the border of the table-row only covers the width of its containing table-cells. 问题在于表行的边界仅覆盖其包含的表单元格的宽度。 I want the last table-row to have the same border width (horizontally of course ;)) like all the other table-rows, without changing its table-cell width or having to add another table-cell. 我希望最后一个表行具有与所有其他表行相同的边框宽度(当然是水平;)),而无需更改其表单元格宽度或不必添加另一个表单元格。

Here's the fiddle: https://jsfiddle.net/tgry53ss/ 这是小提琴: https : //jsfiddle.net/tgry53ss/

Any idea on how to achieve this? 关于如何实现这一点的任何想法?

Add this to your CSS: 将此添加到您的CSS:

.row:last-child::after {
  content:' ';
}

That way it will render another cell and its bottom-border without actually creating one. 这样,它将渲染另一个单元及其底边界,而无需实际创建一个单元。

fiddle 小提琴

Since there is no rowspan / colspan in CSS. 由于CSS中没有rowspan / colspan You can use flexbox instead to achieve the results you wanted. 您可以改用flexbox获得所需的结果。 It can also make the only cell to take the entire width available just like colspan does. 像colspan一样,它也可以使唯一的单元占据整个可用宽度。

 .row { display: flex; border-bottom: 1px solid; } .cell { flex: 1; } 
 <div class="table"> <div class="row"> <div class="cell">1</div> <div class="cell">2</div> </div> <div class="row"> <div class="cell">3</div> <div class="cell">4</div> </div> <div class="row"> <div class="cell">5</div> </div> </div> 

Edit: In case you don't wish the only cell to take the entire width, do this: 编辑:如果您不希望唯一的单元格采用整个宽度,请执行以下操作:

.row {
  display: flex;
  border-bottom: 1px solid;
}
.cell {
  flex: 0 0 50%;
}

I am assuming you set cell width to 50% because you had two cells in each row except last one, so one way of dealing with this will be using inline styling to override that width: 我假设您将单元格宽度设置为50%,因为除了最后一行,每行中都有两个单元格,因此一种解决方法是使用内联样式覆盖该宽度:

<div class="cell" style="width:100%">
    5
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM