Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Last active April 13, 2024 14:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ihorduchenko/a98b966015656d8b5e842db852ceb3d2 to your computer and use it in GitHub Desktop.
Save ihorduchenko/a98b966015656d8b5e842db852ceb3d2 to your computer and use it in GitHub Desktop.
How to adjust the amount of flex-items per row
@mixin max-width($width) {
@media screen and (max-width: $width) {
@content;
}
}
.container {
position: relative;
display: flex;
flex-flow: row wrap;
}
.item {
background-color: tomato;
box-sizing: border-box;
padding: 20px;
outline: 2px solid blue;
flex: 1;
}
@include max-width(992px) {
.item {
flex-basis: 25%;
background-color: red;
}
}
@include max-width(640px) {
.item {
flex-basis: 50%;
background-color: green;
}
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
@phena109
Copy link

This will work as long as the item are with equal flex-basis. If they are variable, then we need a different approach (which I am searching at the moment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment