這是一個我已經有解決方案的問題,但我想知道是否有更好的方法。我想要的是使用flexbox的行之間有空間的行和行之間的行。以下代碼片段中的第一個和第二個示例通過使用:after
來實現此目的,但我必須使用相當數量的CSS將其隱藏在最後一行。行間行間的Flexbox空間
.item:nth-child(4n + 1):nth-last-child(4):after,
.item:nth-child(4n + 1):nth-last-child(4):after,
.item:nth-child(4n + 1):nth-last-child(2):after,
.item:nth-child(4n + 1):nth-last-child(1):after{
display: none;
}
當然有更好的方法(使用flexbox)有誰知道嗎?
我試着用在每個項目上邊框,但顯然這有他們之間的空間,不工作,那麼我仍然需要使用:nth-last-child
隱藏。
.outer{
background-color: #ccc;
margin-bottom: 15px;
}
.wrap{
overflow: hidden;
flex: 4;
justify-content: space-between;
flex-flow: row wrap;
width: 80%;
margin: 0 auto;
background-color: #cdcdcd;
display: flex;
}
.item{
width: 23%;
padding-bottom: 30px;
margin-bottom: 30px;
position: relative;
}
.item2{
width: 23%;
padding-bottom: 30px;
margin-bottom: 30px;
position: relative;
border-bottom: 1px solid red;
}
.item:nth-child(4n + 1):after{
content: "";
position: absolute;
left: 0;
width: 500%;
bottom: 0;
border-bottom: 1px solid red;
}
@media (max-width: 768px) {
.item:nth-child(2n + 1):nth-last-child(2):after,
.item:nth-child(2n + 1):nth-last-child(1):after{
display: none;
}
.item{
width: 43%;
}
}
@media (min-width: 768px) {
.item:nth-child(4n + 1):nth-last-child(4):after,
.item:nth-child(4n + 1):nth-last-child(4):after,
.item:nth-child(4n + 1):nth-last-child(2):after,
.item:nth-child(4n + 1):nth-last-child(1):after{
display: none;
}
}
<div class="outer">
<h3>Working example - 8 Items</h3>
<div class="wrap">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
<div class="item">Item 6</div>
<div class="item">Item 7</div>
<div class="item">Item 8</div>
</div>
</div>
<div class="outer">
<h3>Working example - 5 Items</h3>
<div class="wrap">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
</div>
<div class="outer">
<h3>Using normal border</h3>
<div class="wrap">
<div class="item2">Item 1</div>
<div class="item2">Item 2</div>
<div class="item2">Item 3</div>
<div class="item2">Item 4</div>
<div class="item2">Item 5</div>
</div>
</div>
固定四項爲每一行或會改變嗎? –
它將爲更小的設備,這增加了更多的複雜性改變,所以我並沒有包括在本例中:) –
如果他們去是一個小裝置3行,所以我們有3個紅林家?如果這是我認爲你的代碼不夠用。 –