AGS Logo AGS Logo

Manipulating Text Layout
Using CSS text-wrap

Yellow and black dirds aligned on a clothesline

Photo by Ridham Nagralawala on Unsplash

Page and section headers are one of the first thing readers notice while looking at a web page. Often styled to be larger, bolder, and visually distinct from the rest of the content, they help us process and understand the content being presented.

Because they are front and center, taking care to style them in ways that are visually appealing is a common task performed by designers and developers. If online content was always presented in standardized window sizes, preventing the text from wrapping in undesirable ways would be easy. However, the reality is that we need to make our content work for an almost infinite number of viewport sizes.

Having reached baseline capability in March of 2024, we can now use the text-wrap property to affect how our text gets applied.

text-wrap: wrap

By default, the text-wrap property has a value of wrap which optimizes for as little overflow as possible. From a practical standpoint, the user agent puts as many words as it can on each line before moving onto the next as seen in figure 1.

A header that states 'A Picture is Worth 1000 Words' followed by paragraphs of Lorem Ipsum the first of which ends in orphan. The orphan is circled in yellow.
text-wrap: wrap

Because text-wrap: wrap is optimized for efficiency, it does not take into consideration the length of the paragraph which can lead to orphans. Because orphans can decrease the readability of large blocks of text, and in the context of headers can be unsightly, it is desirable to try and avoid them.

Let's look at the different values which can be assigned to text-wrap to see how they alter how our text is displayed.

text-wrap: nowrap

If we change the text-wrap property value applied to the paragraphs from its default value of wrap to a value of nowrap, we can prevent the lines from wrapping at all. In figure 2, we see the paragraphs now staying on their individual lines and overflowing their container outlined using yellow dots.

text-wrap: nowrap

Although not helpful for our current use case, preventing certain elements from wrapping can come in handy when showing only a small percentage of the text with some other way of accessing the full content such as in expandable panels.

In listing 1, we use text-wrap: nowrap in conjunction with the overflow and text-overflow properties to hide the overflowing text, add ellipses at the end of the line to show that there is more text available than what is being displayed, and achieve the panels found in figure 3.

CSS to visually break off text and replace it with ellipsis
section.closed p {
  text-wrap: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
text-wrap: nowrap with ellipses

Going back to our original layout, we still haven't addressed our orphan issue (figure 4).

A header that states 'A Picture is Worth 1000 Words' followed by 3 paragraphs of Lorem Ipsum the first of which ends in orphan. The orphan is circled in yellow.
Orphan at then end of of the first paragraph

text-wrap: balance

One of the recently added property values we can use to alter how text is painted is balance. Now available across all major browsers (figure 5) it tries to balance the number of characters found in each line.

Data on support for the css-text-wrap-balance feature across the major browsers from caniuse.com
caniuse data for text-wrap: balance

When giving our paragraphs' text-wrap property a value of balance and viewing the output in Chrome (figure 6), we notice a little bit of a difference in the third paragraph, but none on our first one, which is the one with the orphan.

First paragraph unchanged, while the third paragraph has balanced lines
Output in Chrome

Comparatively, when viewing our results in Firefox, we see that our first paragraph orphan is no longer present (figure 7).

First paragraph unchanged, while the third paragraph has balanced lines
Output in Firefox

Both Chrome and Firefox have text-wrap: balance implemented, however they differ in their application. For performance reasons browsers limit the number of lines they will balance characters over. Chrome's limit is 6 or less lines, while Firefox's is 10.

Due to this limitation, text-wrap: balance will not work for large bodies of text and is much better suited for headers or short content such as on the tile of the example shown in figure 8.

Same content shown twice. On the right, the title has a text-wrap property value of wrap. The title is: 'A Picture is Worth 1000 Words' and break after the 1000, leaving a single word on the second line. The left side shows the same title but with a text-wrap property value of balance. In this case the line break happens after 'worth' which prevents an orphan on the second line of the title.
wrap (left) versus balance (right) applied to the page's title

Since text-wrap: balance does not work well for large bodies of text, we need to look for a different solution for our content.

text-wrap: pretty

A better option for handling orphans in large bodies of text like our paragraphs is text-wrap: pretty. Unfortunately the property is currently (January 2025) not yet well supported (figure 9).

Table indicates support in Chrome, Edge, and Opera but not in Safari or Firefox
caniuse support table for text-wrap: pretty on January 23, 2025

Like wrap, when using a value of pretty, the algorithm used is optimized for minimizing overflow therefore putting as many words as it can on a line before moving onto the next. However, when using pretty the user agent prioritizes layout over speed which helps minimize the number of orphans.

Figure 10 shows our original layout in Chrome with text-wrap: pretty applied to the paragraphs and our first paragraph finally no longer ending in an orphaned word.

text-wrap: pretty

Now you know how to use text-wrap to create more aesthetic paragraphs and titles, reducing orphans, and improving readability without sacrificing on a fluid or responsive layout.

Happy Coding!

Custom Websites

Something all businesses and products have in common is the need for a website. Ideally one that is fast, reliable, accessible, and easy to update. Andromeda delievers using the same design expertise our customers have come to expect and combining this with our own Ignition™ Content Management System to produce blazing-fast static websites. Hire us to custom-design your new website and get the very best of design and technology to help your business grow with a top-quality online presence.

References

License: CC BY-NC-ND 4.0 (Creative Commons)