Skip to main content

Threads / CSS

148

Why does `text-wrap: balance` do nothing on my headings?solved

NO Nadia Osei · 5h ago · 34 replies

I have a heading that wraps to five or six lines on mobile and text-wrap: balance appears to do absolutely nothing. Same heading at three lines and it balances perfectly.

No overrides that I can find, no flex weirdness, and it behaves the same in Chrome and Safari. Reduced test case is in the reply below. Am I misreading what the property is for?

6 replies

  • 62

    WT Wes Tremayne · 4h ago

    The property only balances up to four lines — past that browsers bail out because the line-breaking cost is quadratic. If your heading is wrapping to five, you will get nothing and no warning.

  • 18

    NO Nadia Osei · 4h ago

    That is exactly it, mine was six lines. Shortening the heading fixed it, which is probably the real lesson.

  • 41

    MH Marit Halvorsen · 3h ago

    Worth adding: it is balance for headings and pretty for body copy. pretty has no line limit because it only fixes the last line, so it is the safe one to apply broadly.

  • 12

    WT Wes Tremayne · 3h ago

    Yes — and pretty is cheap enough that I set it on p globally now.

  • 9

    DA Dilan Aksoy · 2h ago

    Unrelated but related: it also does nothing inside a flex or grid item that has not been given a width, because there is no line box to balance against yet.

  • 4

    JM Josephine Marr · 1h ago

    Marking this solved. The four-line limit is not in the MDN summary and it should be.

Add a reply

A reply needs some text in it.

Markdown works. Be kind; rule one applies.