The Power of text‑wrap: pretty

Hero image for The Power of text‑wrap: pretty. Image by Jazmin Quaynor.
Hero image for 'The Power of text‑wrap: pretty.' Image by Jazmin Quaynor.

A few years back, I published an article 'Using JavaScript to Avoid Orphans' where I shared a solution for the common typographic challenge of preventing orphans (or 'runts') where single words fall onto the last time of a paragraph. At the time I had been working for a prestigious UK department store improving their customer experience, where one of the tasks presented had been to improve the way that text was displayed on their product listing pages, by removing errant runts from the product titles:

A screenshot from the Selfridges.com Product Listing Page, displaying men's trainers. Here, we demonstrate word wrapping to avoid orphaned words in the product titles by highlighting a single orphan in the title of one of the products.

At the time, the solution was simple: use JavaScript to wrap the final two words in a string in a <nobr> element (albeit a nonstandard HTML element) or otherwise in a <span> with the inline style whitespace: nowrap;.

I liked this feature so much that I used it extensively across the version of my personal website at the time.

However, times have changed. With recent additions to the textwrap property, and assuming that you are okay with a decent but improving level of browser support, then the need for scriptbased adjustments is redundant in the face of more modern CSScentric approaches.

textwrap: pretty represents a modern, elegant way to improve web typography, simplifying the process of ensuring cleaner, more visually consistent text blocks without runts or orphans.


A Quick Recap on Orphans in Typography

I've gone into detail on what orphans in typography are before but for the sake of a quick recap... Orphans, (sometimes referred to as 'runts') in typography, are single words that fall on their own line at the end of a paragraph, isolated on the last line.

I used the letter that Doc wrote to Marty in Back to the Future Part II previously to illustrate this, so it makes sense to use another excerpt from it to illustrate this phenomenon again now. Here, you can see that the last word ('machine') falls onto a line all on its own. I've highlighted it in red just to make it really obvious.

An excerpt from the letter Doc wrote to Marty in Back to the Future II, describing the location of the DeLorean in 1885. This demonstrates an orphan (or 'runt') word on its own on the final line - highlighted in red.

These lone words disrupt the visual harmony and readability of the text, creating an uneven and some feel an unprofessional look. In web design, avoiding orphans can be really difficult, especially when you have fluidwidth layouts to contend with. However, they can cause awkward gaps, draw attention away unnecessarily, and break the reader's flow, detracting from the overall aesthetic and content. Finding a way to ensure that the text we display onpage is visually appealing can be a quick way to improve a user's experience.


Understanding textwrap: pretty

textwrap: pretty is a CSS property which has been introduced as part of CSS Text Level 4 (albeit still currently a working draft), and is designed to enhance web typography by addressing the issue of orphans. Its primary function is to intelligently adjust line breaking within a text block, preventing these lone orphans, and thereby improving the visual consistency and readability of the text.

In action, it looks like this:

textwrap: pretty does also extend beyond merely preventing orphans in paragraphs, it also intelligently adjusts hyphenation, particularly when consecutive lines at the end of a paragraph are hyphenated. In tandem with removing orphans and adjusting text justification, it serves to generally improve linewrapping and textbreaking of a paragraph of text.

We are still in the early days of this type of more typographyfocused CSS capabilities; I expect that we will see further enhancements to textwrap: pretty in the future.


From JavaScript to CSS

Historically, managing orphans in typography required JavaScript to dynamically adjust text, which added complexity to the codebase and had a potential knockon effect to performance (although plenty of the time this transformation would occur at the SSR stage).

In contrast, textwrap: pretty offers a CSSbased solution that is simpler to implement and literally requires a single line of CSS code. Offsetting this type of calculation to the browser rather than attempting it clientside is inevitably more performant too and results in cleaner, more efficient code. With time, as browser uptake improves, it will also result in a more consistent appearance across different browsers and devices.


In Practice

To demonstrate textwrap: pretty in action, let's consider a realworld scenario: enhancing the readability of my blog posts. In my CSS, I simply apply the property to paragraph elements across the site:

p {  text-wrap: pretty;}

What this single line of code does is automatically adjust the text within my paragraphs to avoid orphans. Here's a sidebyside example of a couple of paragraphs of lorem ipsum, without textwrap: pretty on the left, with textwrap: pretty on the right:

Screenshot of three paragraphs of Lorem Ipsum text, side-by-side-, demonstrating the differences in rendering when using text-wrap: pretty.

As you can see, the difference is subtle but significant. The version with textwrap: pretty displays a more visually appealing layout, with no single words stranded at the bottom of paragraphs.

As textwrap: pretty enhances web typography by preventing orphans and improving the way text wraps in a paragraph, I should also mention it's cousin, the textwrap: balance property. balance aims to create a visually harmonious text block by adjusting the wrap to balance the length of lines, which is particularly effective for headlines and shorter pieces of text.

I go into much more detail about balance and compare it against pretty in my article 'Enhancing web typography with textwrap: balance'.

With all this said, it is important to be aware of the limitations here. As textwrap: pretty is part of an asofyetunfinished standard, it is likely to evolve and change as time goes on. Browser support is also generally ok but not amazing. I would personally argue that for something like this, it's a nice additional enhancement that improves user experience, but it's not entirely essential for the users to use and appreciate the content, so having some limitations in support this early in its release is expected, and that's okay.


The Future of Web Typography?

As I mentioned earlier, the introduction of new features to textwrap hints at an exciting future for web typography, where CSS continues to evolve to meet the complex needs of digital design.

This movement towards more sophisticated and typographyfocused CSS properties suggests a future where designers may have even greater control and flexibility to control text rendering directly within the CSS, helping to reduce reliance on JavaScript for visual refinements. As web standards evolve, we can expect to see more such properties that enhance readability and aesthetic appeal.

It's an exciting time for web developers and designers to experiment with these new capabilities, like textwrap: pretty, and provide feedback that could shape future CSS developments.


Categories:

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