I have already found a solution to this question, but I am interested in exploring better alternatives. My goal is to utilize flexbox to create rows of items with equal spacing and dividing lines between them. The examples provided in the code snippet achieve this by using an :after
pseudo-element, albeit requiring additional CSS to hide it on the final row.
.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;
}
It seems there might be a more streamlined approach (utilizing flexbox). Does anyone have any ideas?
I attempted applying a border to each item, but this introduces space between them which is not ideal. Furthermore, I would still need to use :nth-last-child
to hide it.
.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>