I have a fixed-width container into which several variable-width elements must appear in a row, which can spill over into additional rows as necessary.
However, the beginning of each element must be aligned with the one on top of it, so in ASCII art it would look like so (say, padding of 1):
/----------------------------------\ | | | # One # Two # Three # Four | | # Five # Six | | | \----------------------------------/
In other words:
I'm trying to use flexbox for this without success.
This is the best I've come so far, using flex-wrap: wrap
for the container and flex-grow: 1
for the elements.
Problem is that the last row fills out to the edge.
justify-content: flex-start; // this does nothing
If I take away flow-grow: 1
then the elements aren't distributed equally. I also tried fiddling around with last-of-type
on the elements but it's also not enough.
Is this even possible with flexbox
, or am I going about it the wrong way?
The simplest way to do this is with a grid instead of flex and grid template columns with repeat and auto fills , where you have to set the number of pixels that you have given to each element, 100px from your snippet code. This is the proper solution to get last row items left aligned.
To set space between the flexbox you can use the flexbox property justify-content you can also visit all the property in that link. We can use the justify-content property of a flex container to set space between the flexbox.
You will see the free space at the right side of the last flex item. And if you apply margin-left: auto to the last item, the positive free space will be taken up to the left dimension and the last flex item will be pushed to the right.
After trying the suggestions here (thanks!) and searching the web long and wide, I've reached the conclusion that this is simply not possible with flexbox. Any by that I mean that others have reached this conclusion while I stubbornly tried to make it work anyway, until finally giving up and accepting the wisdom of wiser people.
There are a couple of links I came across that might explain it better, or different aspects of the requirements, so I'm posting them here for... posterity.
How to keep wrapped flex-items the same width as the elements on the previous row?
http://fourkitchens.com/blog/article/responsive-multi-column-lists-flexbox
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With