I want to display a variable number of images of equal height and size in a symmetrical grid. Using CSS grid and auto-fill
and minmax
I was hoping to be able to define a minimum and a maximum pixel width for my tracks so I could prevent the image from being scaled too small while fitting as many images on a single row as the users viewport allows. Given the following markup:
<div class="gallery">
<a href="">
<img src="https://via.placeholder.com/300/300" alt="">
</a>
<a href="">
<img src="https://via.placeholder.com/300/300" alt="">
</a>
<a href="">
<img src="https://via.placeholder.com/300/300" alt="">
</a>
<a href="">
<img src="https://via.placeholder.com/300/300" alt="">
</a>
<a href="">
<img src="https://via.placeholder.com/300/300" alt="">
</a>
<a href="">
<img src="https://via.placeholder.com/300/300" alt="">
</a>
<a href="">
<img src="https://via.placeholder.com/300/300" alt="">
</a>
</div>
Where each image has an intrinsic dimensions of 300 x 300px I would of expected the following CSS to have achieved that:
img {
max-width: 100%;
}
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(210px, 300px));
grid-gap: 1rem;
}
However what I'm seeing is that the tracks are not scaling between 300 and 210px, instead the track wraps as soon as the 300px cannot be accommodated.
However I can achieve the desired result using max-content
instead.
img {
max-width: 100%;
}
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(210px, max-content));
grid-gap: 1rem;
}
But max-content
will be the maximum width the image will occupy and this should be 300px so I don't understand why using a static value of 300px doesn't have the same affect.