How to Make a Responsive and Reusable Flexbox Component for Different Number of Boxes?
Hey everyone,
I’m working on a component that uses display: flex . Currently, I have 3 boxes that are working fine, and they adjust nicely when resized. However, I want this component to be reusable in other places where I might want 4 boxes, or even just 2 boxes, without having to rewrite the code.
My goal is to make the component flexible enough so that I can reuse it for any number of boxes (2, 3, or 4 boxes) . Is that possible?
5 Replies
I'm struggling to understand the question, because I don't see anywhere that your current code is particularly restricted to or designed for three boxes? Like, you have a flex container with wrapping and a child class that will grow to fill the space if it can. That should work for any number of elements, so what's the problem?
Yeah you have a point. My qeustion is kinda dumb. So yeah i can add boxes or remove boxes and thats fine desktop wise. But what if i want depending on the number of boxes to either have a 2x2 layout on mobile (if its 4 boxes) or just 1 box per line( if its 3 boxes). it's impossible right ?
you have 2 options:
- manually set the grid-column-end of the last element
- use flexbox with
flex: 50% 1 1
set on the children
the first one is the preferred option for your caseI went with this , i somehow made it work clicking around , still gotta figure it out how cause its a bit messy :
https://codepen.io/Christian-Stoyanov/pen/OJeKaQR
Only reason im not sharing html is because i use twig (html + php + syntactic sugar in a way)
it looks messy, but if it works then it works
i can't comment about that because it's missing the html