Extende colums to fill empty row with grid css
Hi everyone!
I set a basic grid with only two columns, 1fr each.
I would like to know if it's possible to extend automatically the size of the green box, that I draw in the example image, to fill the gap in the second row, first column. Is it possible?

29 Replies
i think you can use minmax(1f, auto)
no, doesn't work
No, it doesn't
Because I have a situation where under the green box it could be empty or filled with another content (let's call it a box, for semplicity sake).
Same thing viceversa, on the right column
Does your grid only ever have 4 "boxes"?
Yeah
maybe some grid area funkery will do the trick
Mh, the idea is to use that solution, but doesn't seems so flexible to me
But yeah, I will never have more than 4 boxes (or at least for now)
but what exactly are you trying to do?
Is the flow automatic (natural) or are you specifically defining the contents of each cell?
If you want a natural flow, you could do somethig like this to detect of there are only 3 items. This will tell the first "box" to span 2 rows and push the 3rd box contents onto the 4th cell:
However if you are specifically defining what goes where, this will probably need to be done programatically
The goal is that I can have this dynamic grid where sometimes, based on some results, I can have 1, 2, 3 or 4 boxes.
So I want to dynamically sizing these boxes to fill the entire grid
Alternatively you could tell the first child to always span the 2 rows unless the container has a 4th item
Mhh, seems it doesn't work
so, how many columns and rows do you want for 1, 2, 3 and 4 children?
2 columns and 2 rows
so, always 2 columns and 2 rows?
Yep
all the same size?
No, they may vary
and vary based on what?
Can you share the code in a codepen or similar to help us understand?
Based on the content
As soon as I can yes, I'm not working on my personal pc rn
Oh eventyally I found this interesting article and I made it works
https://www.matuzo.at/blog/2022/counting-children
Manuel Matuzovic
Parents counting children in CSS - Manuel Matuzovic
Using :has() to select parent elements that have a specific number of children.
so, not based on content then?
Mh yeah, maybe I was wrong, sorry. The idea is to fill the area of the grid, indipendently from the size of the content
So If a I have 2x2 grid and I have only 3 boxes, the first one should occupy first colum and the first half of the 2 row, the rest of the items the leftover available space, respectively the second column first row, second column second half of the second row
sounds like you need flexbox
add
display: flex
to the parent, then add flex: 1 0 50%
to the children
1 child: takes 100% of the width
2 children: 50/50
3 children: 50/50 and then 100%
4 children: 50/50 and 50/50
btw, add flex-wrap: wrap
to the parentEdit fiddle - JSFiddle - Code Playground
JSFiddle - Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle.
there, an example of it working
Thx, I'll give it a try for sure
you're welcome