防止响应列出现双边框

Prevent double border from responsive columns

假设您正在使用 Bootstrap 4 并打印一些单元格。细胞数量不可预测。

.cell {
  border: 1px solid black;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">

<div class="row no-gutters">
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
</div>

当您 运行 编码时,您会看到 "double" 边框。如果我使用 nth-child 选择器,我必须计算每个断点,因为在特定断点上它每行显示不同的单元格。你是怎么解决的?

您可以对元素使用 border-right/border-bottom,对容器使用 border-top/border-left

.cell {
  border-right: 1px solid black;
  border-bottom: 1px solid black;
}
.row {
  border-top: 1px solid black;
  border-left: 1px solid black;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<div class="row no-gutters">
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
</div>

如果单元格较少,您可以使用伪元素尝试此 hack

.cell {
  border-right: 1px solid black;
  border-bottom: 1px solid black;
  position:relative;
  z-index:1;
}
.row {
  border-left: 1px solid black;
  margin:10px;
  overflow:hidden; /*hide the overflow*/
  padding-top:1px; /*for the top border*/
}


/*top border for all*/
.cell:first-child:before {
  content:"";
  position:absolute;
  bottom:100%;
  left:0;
  height:1px;
  width:100vw;
  background:#000;
}
/*hide the unwated top border in case of few cells*/
.cell:last-child:before {
  content:"";
  position:absolute;
  bottom:100%;
  left:calc(100% + 1px);
  height:1px;
  width:100vw;
  background:#fff;
}

/*avoid issue with stacking context and 
be sure the cells are on the top of the pseudo element*/
.cell:first-child,
.cell:last-child {
  z-index:auto;
}

/*for one cell*/
.cell:first-child:last-child {
  border-top:1px solid;
}

.cell:first-child:last-child:before {
  content:none;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<div class="row no-gutters">
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
</div>

<div class="row no-gutters">
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
</div>

<div class="row no-gutters">
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2">cell</div>
</div>

你可以这样处理:https://www.codeply.com/go/u5dCBDg1he

.row {
    border-style: solid;
    border-color: black;
    border-width: 1px  0   0  1px;
}

.cell {
    border-color: black;    
    border-style: solid;
    border-width: 0  1px 1px  0;
} 

这会为整个 row 添加上边框和左边框,然后为每个 cell 添加右边框和下边框。您还可以使用边框实用程序 类.

来完成此操作
<div class="row no-gutters border-left border-top">
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2 border-bottom border-right">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2 border-bottom border-right">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2 border-bottom border-right">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2 border-bottom border-right">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2 border-bottom border-right">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2 border-bottom border-right">cell</div>
  <div class="cell col-6 col-sm-4 col-md-3 col-lg-2 border-bottom border-right">cell</div>
</div>