Enhancing Web Typography with text‑wrap: balance

Hero image for Enhancing Web Typography with text‑wrap: balance. Image by Jazmin Quaynor.
Hero image for 'Enhancing Web Typography with text‑wrap: balance.' Image by Jazmin Quaynor.

At the end of last month, I wrote about textwrap: pretty and how this recent addition to CSS makes some of the previous techniques I had used and talked about for controlling typography onscreen especially when it comes to avoiding runts of orphans at the end of paragraphs redundant.

Here, I'd like to discuss the close relation; textwrap: balance. Although similar (in that it's a relatively new addition to the CSS Text Module draft), using balance instead of pretty achieves quite a different visual effect by balancing lines of text against one another, which is especially useful for shorter text elements like page titles and headlines.

It's a different approach to textwrap: pretty, and achieves a different effect, but textwrap: balance is equally important when it comes to displaying visually appealing and harmoniouslybalanced text in our websites.


Understanding textwrap: balance

textwrap: balance is a new(ish) CSS property that aims to distribute the length of each line of text within a block more evenly. Unlike textwrap: pretty, which focuses on preventing orphans at the end of paragraphs, balance ensures that the lines in a text block are of similar length, creating a more symmetrical and visually appealing layout.

In action, it looks like this:

For the sake of a direct comparison, here is the same block of text, but with textwrap: pretty toggled on and off:

Both balance and pretty are part of the evolving CSS Text Module and (as with many newer CSS features) browser support is currently ok, but still quite limited.


Practical Applications of textwrap: balance

In practical terms, textwrap: balance can dramatically improve the appearance of headlines and titles. I use it extensively across the titles you see here on my personal website in fact!

To offer another example, consider the product listing page on one of the big ecommerce websites I once worked on. Here is a row of products, with their titles rendered normally:

A screenshot of the Selfridges Product Listing Page showing three men's jackets. This demonstrates how the title text renders uncomfortably with very long lines of text and occasional widows on the last line.

You'll hopefully notice that some of the titles break unevenly across lines, and leave a visually jarring short line at the end. You also get these really long lines of text, sometimes followed by a single word, which makes it difficult for the user to read.

Now, here is the exact same screenshot, but this time I've added textwrap: balance to the titles:

A screenshot of the Selfridges Product Listing Page showing three men's jackets. This has text-wrap: balance applied to the titles, and shows that the titles are much neater and more evenly-balanced, without long lines of text or widows.

As you can see, the product items now look much cleaner; the titles do not run extremely long but instead, each line in the title is balanced against the other. Although this makes it more visually appealing, what it also does and perhaps more importantly it improves the user's experience of the site, making it easier to scan the product results and find the one they want to buy.

This becomes particularly helpful in responsive design where fluid layouts mean that any title could have an awkward break in it at any point. Using textwrap: balance in this way ensures that the text elements adapt and display harmoniously regardless of the screen sizes.


textwrap: balance vs. textwrap: pretty

Although similar, textwrap: pretty and textwrap: balance address different typographic challenges in web design.

textwrap: pretty focuses on eliminating orphans in longer text blocks, ensuring that paragraphs don't end with a single isolated word. On the other hand, textwrap: balance aims to evenly distribute text across each line in shorter elements, like headlines or titles, ensuring that lines are of similar length for a balanced appearance.

Whilst pretty enhances readability for larger pieces of text (like paragraphs), balance improves the visual harmony of shorter text elements.


Wrapping up

There isn't much more to say here aside from to share a little personal advice: I like to use pretty in my base stylesheet and apply it to every p, li, and blockquote. I also like to apply textwrap: balance to all my titles. Something like this:

p, li,blockquote {  text-wrap: pretty;}h1, h2, h3, h4, h5, h6 {  text-wrap: balance;}

With these few lines of CSS, you can very easily improve the way that text renders across your website or application. Assuming of course that your user's browsers support it at this early stage...


Categories:

  1. CSS
  2. Development
  3. Front‑End Development
  4. Guides
  5. Responsive Development
  6. Typography