Vertical gap for tiles in CSS
Now let's add some vertical gap as well. To do this, we'll set margin-bottom to 10px for all descendants:
<div class="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
<div class="child">7</div>
<div class="child">8</div>
</div>
.parent {
display: flex;
flex-wrap: wrap;
width: 320px;
border: 1px solid red;
}
.child {
box-sizing: border-box;
width: 100px;
height: 100px;
margin-right: 10px;
margin-bottom: 10px;
border: 1px solid green;
}
.child:nth-child(3n) {
margin-right: 0;
}
:
As we can see, everything works fine, except that the last row is offset from the parent. Most often, this is not critical.
Make a tile of two elements per row with a distance between elements of 20px.
Make a tile of three elements in a row with a distance between elements of 20px.
Make a tile of four elements per row with a distance between elements of 20px.