I have a grid. Each cell will be an image with a max-width (used simple colored div in ex). Is there a way to have the grid automatically add another column when the images reach their max-width instead of just increasing the gap between them. (same for removing a column when shrink to min-width). Here is the sample code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
.pet-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
width: 100vw;
}
.pet {
aspect-ratio: 1/1;
background-color: cadetblue;
max-width: 205px;
}
</style>
</head>
<body>
<header></header>
<main>
<div class="pet-grid">
<div class="pet"></div>
<div class="pet"></div>
<div class="pet"></div>
<div class="pet"></div>
<div class="pet"></div>
<div class="pet"></div>
<div class="pet"></div>
<div class="pet"></div>
</div>
</main>
<footer></footer>
</body>
</html>
2
Answers
Use
grid-template-columns
withauto-fill
orauto-fit
https://css-tricks.com/auto-sizing-columns-css-grid-auto-fill-vs-auto-fit/
I think this might be what you are looking for. The idea here is to calculate the number of columns based on a maximum column width much like auto-fit does. but round the column number up when the available width is between the multiples of the cell’s maximum width instead of down.
To do this we need to divide the viewport width by the cell’s maximum width, and to do this, this answer leans heavily on the information provided in the article CSS Type Casting to Numeric: tan(atan2()) Scalars by Jane Ori.
Here we define a custom property "–1bcw" to mean 1 body content box width. i.e. 100vw – 16px for the body’s margin.
To calculate the target number of columns, we take the body content box width and divide it by the sum of the maximum width of a cell and the width of the gap. However we add one gap width to the body content box width because the grid will have one fewer gaps than columns. Then we round it up to the nearest integer.
The final calculation looks like this:
Then the cells will grow up to a width of 205px, and then add another column, reducing the cell width down to match.
This is a bit new and a bit hacky. It seems to work in Chrome and Firefox, but I haven’t tried it on Safari.