4

I need a grid where I only specify the minimum width of the items.

Here is my attempt using flex-wrap and flex-basis: https://jsfiddle.net/2z9pgjfg/1/

HTML:

<div class="container">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

CSS:

.container {
  display: flex;
  flex-wrap: wrap;
}

.item {
  background-color: red;
  border: 1px solid black;
  flex: 1;
  flex-basis: 150px;
}

.item:after {
  content: "";
  display: block;
  padding-bottom: 75%;
}

I want any items in the last row to be the same size as all the others. Is there a way to achieve this without media queries?

Michael Benjamin
  • 307,417
  • 93
  • 525
  • 644

1 Answers1

1

Set flex grow to 0.

.container {
  display: flex;
  flex-wrap: wrap;
}

.item {
  background-color: red;
  border: 1px solid black;
  flex: 0;
  flex-basis: 150px;
}

.item:after {
  content: "";
  display: block;
  padding-bottom: 75%;
}
<div class="container">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>
Gerard
  • 14,447
  • 5
  • 28
  • 48