You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Reacted by Dan Tonon, Ilya Streltsyn, jonjohnjohnson, Sebastian Zartner, Yisi, Paul Alexander, Corey Worrell, Krzysztof Maczyński, jimmyfrasche, Johannes Odland and 123 more
Reacted by Luca Andrea Rossi, Ben Moran, Ege Esin, Diogo Terremoto, Germán Freixinós, Gyorgy Kallai, Alex, Kane Wang and Janosh Riebesell
Reacted by Dan Tonon, jimmyfrasche, Johannes Odland, Riley Shaw, doleance, Tobias Buschor, Vincent Rubinetti, Lea Verou, Jan Willem Henckel, Bernhard Baumrock and 36 more
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.
Reacted by Dan Tonon, Justin Wyne, Marcus Downing, Lea Verou, Lucas Gager, Sara Joy, Anibaka, Tran Quang Tien and Alex
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. 😢
Reacted by Paul Alexander, Tran Quang Tien and Alex
@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.
When balancing line breaking with items of equal width there will be multiple optimal solutions. The algorithm could prioritise full rows in the start, center or in the end of the container.
The example @SelenIT gave prioritises fuller rows in the end of the container, so that the first items grows more when using flex-grow:
Would it be possible to provide control over how the algorithm would balance items?
I.e. have something like a separate flex-wrap-balance property with possible values none, start, center or end?
Reacted by Dan Tonon, jimmyfrasche, Skye Ewers, Marcus Downing, Tobias Buschor, Roman Komarov, Vincent Rubinetti, yarusome, Raura, Ilya Streltsyn and 23 more
Reacted by Dan Tonon, Skye Ewers, Tobias Buschor, Roman Komarov, jhaysODPS, WKD, Tran Quang Tien, Ege Esin, Max Fenton, Alex and 2 more
#9102 is discussing moving balance out of text-wrap, not sure if flexbox should be consistent with the decision there or have the discussion independently.
Does this proposed feature also take into consideration situations where the individual flex children don't expand their widths beyond a certain fixed amount? In other words, situations where justify-content: space-[around|evenly|between]; comes into play?
The CSS Working Group just discussed [css-flexbox-2] Add flex-wrap: balance;, and agreed to the following:
RESOLVED: Add balance keyword to flex-wrap
RESOLVED: try to, pending compat impact, for the purpose of flex line breaking, clamp the main size to 0
The full IRC log of that discussion
<sgill> TabAtkins: 7.5 years ago i opened an issue asking for balance
<sgill> year or so ago ian started exploring an implementation. we have a mature one
<sgill> add a new value for the flex-wrap property
<iank_> `flex-wrap: nowrap | [ [ wrap | wrap-reverse ] || balance <integer [1,∞]>?]`
<sgill> add a balance keyword
<sgill> as part of line breaking we already have base size for line breaking
<sgill> iank_: can do it in O(nlogn) complexity
<sgill> TabAtkins: can also supply an integer to guarantee some number of lines
<sgill> e.g. wiki references section uses multicolumn to balance the references. somewhat expensive
<lea> q+
<astearns> q+
<sgill> one small bit we may want to correct
<sgill> currently for line wrapping flex items can become negatively sized
<sgill> allows the size of a flex line to not grow monotonically
<sgill> currently our implementation ignores that by clamping to 0
<sgill> it's a weird situation
<oriol> This is https://github.com//issues/12222
<sgill> may want to either resolve that for balancing flex items are clamped to base size of 0 and possibly go further
<sgill> by changing current flex spec. which would be a behavior change
<sgill> dholbert: not allowing this for the purposes of line wrapping?
<sgill> iank_: yes
<sgill> have not seen a case where you go past items
<sgill> two resolutions
<sgill> adding balance and figuring out the clamping behavior
<astearns> ack lea
<sgill> leaverou: still don't understand what the integer does
<sgill> TabAtkins: forces there to be at least that many flex lines
<dholbert> q+
<sgill> iank_: this is common if you would like to be balanced but at least be, for example, 2 lines
<sgill> leaverou: wonder if we could ship this later
<sgill> iank_: no. required to do the column case when you don't have a size to break against
<hober> q+
<sgill> TabAtkins: if you like at the wiki case. if they wanted to use balance they could do balance 2. without that column flexboxes would not balance since they do not have a height
<sgill> if you don't specify an integer you would like break and then balance
<astearns> ack astearns
<dholbert> q-
<sgill> leaverou: there are many use cases you want to balance a grid. instead of adding it to flex we create a separate property and shorthand flex/grid properties
<sgill> TabAtkins: they would have to be shipped at the same time
<oriol> q+
<sgill> iank_: grid case is not simple
<sgill> TabAtkins: any grid balancing we do we would probably want more controls
<sgill> leaverou: is there a way to pave the way forward?
<sgill> TabAtkins: we could just do a grid-wrap in the future
<astearns> ack fantasai
<sgill> iank_: we could always shorthand in the future but probably don't want to right now
<sgill> fantasai: we have a text-wrap property and it has a similar syntax
<sgill> good that the syntax is consistent
<sgill> in terms of integer, not sure it should be on the wrap property instead of its own
<sgill> this is case where it is similar to mulicol so maybe it would be better to match syntax there
<sgill> lots of cases where you know the number of columns you want but many don't want just want as many columns as can reasonably fit
<sgill> multicol has solved this
<sgill> reusing that here would mean the author gets familiar interface and powers
<lea> multicol has `column-fill: balance;`, but here it may not be column
<fantasai> I'm talking about `column-count` and `column-width`
<sgill> hober: in multicol you can specify column width and get as many columns as can fit. also count.
<lea> like `row-count`?
<sgill> both of your ideas together is you want the number to be in a different property and work similarly
<sgill> TabAtkins: i have minor objection. would require making flex lines a real line concept. so that they exist even if they are empty
<sgill> only have flex lines when there are items
<sgill> that's why number is part of balance
<sgill> having more control over flex lines, though, is interesting
<sgill> wouldn't say it's a bad thing to have it in. don't know if i want to solve it right nw
<lea> +1 fantasai
<sgill> fantasai: i think introducing the integer here may hinder future work
<sgill> TabAtkins: having guaranteed 2 columns seems to be a pretty core usecase
<sgill> astearns: not precluding solving that
<sgill> TabAtkins: we know people want to use it right now. should not push it off.
<sgill> astearns: agree adding the integer to flex-wrap would preclude changes to the future
<sgill> fantasai: don't think it works well to include it in the same property since it's a numeric value
<sgill> and flex wrap controls a bunch of other things
<sgill> seems like it should cascade independently
<astearns> q?
<astearns> ack hober
<sgill> hober: i just asked florian is there a compromise here? we do syntax tab wants and in the future if we add a new property we can retoraciveley do something?
<sgill> fantasai: no would be ugly
<sgill> TabAtkins: my idea is that the number of the flex lines would be the larger of the two
<sgill> fantasai: doesnt sound like great experience
<sgill> hober: worried about compat with the negative margin discussion
<sgill> iank_: don't think it would be much of a compat risk. happy to do it just for balance
<florian> q+
<astearns> ack oriol
<sgill> oriol: two questions.
<sgill> 1.) for example if you have 3 items and specify 5. you still get 3?
<sgill> iank_: yes
<astearns> maybe?
<sgill> 2.) reason provided for the number is the column flexbox case. when balancing text don't you have the same problem with orthogonal cases?
<sgill> TabAtkins: certainly same situation. but text is limited by the ICB
<sgill> probably applies somewhat but not strongly. would not object to explore something in that space
<astearns> ack fantasai
<astearns> ack florian
<sgill> florian: we think it's a bad fit because its numerical. idk why
<sgill> fantasai: sometimes when we have something that is a mode switch behavior and something in that area that is numerical we put them in separate properties to control them independently
<sgill> putting them in function notation does not get independent cascading and is awkward
<sgill> florian: unless i'm missing something the number here only works if you are in the balancing mode
<sgill> you are not choosing balance vs not balance independently with the lines
<sgill> fantasai: there are a whole bunch of other properties that don't apply if you don't turn this other thing on
<sgill> don't shove them together
<sgill> astearns: happy with resolving to add balance and decide how we want to add constraint later
<sgill> TabAtkins: that's fine. we think doing the number thing will be necessary for this feature
<sgill> PROPOSAL: Add balance keyword to flex-wrap
<sgill> RESOLVED: Add balance keyword to flex-wrap
<sgill> PROPOSAL: try to, pending compat impact, for the purpose of flex line breaking, clamp the main size to 0
<oriol> q+
<sgill> RESOLVED: try to, pending compat impact, for the purpose of flex line breaking, clamp the main size to 0
<astearns> ack fantasai
<astearns> ack oriol
Reacted by Declan Chidlow, Pablo Lillia, Zak, Renildo Pereira, Jean-Philippe Paradis, saku, Monknow, Marcus Otterström, Yisi, Jesse and 18 more
Activity
Dan503 commentedon May 25, 2019
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. 😊
tabatkins commentedon May 29, 2019
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.
Dan503 commentedon May 29, 2019
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. 😢
yisibl commentedon Jun 15, 2019
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.
SelenIT commentedon Jun 17, 2019
@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...
Dan503 commentedon Jun 17, 2019
@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.
yisibl commentedon Jul 2, 2021
@tabatkins Are there any plans to launch Flexbox2?
johannesodland commentedon Aug 1, 2022
When balancing line breaking with items of equal width there will be multiple optimal solutions. The algorithm could prioritise full rows in the start, center or in the end of the container.
The example @SelenIT gave prioritises fuller rows in the end of the container, so that the first items grows more when using flex-grow:
Would it be possible to provide control over how the algorithm would balance items?
I.e. have something like a separate
flex-wrap-balanceproperty with possible valuesnone,start,centerorend?matthew-dean commentedon Apr 28, 2023
I presume CSS Grid will get a similar value? If so, should it just be called
wrap-balance?Loirooriol commentedon Aug 21, 2023
#9102 is discussing moving
balanceout oftext-wrap, not sure if flexbox should be consistent with the decision there or have the discussion independently.proimage commentedon Oct 2, 2023
Does this proposed feature also take into consideration situations where the individual flex children don't expand their widths beyond a certain fixed amount? In other words, situations where
justify-content: space-[around|evenly|between];comes into play?manuelmeister commentedon Oct 13, 2023
I would love to see this on the csswg agenda.
Anoesj commentedon Feb 29, 2024
This would be fantastic!
mayank99 commentedon Oct 21, 2024
Would the addition of a
flex-wrap: prettyalso be tied to this issue?Unlike
balance, theprettyvalue would be mainly concerned with minimizing orphans (rather than making the rows completely balanced).18 remaining items
yisibl commentedon Nov 26, 2025
flex-wrap: balanceis primarily used to achieve layouts like those seen in Google Image Search or Flickr.com.@bfgeek explains how it works in his article.
This is undoubtedly one of the most fascinating features of CSS layouts! ✌🏻❤️
Demo: https://codepen.io/yisi/full/WbwMPBK

