I have started learning CSS Grid from yesterday and I have a doubt.
I was reading about how can I use auto-fit
to place the elements evenly in the rows from this website.
The post says that
auto-fit FITS the CURRENTLY AVAILABLE columns into the space by expanding them so that they take up any available space. The browser does that after FILLING that extra space with extra columns (as with
auto-fill
) and then collapsing the empty ones.
It says that empty columns will be collapsed and the remaining element will share the extra space evenly.
However, when I was trying to use this in my code I don't find the blank columns collapsing and the remaining valid elements taking extra space.
.container {
height: 100%;
display: grid;
grid-auto-rows: 150px;
gap: 5px;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}
.container > div {
text-align: center;
font-weight: 600;
font-size: 30px;
color: white;
}
div > img {
height: 100%;
width: 100%;
}
body {
margin: 10px;
}
<div class="container">
<div><img src="./1.jpeg" alt="image" /></div>
<div><img src="./2.jpeg" alt="image" /></div>
<div><img src="./3.jpeg" alt="image" /></div>
<div><img src="./4.jpeg" alt="image" /></div>
<div><img src="./5.jpeg" alt="image" /></div>
<div><img src="./6.jpeg" alt="image" /></div>
<div><img src="./7.jpeg" alt="image" /></div>
<div><img src="./8.jpeg" alt="image" /></div>
<div><img src="./9.jpeg" alt="image" /></div>
<div><img src="./10.jpeg" alt="image" /></div>
<div><img src="./11.jpeg" alt="image" /></div>
<div><img src="./12.jpeg" alt="image" /></div>
<div><img src="./13.jpeg" alt="image" /></div>
<div><img src="./14.jpeg" alt="image" /></div>
<div><img src="./15.jpeg" alt="image" /></div>
<div><img src="./16.jpeg" alt="image" /></div>
<div><img src="./17.jpeg" alt="image" /></div>
<div><img src="./18.jpeg" alt="image" /></div>
</div>
Here's is the OUTPUT:
When I inspected the output I found that the last row's blank columns have not collapsed. Why?
Also why the last two images have not taken equal remaining space which it should take?