I'm a newbie using grid css and there are still things I don't get. I've the next container:
<section class="css-grid">
<div class="css-item"><img src="1.jpg" /></div>
<div class="css-item"><img src="2.jpg" /></div>
<div class="css-item"><img src="3.jpg" /></div>
<div class="css-item"><img src="4.jpg" /></div>
<div class="css-item"><img src="5.jpg" /></div>
<div class="css-item"><img src="6.jpg" /></div>
</section>
With the next CSS code:
section.css-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
grid-template-rows: 1fr;
column-gap: $gap-5px;
}
img {
width: 100%;
cursor: pointer;
}
So far, this is perfect for the single line. It makes to fit the 6 images with a minimum of 250px or a maximum of everything. Thing is, I need that, when resizing down the screen, the new line made makes the first image to take the whole width, and this gets divided by the number of new elements, I mean:
- 1 image will take the whole width (or 1fr)
- 2 images will split this between them, so 1fr and 1fr
- and so on, getting the same result as above when multiple images are in the same row (on my example when 3 images up and 3 images down).
I've tried defining a grid-template-rows: 1fr but it does nothing. I've tried using grid-auto-columns but nothing. I'm still getting along with css grid but this kind of little stuff I don't see them yet.
Any tips?
Thanks,
--
Edit - Added more info
I'm sorry for the explanation, I have a mess in my head trying to figure this out.
Ok so, this is what looks like with the line:
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
Then, using media-query, I need that, when resizing under, let's say, 1300px, when a new row is displayed cause the elements don't fit with such size, to occupy the whole width, sharing with next image if there is.
I thought my main line (grid-template-columns) was fine, but wasn't. So I tried something like:
@media screen and (max-width: 1300px) {
grid-template-columns: 1fr;
}
But this, obviously, does not work.
What I need is, instead of: this
I need the a single image to occupy the whole gap:
If there are two images, 50% for them:
When there are 3, they are fine:
I hope this time it's a little bit more clear, but I don't think so.
auto-fitso the elements will be positioned to create as many columns allowed by theminmaxconstraint.. but then you say you want the first element to take one column by itself and the following lines to adapt to that width and keep arranging their elements in columns fitting that new width still using the previous constraints? what's the width ruling? because if the container will be resized shrinking down below the minimum, the elements will arrange anyway 1 per line