CSS grid that's constrained vertically

Finding it pretty difficult to recreate this pattern tbh Each image needs a fixed aspect ratio (easy enough), but as the grid grows the content should be constrained by the grid height and overflow/grow horizontally if it needs to. Problem I've currently got is how the horizontal space is distributed between columns rn (2nd image) Is this possible? https://codepen.io/nwoodward/pen/MYgMKgX?editors=1010
No description
25 Replies
Nick
NickOP3w ago
No description
Chris Bolson
Chris Bolson3w ago
I suspect that you are over defining columns, rows and, most importantly, heights. You can't expect the content to both maintain the aspect-ratio and fill the whole viewport at the same time, especially if the viewport size changes (responsive). I have forked your CodePen, cleaned out some of the unnecessary column and span definitions and removed the heights. However, I don't know it this is actually what you are after.
Nick
NickOP3w ago
Hi Chris, thanks for taking a look. So I've made a couple of examples to show what I'm after. Sorry I wasn't more clear in the OP I'm basically pretty easy about the requirements, outside of the grid never overflowing the screen vertically and the fixed image ratio. If there's overflow horizontally at smaller sizes I'll just change the grid layout
No description
Simon
Simon3w ago
What's that white-space beneath each image that gets proportionally bigger when the img increases in size? edit: I believe aspect-[238/153] on the div causes it. Shouldn't that be on the img or even removed?
No description
Chris Bolson
Chris Bolson3w ago
Ah, good spot. As you have a fixed aspect ratio on the cells this inevitably leads to different heights, espcially taking into account the grid gap. I have now added items-stretch (align-items: stretch;) to the parent to ensure that the cells occupy the full height. However this may cause the aspect-ratio to "break" slightly as the cell height is being forced.
Nick
NickOP3w ago
So right now your example expands and contracts with the width of the screen, with any overflow of the viewport being vertical. I'm after the opposite if possible - I'd like the grid to expand and contract with the height of the screen, with any overflow of the viewport being horizontal (if you look at the examples with the red backgrounds, none of them overflow vertically)
Nick
NickOP3w ago
------------------ My only issue with this demo (https://codepen.io/nwoodward/pen/mybZPpG?editors=1010) is that the horizontal space in the grid is not collapsing to fit the size of the content I'm just trying to get rid of the gaps here in red
No description
Nick
NickOP3w ago
as the screen height gets shorter, the problem gets worse
No description
Chris Bolson
Chris Bolson3w ago
hmm, without defining specific column widths I am not sure how you could do this 🤔 Whilst it might not be exactly the correct aspect, you could try adding aspect-[238/153] to the parent container. of course that won't work if you have more rows 🤔
Nick
NickOP3w ago
yeah that might be a way to do it - just eyeball the aspect nah, the number of rows can be set manually, don't mind doing that i'm just surprised there isn't a way to shrink the columns to fit the content?
Nick
NickOP3w ago
like they're just evenly spaced here, but something like min-fit doesn't work
No description
Nick
NickOP3w ago
*w-fit yeah I think this is it - the idea that if all the images are the same aspect ratio, the grid can just be given the same aspect ratio https://codepen.io/nwoodward/pen/VYZJjdJ?editors=1010 *turns out it's just a fluke that this works 😄
ἔρως
ἔρως3w ago
if you had 0 gap, then you could use the aspect ratio for the grid however, the gap makes the aspect ratio a variable, and it changes depending on the size of the screen this is because the gap is an absolute size
Nick
NickOP3w ago
gap-[2%]?
ἔρως
ἔρως3w ago
you have to take into account the gap anyways but with percent it is a lot easier
Nick
NickOP3w ago
aspect ratio does seem to be working in this example, even at different screen heights: https://codepen.io/nwoodward/pen/VYZJjdJ?editors=1010 although i'm struggling to apply it to my project, annoyingly
ἔρως
ἔρως3w ago
it will work for the images, and it may look like it is working for the grid, but it wont be correct with a gap, the grid is no longer - say - 2:3 it's (2 + 2x gap):(3 + 2x gap)
Nick
NickOP3w ago
i don't doubt it, but it definitely seems to work fine. maybe that's a quirk in FF 😄
No description
No description
ἔρως
ἔρως3w ago
thats because the difference is minuscule, but exists you can see the tiny space on the right side
Nick
NickOP3w ago
do you know why something like auto-cols-min doesn't make the col size the same as the content? or have i completely misunderstood it? on an example like this: https://codepen.io/nwoodward/pen/MYgMpyq?editors=1010
Nick
NickOP3w ago
In my naive mind, all i have to do is get the col size to match the content width
No description
Nick
NickOP3w ago
auto-cols-min just completely collapses
No description
ἔρως
ἔρως3w ago
do you have an height set?
Nick
NickOP3w ago
not in pixels, but i can do in vh or %?
ἔρως
ἔρως3w ago
i wouldnt

Did you find this page helpful?