5

I have this piece of css to set the columns of my grid:

grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));

this works fine but I want to extend this so I can use a percentage value with a max of a certain amount of pixels like so:

grid-template-columns: repeat(auto-fill, minmax(minmax(15%, 180px), 1fr));

But this just gives me colums of a width of 100%. Which is definitely not what i want.
How to I accomplish this?

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
FutureCake
  • 2,614
  • 3
  • 27
  • 70

1 Answers1

7

Use min() inside minmax() like below

.container {
  display:grid;
  grid-template-columns: repeat(auto-fill, minmax(min(15%, 180px), 1fr));
  grid-gap:10px;
  margin:5px;
}
.container > * {
  height:50px;
  background:red;
}
<div class="container">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>

<div class="container" style="grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • What if one of the terms inside `min` is `max-content`? It doesn't seem to work, only for fixed length/percentage. – ranieri Mar 18 '21 at 13:44
  • 1
    @ranisalt yes, it won't work with max-content. `min` accept only a calculation (related: https://stackoverflow.com/a/62523544/8620333 – Temani Afif Mar 18 '21 at 19:05