Skip to content

[css-flexbox-2] Add flex-wrap: balance; #3070

Closed

Description

@tabatkins

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.

Activity

  1. Dan503 commented on May 25, 2019

    @Dan503

    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. 😊

  2. tabatkins commented on May 29, 2019

    @tabatkins
    MemberAuthor

    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.

  3. Dan503 commented on May 29, 2019

    @Dan503

    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. 😢

  4. yisibl commented on Jun 15, 2019

    @yisibl
    Contributor

    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.

  5. SelenIT commented on Jun 17, 2019

    @SelenIT
    Collaborator

    @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...

  6. Dan503 commented on Jun 17, 2019

    @Dan503

    @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.

  7. yisibl commented on Jul 2, 2021

    @yisibl
    Contributor

    @tabatkins Are there any plans to launch Flexbox2?

  8. johannesodland commented on Aug 1, 2022

    @johannesodland
    Contributor

    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:

    more like creating layouts like this

    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?

    Screenshot 2022-08-01 at 18 59 03

  9. matthew-dean commented on Apr 28, 2023

    @matthew-dean

    I presume CSS Grid will get a similar value? If so, should it just be called wrap-balance?

  10. Loirooriol commented on Aug 21, 2023

    @Loirooriol
    Contributor

    #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.

  11. proimage commented on Oct 2, 2023

    @proimage

    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?

  12. manuelmeister commented on Oct 13, 2023

    @manuelmeister

    I would love to see this on the csswg agenda.

  13. Anoesj commented on Feb 29, 2024

    @Anoesj

    This would be fantastic!

  14. mayank99 commented on Oct 21, 2024

    @mayank99

    Would the addition of a flex-wrap: pretty also be tied to this issue?

    Unlike balance, the pretty value would be mainly concerned with minimizing orphans (rather than making the rows completely balanced).

  15. 18 remaining items

  16. yisibl commented on Nov 26, 2025

    @yisibl
    Contributor

    flex-wrap: balance is 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
    Image

  17. Hexstream commented on Nov 26, 2025

    @Hexstream

    flex-wrap: balance is primarily used to achieve layouts like those seen in Google Image Search or Flickr.com.

    Actually, I think you would pretty much want to enable flex-wrap: balance by default in your stylesheets...

    I use flex extensively for tabs and the results often look ugly because of the lack of this obvious feature...

  18. bfgeek commented on Jan 20, 2026

    @bfgeek

    Once we resolve on #11480 Agenda+ to decide on syntax.

  19. moved this to FTF agenda items in CSSWG January 2026 meetingon Jan 22, 2026
  20. moved this from FTF agenda items to Thursday morning in CSSWG January 2026 meetingon Jan 26, 2026
  21. moved this from Thursday morning to Skipped/Extra things in CSSWG January 2026 meetingon Jan 29, 2026
  22. moved this from Skipped/Extra things to Thursday afternoon in CSSWG January 2026 meetingon Jan 29, 2026
  23. css-meeting-bot commented on Jan 29, 2026

    @css-meeting-bot
    Member

    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
  24. bfgeek commented on Jan 30, 2026

    @bfgeek

    For the algorithm we currently use:

    • We use the "standard" Knuth-Plass type scoring where we minimize:
    $$min\left\{\sum_i^{N} \left({\mathit{free\_space}}_{i}\right)^2 \right\}$$

    I.e. We minimize the square of the free-space on each flex-line across all lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    Sponsor
    SponsoredKunjungi sekarang
    Promo