How do i acheive this in flexbox without creating two rows of div

lets say i have an array of 7 elements, the max no of elements permited in a row is 4 and the rest should go in row(2). These remaining 3 elemts should start in the center (example image ). How can i acheive this?
No description
8 Replies
Mannix
Mannixā€¢3mo ago
use grid ?
Avinash
Avinashā€¢3mo ago
yes i tried with grid, created 6 colums and each box spanning 2coloumns, since im using tailwind and react this is getting confused really quick
Jochem
Jochemā€¢3mo ago
Then don't use tailwind for this part
Mannix
Mannixā€¢3mo ago
don't use tailwind period šŸ˜‚
clevermissfox
clevermissfoxā€¢3mo ago
youll want to do some flex-hacking if you dont want to use grid (which would be easier). make a breakpoint where the container is wide enough to hold three wide so the other two will wrap. in your codepen youre using 7 boxes and in your screenshot there are five. this video may help (the flex part to create a 'breakpoint') and the codepen is linked in the vid description https://youtu.be/LEkFckg7sfw?si=ZpFAm6LaXS_e2_nW but grid would be a lot simpler
Kevin Powell
YouTube
Use this instead of media queries or container queries
Media queries are a bit limited in how they work, and while container queries go a really long way in solving a lot of those limitations, there are intrinsic patterns that we can follow that allow us to do things that neither one of them can do. The setup is a bit strange and can look complex, but, in my opinion, it's also 100% worth it! šŸ”— Lin...
snxxwyy
snxxwyyā€¢3mo ago
You can set the children of the flex container to have a flex-basis of 25%, if you add a gap make sure to take it away from the 25% like so, calc(25% - gap), otherwise the gap will take up space not allowing the desired amount of children on one row. Then on the flex container put justify-content: center and add a flex-wrap: wrap. That should do it for you.