I've got a grid of elements which I want equally divided in 3. So I want each row to contain 3 elements. Because I want them to have an equal gutter I used justify-content: space-between
when using flexbox. This looked good until I noticed that when the last row contains 2 elements they just get split up.
.blue{
width: 420px;
height: 230px;
background: blue;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.red{
width: 32%;
height: 100px;
background: red;
display: block;
border-sizing: border-box;
border: 1px solid white;
}
.green{
width: 420px;
height: 230px;
background: green;
display: flex;
flex-wrap: wrap;
}
.orange{
width: 32%;
height: 100px;
background: orange;
display: block;
border-sizing: border-box;
border: 1px solid white;
margin-right: 1.25%;
}
.orange:nth-child(3){
margin-right: 0;
}
<section class="blue">
<div class="red"></div>
<div class="red"></div>
<div class="red"></div>
<div class="red"></div>
<div class="red"></div>
</section>
<section class="green">
<div class="orange"></div>
<div class="orange"></div>
<div class="orange"></div>
<div class="orange"></div>
<div class="orange"></div>
</section>
My second approach seems to fix most of it, but I want it to be responsive and be broken down in rows of 2 and on mobile in rows of 1, then that will also look off. Is there anything to fix this without javascript? I've been looking to add classes based on the length of the amount of elements, but that seems unprofessional... There might be something I don't know yet which could help me out in this case.