I have a list view in which all the list items are designed to look like cards. I use justify-content: space-between to distribute the cards evenly. I need to display 5 cards in one row on a wide screen, but the cards should be placed on the left with the same gaps.
This code works great when I need to have 3 cards in one row, but not when I have 5 cards in one row. For this example I replaced <ul> and <li> with <div> with .parent and .child classes.
How can I fix it?
.parent {
display: flex;
flex-wrap: wrap;
width: 100%;
height: 100%;
gap: 20px;
box-sizing: border-box;
justify-content: space-between;
background-color: lightgreen;
}
.parent::after {
content: "";
width: 16%;
height: 0;
}
.child {
flex-basis: 16%;
background-color: blue;
color: white;
display: flex;
justify-content: center;
align-items: center;
}
@media only screen and (max-width : 1024px) {
/* for tablet */
.parent::after {
width: 46%;
}
.child {
flex-basis: 46%;
}
}
@media only screen and (max-width : 768px) {
/* for mobile */
.parent::after {
display: none;
}
.child {
flex-basis: 100%;
}
}
<div class="parent">
<div class="child"> <span>1</span> </div>
<div class="child"> <span>2</span> </div>
<div class="child"> <span>3</span> </div>
<div class="child"> <span>4</span> </div>
<div class="child"> <span>5</span> </div>
</div>
