Grid layout - make last item span to the last column
I was thinking if there was a way to make dynamically the last element in a given grid to span to the last column of at the end there are some empty slots.
I know that using
But this only works when we know x but now we don't know x.
I also thought of using
But this one moves the element to the end without spanning it allover.
Is there a way to do this for grid element like with flex-grow:1 for flex elements.
18 Replies
How do you do not know how much x is? Are you using auto-fit/fill?
Let say you have a parent with 2 columns
When you get an even number of children there will not be left space
But in case of odd number of children there will be one empty slot for the last row...
For this case for even case x will be 0 and 1 for odd case
If that's the usecase, then, can't you do something like this?
This works of course assuming the parent always has 2 columns
I'm going to check this but I think in case the number of cols is no longer 2 this wouldn't work well...
I have illustrated the requirement at the following address: https://github.com/vickbk/vickbk.github.io/tree/main/frontend/grid-span
Here is a live preview of what is expected: https://vickbk.github.io/frontend/grid-span/
GitHub
vickbk.github.io/frontend/grid-span at main ยท vickbk/vickbk.github.io
github page repo. Contribute to vickbk/vickbk.github.io development by creating an account on GitHub.
@Vick, something like this works
And yes, it works only if you know how many columns you have
Yes ofcourse
I'm looking for a way of not keeping track of the number of cols
I initially thought it's must be there somewhere ๐ค ๐
A cool
๐
No I dont think there's a way to do that
This may help give you some ideas
https://youtu.be/ZDFgJeQOn20?si=9pN4pYKDHl7YNuLd
Kevin Powell
YouTube
A CSS Trick to select a specific row with Grid auto-fit/fill
Looking to step up your CSS Game? Check out https://cssdemystified.com
๐ Links
โ
Code for last row only: https://codepen.io/kevinpowell/pen/ogjwxyv/19639a71b6ff26e1bf87738fa6db12d5
โ
Code for first two rows only: https://codepen.io/kevinpowell/pen/vENZGvG/14a4bdacb2fa79bbdd9cd1e400b57097
โ
Code for 2nd row only: https://codepen.io/kevi...
if you know the width of your grid column you might also be able to figure it out via container queries?
I found a way to fix it with the new sibling-index() currently supported in chromium browser only but for a full grid it is breaking the design:
Also in case the some elements in the grid are spanning multiple columns the design can also get broken.
Live demo: only working in latest chromium browsers

If you know the number of columns, as per your code, this is relatively simple as lko showed you a couple of days ago, without the need for
sibling-index() or mod()yeah... I will stick to it
I actually created a demo similar to your idea a couple of weeks ago, using container queries (though it could equally be media queries) to define the number of columns and then a single mod(sibling-index()) calculation to define the start position of the final cell.
(in my demo I wanted the final cell to expand the remaining columns on the last row)
Chris Bolson
CodePen
Using mod() & @container query to make last cell span remaining gri...
This demo shows how we can use container media queries and the new mod() function to make the last cell to span the remaining columns in the grid...
Works exactly as I was thinking...
but either of our solutions are suffering if some other element span more than one column

yea, that would be difficult to figure out the column index when there are items that span. think you would need some js for that
here an example
https://codepen.io/MarkBoots/pen/emJREML?editors=1111

Beautiful until our last-span property comes out