Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display wrapping flex items as space-between with last row aligned left?

Tags:

html

css

flexbox

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:

  • The first element of every row must be left-aligned
  • The last element of every row (except for the final row) must be right-aligned
  • Every element must be left-aligned to the element above it

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?

like image 879
Guy Benron Avatar asked Dec 17 '15 09:12

Guy Benron


People also ask

How do I align my last flex to the left?

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.

How do you put space between items in Flex?

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.

How do I align my last flex to the right?

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.


1 Answers

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

like image 101
Guy Benron Avatar answered Sep 23 '22 11:09

Guy Benron