More elegant fit for elements auto stacking than grid-template-columns: repeat(auto-fit, n);

As you can see on the pictures, that empty space is eventually created on certain screen sizes that look super ugly. Any more dynamic and elegant ides where cards will fill up all the space and eventually stack like in the picture, outside of hard codding it for each screen size? CSS code:
css
display: grid;
width: 100%;
grid-template-columns: repeat(auto-fit, max(13.313rem));
gap: 1.5rem;
css
display: grid;
width: 100%;
grid-template-columns: repeat(auto-fit, max(13.313rem));
gap: 1.5rem;
No description
No description
33 Replies
b1mind
b1mindā€¢3w ago
grid-template-columns: repeat(auto-fit, minmax(320px, 1fr); something like that, you can magic number your rem to find the right size you want grid-template-columns: repeat(auto-fit, minmax(min(320px, 100%), 1fr); or something like this too Don't be afraid of using a media query though often times that is just what you need to make things fit how you want šŸ„²
croganm
croganmā€¢3w ago
What b1mind said is good! However, flex with a flex wrap may also work for your situation. Just depends on exactly what you're going for @Dovah
b1mind
b1mindā€¢3w ago
Flex works much differently though, and not sure shrink/grow would work for wrapping things and filling things as well as grid. not sure... I mean I am sure it does not do as well of a job at this.
croganm
croganmā€¢3w ago
It does indeed, 100% depends on what they're exactly going for
b1mind
b1mindā€¢3w ago
šŸ¤·ā€ā™‚ļø depends what you need and how you need it to behave though for sure.
Dovah
DovahOPā€¢3w ago
Will test the first solution and come back with results, but I'm pretty sure that is what I want!
croganm
croganmā€¢3w ago
I prefer the grid approach myself. They just didn't quite give a good explanation in terms of what they want it to do fully
b1mind
b1mindā€¢3w ago
Again don't be afraid to use a media query at a breakpoint either, not something to avoid just not something to go overboard with.
Dovah
DovahOPā€¢2w ago
I did thought about wrap, but I'm pretty sure I had similar situation in past project and I went with grid, that is where I pulled the code I showed you. xD Either way, will come back when I get to work on project! Thanks for all the help Oh it is a media query, I'm just always wondering about more dynamic aproaches, as Kevin himself teaches us. xD When I can't thing of any, or if it is easier I do go with more breakpoints. think* Yup. xD I always drop just a bite to not bother too much, since it's usually simple solution, but in general there are always more options and it is a guessing game when you try to answer without me bringing more info. xD Damn, it works perfectly Thanks! xD So basically it's 1FR width until room for my custom size is created? @b1mind
b1mind
b1mindā€¢2w ago
min max works the same here so its 320px min or 100% (so it can contract smaller if needed) and max of 1fr which works like normal. The key is the auto-fit keyword which will "fit" the remaining space until the min is hit. Thats how my mental model has it at least xD Fun fact is you can actually do two minmax() in auto fit as a repeat too
b1mind
b1mindā€¢2w ago
Well two being any number xD so tech you could auto-fit/fill a repeat of things like this
Dovah
DovahOPā€¢2w ago
Ok that is brain overflow for now xD Will need to absorb it later xD
b1mind
b1mindā€¢2w ago
yea I don't know how often you would do that but shows how auto-fit works.
Dovah
DovahOPā€¢2w ago
Wait How do I resize it horizontaly in codepen? xD Ok I just f12-ed xD
b1mind
b1mindā€¢2w ago
I keep my code blocks to the side so I can just grab the middle bit and drag around. But yes devtools also šŸ˜„
b1mind
b1mindā€¢2w ago
No description
Dovah
DovahOPā€¢2w ago
Oh xD Hmmmm super interesting Will bookmark it and analyze it later But yes, that is peprfect solution for what I asked !
b1mind
b1mindā€¢2w ago
glad that worked for ya! šŸ¤˜ marked solved (tag)
Dovah
DovahOPā€¢2w ago
Is there a height only version? Nto sure If I'll need it though Like for card to go from ex: 212px to 240px ? Height-vise
b1mind
b1mindā€¢2w ago
don't restrict heights let the content be free
Dovah
DovahOPā€¢2w ago
Rips my shirt off jk
b1mind
b1mindā€¢2w ago
the more you define height the worse your life will be in CSS
Dovah
DovahOPā€¢2w ago
WELL tell that to project designer xD But yes I understand I was just wondering
b1mind
b1mindā€¢2w ago
I mean thats our job to tell them šŸ˜‚ pixel perfect is a lie
Dovah
DovahOPā€¢2w ago
It's free project so I can only complain with imaginary designer xD
b1mind
b1mindā€¢2w ago
not something that fits the constraints we have as "responsive" web developers Its up to you to code it in such away
Dovah
DovahOPā€¢2w ago
Yup! I shoul definetely start NOT* going pixel perfect
b1mind
b1mindā€¢2w ago
no its a lie
Dovah
DovahOPā€¢2w ago
fixed xD
b1mind
b1mindā€¢2w ago
Stop trying for pixel perfect its counter productive to responsive design there ya go ! hahah
Dovah
DovahOPā€¢2w ago
I'm shaking my rust off, both code vise and brain strain vise, so I'm just going with less resistance route instead, so what I see I do, not much thinking to not overload myself. xD Either way, thanks! Spot on with solution. No more rambling
clevermissfox
clevermissfoxā€¢2w ago
Woah. I don't even understand what the two mimax are doing here, i assumed it would be something like repeat(autofit, minmax( minmax(0px, 300px), 1fr) ). Basically a nested minmax instead of a min(). But this is just two in a row. How does it know which to choose?
* EDIT: OH!! I've needed this behaviour and told it wasn't possible. It's repeating the two cols, one is set by the first minmax 300px,1fr; the second set by the second minmax (n)px, 0.5fr. Sooo cool!!! * Very cool to know it's possible though will have to play around!
Want results from more Discord servers?
Add your server