Dynamic Grid Columns

I created a grid layout. Can I make the .container dynamically use the remaining space? Here is my jsfiddle: https://jsfiddle.net/withLume/wqb674u9/2/
Dynamic Grid Layout - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
10 Replies
Chris Bolson
Chris Bolsonβ€’11mo ago
What remaining space are you referring g to? The extra space at the end is due to the padding you have on the images element.
Lume
Lumeβ€’11mo ago
Currently in the fiddle i have 6 images and the container takes 2 slotes. So everthing looks good. But when I remove an image the container is not long enough, it only spans accross 2 slots. And if i have 5 instead of 6 images, it should look like this: Do you understand?
Kevin Powell
Kevin Powellβ€’11mo ago
That's not how grid works. You could use a grid-column-end: -1 so it always ends at the end, but without a start defined it's not going to do what you want it to. I'd use flexbox for something like that.
Lume
Lumeβ€’11mo ago
When should I use grid and when should i use flexbox? This layout looked more like a grid layout πŸ˜…
Kevin Powell
Kevin Powellβ€’11mo ago
Normally, I'd agree πŸ˜„ But grid is all about defined spaces, as soon as something needs to be more independent, then flexbox You could do this using :has() to count the number of children, and then have that get the .container to have the right column and span count, but I'm not sure if it's totally worth the effort
Lume
Lumeβ€’11mo ago
Yeah, it might be overengineered.
Jochem
Jochemβ€’11mo ago
the alternative would be to set the start and end columns with javascript for the last element. If you only set them with JS, your fallback would be to just have it as a regular sized grid item
Chris Bolson
Chris Bolsonβ€’11mo ago
This is the sort of case where it would be able to use counter() to calculate the current grid column πŸ€” If that were possible you could use this this; grid-column: counter(cols) / -1; Though grid-column: auto / -1; would be even nicer πŸ˜€
Lume
Lumeβ€’11mo ago
I tried using css vars and then count the elements. It didn't work. Btw. this is the end result: https://jsfiddle.net/withLume/wqb674u9/43/ Is it possible to further simplify it? Currently it uses a lot of css magic πŸ˜…
Dynamic Grid Layout - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
Chris Bolson
Chris Bolsonβ€’11mo ago
Your solution clearly works however it still breaks if you remove one of the images as you originally mentioned. Your last comment re css vars actually made me think - this could be done using CSS custom properties (variables) though is still a bit of a hack and could get messy depending on how much flexibility as regards the number or cells (images on your case) you have. I have thrown together a quick codepen to show a possible solution: https://codepen.io/cbolson/pen/XWyVXRz. I'm sure that somebody with a better understanding of nth-child calculations could come up with a more graceful way to define the custom variable without having to define it for each column.... I tried using various combinations but ended up going the basic route. Then of course this would all have to be reset for each breakpoint..... Not a great solution but I just wanted to see if it could be done without JavaScript
Want results from more Discord servers?
Add your server
More Posts