Hexstream commentedon Nov 26, 2025
Actually, I think you would pretty much want to enable
flex-wrap: balanceby default in your stylesheets...I use flex extensively for tabs and the results often look ugly because of the lack of this obvious feature...
bfgeek commentedon Jan 20, 2026
Once we resolve on #11480 Agenda+ to decide on syntax.
css-meeting-bot commentedon Jan 29, 2026
The CSS Working Group just discussed
[css-flexbox-2] Add flex-wrap: balance;, and agreed to the following:RESOLVED: Add balance keyword to flex-wrapRESOLVED: try to, pending compat impact, for the purpose of flex line breaking, clamp the main size to 0The full IRC log of that discussion
<sgill> TabAtkins: 7.5 years ago i opened an issue asking for balance<sgill> year or so ago ian started exploring an implementation. we have a mature one
<sgill> add a new value for the flex-wrap property
<iank_> `flex-wrap: nowrap | [ [ wrap | wrap-reverse ] || balance <integer [1,∞]>?]`
<sgill> add a balance keyword
<sgill> as part of line breaking we already have base size for line breaking
<sgill> iank_: can do it in O(nlogn) complexity
<sgill> TabAtkins: can also supply an integer to guarantee some number of lines
<sgill> e.g. wiki references section uses multicolumn to balance the references. somewhat expensive
<lea> q+
<astearns> q+
<sgill> one small bit we may want to correct
<sgill> currently for line wrapping flex items can become negatively sized
<sgill> allows the size of a flex line to not grow monotonically
<sgill> currently our implementation ignores that by clamping to 0
<sgill> it's a weird situation
<oriol> This is https://github.com//issues/12222
<sgill> may want to either resolve that for balancing flex items are clamped to base size of 0 and possibly go further
<sgill> by changing current flex spec. which would be a behavior change
<sgill> dholbert: not allowing this for the purposes of line wrapping?
<sgill> iank_: yes
<sgill> have not seen a case where you go past items
<sgill> two resolutions
<sgill> adding balance and figuring out the clamping behavior
<astearns> ack lea
<sgill> leaverou: still don't understand what the integer does
<sgill> TabAtkins: forces there to be at least that many flex lines
<dholbert> q+
<sgill> iank_: this is common if you would like to be balanced but at least be, for example, 2 lines
<sgill> leaverou: wonder if we could ship this later
<sgill> iank_: no. required to do the column case when you don't have a size to break against
<hober> q+
<sgill> TabAtkins: if you like at the wiki case. if they wanted to use balance they could do balance 2. without that column flexboxes would not balance since they do not have a height
<sgill> if you don't specify an integer you would like break and then balance
<astearns> ack astearns
<dholbert> q-
<sgill> leaverou: there are many use cases you want to balance a grid. instead of adding it to flex we create a separate property and shorthand flex/grid properties
<sgill> TabAtkins: they would have to be shipped at the same time
<oriol> q+
<sgill> iank_: grid case is not simple
<sgill> TabAtkins: any grid balancing we do we would probably want more controls
<sgill> leaverou: is there a way to pave the way forward?
<sgill> TabAtkins: we could just do a grid-wrap in the future
<astearns> ack fantasai
<sgill> iank_: we could always shorthand in the future but probably don't want to right now
<sgill> fantasai: we have a text-wrap property and it has a similar syntax
<sgill> good that the syntax is consistent
<sgill> in terms of integer, not sure it should be on the wrap property instead of its own
<sgill> this is case where it is similar to mulicol so maybe it would be better to match syntax there
<sgill> lots of cases where you know the number of columns you want but many don't want just want as many columns as can reasonably fit
<sgill> multicol has solved this
<sgill> reusing that here would mean the author gets familiar interface and powers
<lea> multicol has `column-fill: balance;`, but here it may not be column
<fantasai> I'm talking about `column-count` and `column-width`
<sgill> hober: in multicol you can specify column width and get as many columns as can fit. also count.
<lea> like `row-count`?
<sgill> both of your ideas together is you want the number to be in a different property and work similarly
<sgill> TabAtkins: i have minor objection. would require making flex lines a real line concept. so that they exist even if they are empty
<sgill> only have flex lines when there are items
<sgill> that's why number is part of balance
<sgill> having more control over flex lines, though, is interesting
<sgill> wouldn't say it's a bad thing to have it in. don't know if i want to solve it right nw
<lea> +1 fantasai
<sgill> fantasai: i think introducing the integer here may hinder future work
<sgill> TabAtkins: having guaranteed 2 columns seems to be a pretty core usecase
<sgill> astearns: not precluding solving that
<sgill> TabAtkins: we know people want to use it right now. should not push it off.
<sgill> astearns: agree adding the integer to flex-wrap would preclude changes to the future
<sgill> fantasai: don't think it works well to include it in the same property since it's a numeric value
<sgill> and flex wrap controls a bunch of other things
<sgill> seems like it should cascade independently
<astearns> q?
<astearns> ack hober
<sgill> hober: i just asked florian is there a compromise here? we do syntax tab wants and in the future if we add a new property we can retoraciveley do something?
<sgill> fantasai: no would be ugly
<sgill> TabAtkins: my idea is that the number of the flex lines would be the larger of the two
<sgill> fantasai: doesnt sound like great experience
<sgill> hober: worried about compat with the negative margin discussion
<sgill> iank_: don't think it would be much of a compat risk. happy to do it just for balance
<florian> q+
<astearns> ack oriol
<sgill> oriol: two questions.
<sgill> 1.) for example if you have 3 items and specify 5. you still get 3?
<sgill> iank_: yes
<astearns> maybe?
<sgill> 2.) reason provided for the number is the column flexbox case. when balancing text don't you have the same problem with orthogonal cases?
<sgill> TabAtkins: certainly same situation. but text is limited by the ICB
<sgill> probably applies somewhat but not strongly. would not object to explore something in that space
<astearns> ack fantasai
<astearns> ack florian
<sgill> florian: we think it's a bad fit because its numerical. idk why
<sgill> fantasai: sometimes when we have something that is a mode switch behavior and something in that area that is numerical we put them in separate properties to control them independently
<sgill> putting them in function notation does not get independent cascading and is awkward
<sgill> florian: unless i'm missing something the number here only works if you are in the balancing mode
<sgill> you are not choosing balance vs not balance independently with the lines
<sgill> fantasai: there are a whole bunch of other properties that don't apply if you don't turn this other thing on
<sgill> don't shove them together
<sgill> astearns: happy with resolving to add balance and decide how we want to add constraint later
<sgill> TabAtkins: that's fine. we think doing the number thing will be necessary for this feature
<sgill> PROPOSAL: Add balance keyword to flex-wrap
<sgill> RESOLVED: Add balance keyword to flex-wrap
<sgill> PROPOSAL: try to, pending compat impact, for the purpose of flex line breaking, clamp the main size to 0
<oriol> q+
<sgill> RESOLVED: try to, pending compat impact, for the purpose of flex line breaking, clamp the main size to 0
<astearns> ack fantasai
<astearns> ack oriol
bfgeek commentedon Jan 30, 2026
For the algorithm we currently use:
I.e. We minimize the square of the free-space on each flex-line across all lines.
Add parsing support for flex-wrap: balance