我的布局是这样的(我输入的是内联样式而不是类的简化版本)
.qa {
border-bottom: 1px solid #ccc;
}
<div style="display: flex; flex-wrap: wrap; flex-direction: row">
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
</div>
这基本上呈现出以下内容:
我的问题是:
如何摆脱最后一行的边框?
答案 0 :(得分:6)
我们可以创建另一个CSS类,从存在元素中删除任何样式
.no-border {
border-bottom: none;
}
然后将此类添加到html元素中,例如
<div class="qa no-border" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
.qa:last-child {
border-bottom: none;
}
.qa:nth-last-child(2) {
border-bottom: none;
}
答案 1 :(得分:5)
您可以为元素添加负底边距,然后隐藏上溢。这将隐藏不需要的边框。
.qa {
border-bottom: 1px solid #ccc;
margin-bottom:-1px;
margin-top:1px; /*to rectify the bottom margin, we can also consider padding-bottom*/
/*irrelevant styles*/
padding: 5px;
margin-left:5px;
margin-right:5px;
box-sizing: border-box;
flex:1 1 40%;
}
.wrapper {
display: flex;
flex-wrap: wrap;
flex-direction: row;
overflow:hidden;
}
<div class="wrapper">
<div class="qa">
<div>Question</div>
<div>Answer<br>Answer</div>
</div>
<div class="qa">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa">
<div>Question</div>
<div>Answer<br>Answer</div>
</div>
<div class="qa">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa">
<div>Question</div>
<div>Answer<br>Answer</div>
</div>
</div>
即使一行中的元素数不同于2:
.qa {
border-bottom: 1px solid #ccc;
margin-bottom:-1px;
margin-top:1px;
/*irrelevant styles*/
padding: 5px;
margin-left:5px;
margin-right:5px;
box-sizing: border-box;
flex:1 1 20%;
}
.wrapper {
display: flex;
flex-wrap: wrap;
flex-direction: row;
overflow:hidden;
}
<div class="wrapper">
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer<br> answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer<br> answer</div>
</div>
</div>
它还可以与响应式布局一起使用,在该布局中,小屏幕上的列数可以更改:
.qa {
border-bottom: 1px solid #ccc;
margin-bottom:-1px;
margin-top:1px;
/*irrelevant styles*/
padding: 5px;
margin-left:5px;
margin-right:5px;
box-sizing: border-box;
flex:1 1 20%;
}
.wrapper {
display: flex;
flex-wrap: wrap;
flex-direction: row;
overflow:hidden;
}
@media all and (max-width:800px) {
.qa {
flex:1 1 30%;
}
}
@media all and (max-width:400px) {
.qa {
flex:1 1 40%;
}
}
<div class="wrapper">
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer<br> answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" >
<div>Question</div>
<div>Answer<br> answer</div>
</div>
</div>
答案 2 :(得分:4)
不要认为它是border-bottom
。
将其视为border-top
,并排除前两个元素。
所以代替这个:
.qa { border-bottom: 1px solid #ccc; }
尝试一下:
.qa + .qa + .qa { border-top: 1px solid #ccc; }
.qa + .qa + .qa {
border-top: 1px solid #ccc;
}
<div style="display: flex; flex-wrap: wrap; flex-direction: row">
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
</div>
next-sibling combinator(+
)的目标是紧随另一个元素之后的元素,并且它们共享相同的父元素。
因此.qa + .qa
仅定位到.qa
元素之后的.qa
元素。
.qa + .qa + .qa
仅定位在.qa
元素之后的两个.qa
元素。
或者,您可以尝试以下操作:
.qa:nth-child(n + 3) { border-top: 1px solid #ccc; }
.qa:nth-child(n + 3) {
border-top: 1px solid #ccc;
}
<div style="display: flex; flex-wrap: wrap; flex-direction: row">
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
</div>
答案 3 :(得分:3)
根据您的布局,项目编号:
[1] [2]
[3] [4]
[5] [6]
现在,您可以:
.qa {
border-bottom: 1px solid #ccc;
}
.qa:nth-of-type(5),
.qa:nth-of-type(6) {
border-bottom: none;
}
.qa:nth-of-type(n+5) {
border-bottom: 1px solid #ccc;
}
border-top
您还可以翻转弯曲方向以使其更“合理”,但也需要固定高度(需要包裹,请参见here)
.container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
以不同方向对项目编号:
[1] [4]
[2] [5]
[3] [6]
现在您删除了每第三个项目的下边界:
.qa {
border-bottom: 1px solid #ccc;
}
.qa:nth-of-type(3n) {
border-bottom: none;
}
还有更复杂的修复程序。例如,您可以将项目分组,并在基于行的选择器上应用边框。首先,这将最接近您的实际意图:
.row .qa {
border-bottom: 1px solid #ccc;
}
.row:last-of-type .qa {
border-bottom: none;
}
答案 4 :(得分:1)
您可以使用border-top
并通过:nth-child
CSS选择器删除前两个。像这样:
.qa {
border-top: 1px solid #ccc;
}
.qa:nth-child(-n+2) {
border-top: none;
}
<div style="display: flex; flex-wrap: wrap; flex-direction: row">
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
</div>
答案 5 :(得分:0)
如果您可以在.qa的父容器中添加:after
伪元素(请确保您的父容器设置为position: relative;
或position: absolute;
)
.qa父元素的CSS
{
content: "";
position: relative;
bottom: 0;
left:0;
right:0;
height: /* set this to your (bottom padding of container + bottom margin of .qa box + border width) */
background: #fff; /* match this with your parent element background colour*/
}
答案 6 :(得分:0)
当您知道要立即覆盖一条规则时,我不喜欢添加一条规则,因此这是aridlehoover答案的稍有不同的版本,仅包含一条CSS规则。
.qa:not(:nth-child(-n+2)) {
border-top: 1px solid #ccc;
}
<div style="display: flex; flex-wrap: wrap; flex-direction: row">
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
<div class="qa" style="width:50%;">
<div>Question</div>
<div>Answer</div>
</div>
</div>