Csswg-drafts: [css-flexbox-2] Add flex-wrap: balance;

Created on 31 Aug 2018  Â·  6Comments  Â·  Source: w3c/csswg-drafts

We discussed having a flex-wrap:balance value, similar to text-wrap:balance, to even out the length of pre-flexed lines. We should add to level 2.

css-flexbox-2

Most helpful comment

@yisibl for your case, the Grid layout seems to be a better option, as it preserves the horizontal rhythm of the lines automatically. But this issue is about the different case, more like creating layouts like this without manually writing scary CSS selectors for each number of items...

All 6 comments

I like the sound of this but can you add an example of what this would do?

Would it be a property that tries to place an equal number of flex items in each flex block at all times? That would be pretty awesome. 😊

Not necessarily equal number, but an equal sum-of-flex-basis, yeah. That way you don't end up with the frustrating scenario where you have twenty items, and nineteen of them fit on the first line, leaving a single lonely item flexing to fill THE ENTIRE SECOND LINE.

This luckily isn't cyclic layout; we already know all the flex basis values when we do linebreaking, as that's how we do it in the first place. We just need to attempt to balance the lines' sums, like text-wrap:balance does.

I'm not sure whether we want to specify the same algorithm, or just do something simple and trivial like "greedily consume until you've met or exceeded TOTALBASIS/LINES worth of flex-basis (or have filled the line), then break to next line". That won't always produce an ideal balancing, but it's super easy to spec and implement and test, and no perf impact since the only new information you need to collect is the sum of all basises, which you can collect while you're calculating all the basises, and then it's just a linear line-filling like normal.

In most normal cases, where items are all approximately the same width, tho, it'll balance quite well.

Thank you!

I ran into this issue constantly when flexbox first came out. Let's say I was making a layout with 3 equal width columns. I really loved how adding flex-grow: 1; to all the list items would mean having 1 item lead to 1 full width column. 2 items would lead to 2 equal width columns. 3 items leads to 3 equal width columns. But then if you have 4 items, you have 3 equal width columns but the last item is full width. ☹

This makes the last item look visually far more important than all the previous items. It's not supposed to look any more important than the others though.

Idealy, having 4 items would lead to 2 equal width columns and 2 rows.

Because of this issue, I just stopped trying to use this technique and just let the items not fill the entire container. 😢

What we can do now seems to be to add empty elements at the end and set them to the same width.

Demo: https://codepen.io/yisi/pen/ZGYNwG

It is hoped that the specification can be added as soon as possible.

@yisibl for your case, the Grid layout seems to be a better option, as it preserves the horizontal rhythm of the lines automatically. But this issue is about the different case, more like creating layouts like this without manually writing scary CSS selectors for each number of items...

@SelenIT I like that in your example when there are an odd number of items that don't divide well into each other it is the later rows that are given the extra items rather than the earlier items.

Earlier items are more likely to be more important. There should probably be a property to define if odd items fill more toward the start middle or end though for more design options.

Was this page helpful?
0 / 5 - 0 ratings