This should hopefully get you in the right direction.
I've done the CSS
so it mobile up. So first media query is for tablets, second is for desktops and the width: 100%
is for mobile, so obviously will display one column. You can modify this to suit what you need. Play around with it and any issues comment and I will have a look :)
FlexBox approach
flex-direction The flex-direction CSS property sets how flex items are placed in the flex container defining the main axis and the direction (normal or reversed). Have a look at examples here
flex-flow The flex-flow CSS property is a shorthand property for flex-direction and flex-wrap properties. Have a read here
justify-contentThe CSS justify-content property defines how the browser distributes space between and around content items along the main-axis of a flex container, and the inline axis of a grid container. Have a read here
.grid-container {
display: flex;
flex-direction: row;
flex-flow: row wrap;
justify-content: space-between;
width: 100%;
}
.grid-item {
display: flex;
background-color: orange;
width: calc(100% - 10px);
margin: 5px
}
@media (min-width: 720px) {
.grid-item {
width: calc(33.333% - 10px);
}
}
@media (min-width: 1020px) {
.grid-item {
width: calc(25% - 10px);
}
}
<div class='grid-container'>
<div class='grid-item'>item 1</div>
<div class='grid-item'>item 2</div>
<div class='grid-item'>item 3</div>
<div class='grid-item'>item 4</div>
<div class='grid-item'>item 5</div>
<div class='grid-item'>item 6</div>
</div>
Remember if you change the margin value to say 20px
you will then also have to change the width
value as it's doing this:
width: calc(grid-item-desired-width% - margin);
I'd really recommend reading the link above as they all have interactive demonstrations which will give you a visual example of how these flexbox properties work. I'd also recommend just playing around with flex-box and maybe try out different values to see how they act on your grid. It's the best way to learn it and get it stuck in your head. Hope you find the above helpful.
CSS Grid approach
Another way is using CSS Grid
. Wasn't sure what way you want but i'd personally go with flexbox. In case you want it below is the CSS grid approach
.grid-container {
display: grid;
grid-template-columns: auto;
padding: 10px;
grid-gap: 10px 10px;
}
.grid-item {
border: 1px solid black;
padding: 20px;
font-size: 30px;
text-align: center;
}
@media (min-width: 720px) {
.grid-container {
grid-template-columns: auto auto auto;
}
}
@media (min-width: 1020px) {
.grid-container {
grid-template-columns: auto auto auto auto;
}
}
<div class='grid-container'>
<div class='grid-item'>item 1</div>
<div class='grid-item'>item 2</div>
<div class='grid-item'>item 3</div>
<div class='grid-item'>item 4</div>
<div class='grid-item'>item 5</div>
<div class='grid-item'>item 6</div>
</div>