The Long-Awaited Arrival of Intelligent Text Wrapping
#Frontend

The Long-Awaited Arrival of Intelligent Text Wrapping

Tech Essays Reporter
5 min read

Safari's implementation of text-wrap: pretty finally brings modern browsers up to 15th-century typographic standards, though a subtle implementation quirk still mars the perfect paragraph.

For centuries, the art of beautiful typography remained largely unchanged. Johannes Gutenberg's printing press in the 15th century introduced sophisticated line-breaking techniques that balanced text across pages, creating the harmonious paragraphs we've come to expect in printed material. Yet somehow, in the digital age, we've been stuck with a far more primitive approach—until now.

In 2025, something truly monumental happened in the world of software development. Safari shipped a reasonable implementation of text-wrap: pretty, bringing modern browsers closer than ever to the cutting-edge XV-century technology that Gutenberg pioneered. WebKit's announcement marks a significant milestone in web typography, finally giving us the tools to create beautiful paragraphs online.

{{IMAGE:1}}

The naive approach to text layout has long been the standard: add words to a line until they no longer fit, then start a new line. This greedy algorithm, while simple, produces results that are rarely aesthetically pleasing. Sometimes it makes sense to squeeze in one more word or leave an extra space to create better balance across lines. Gutenberg understood this intuitively, manually adjusting his layouts to achieve visual harmony. In 1981, Donald Knuth and Michael Plass formalized this approach using dynamic programming, creating the sophisticated line-breaking algorithm that powers TeX and produces its famously beautiful typography.

For reasons that remain inexplicable, browsers clung to the naive greedy approach for decades, subjecting generations of web users to ugly typography. The problem is admittedly harder in a browser context than in print. With fixed page sizes, print layout can be computed once, offline. But on the web, window widths are arbitrary and even change dynamically, requiring "online" line-breaking that must adapt in real-time. On the other hand, 21st-century browsers have vastly more computing resources than we had in 1980 or even 1450!

Making lines approximately equal in terms of character count is only half the battle. No matter how carefully you break lines, they won't be exactly the same length. If you want both left and right edges of your text to align perfectly, you need to adjust the spaces between words—a process known as justification.

In CSS, text-wrap: pretty asks the browser to select line breaks intelligently to make lines roughly equal, while text-align: justify adjusts whitespace to make them exactly equal. Although Safari is the first browser to ship a non-joke implementation of text-wrap, combining these properties reveals a subtle but significant issue.

Justifying text-wrap: pretty

The problem manifests as whitespace between words being blown out of proportion. Here's the same justified paragraph with and without text-wrap: pretty:

When using the "smart" algorithm, an entire extra line might be added to achieve better balance, requiring all whitespace to be inflated proportionally. By itself, either p { text-wrap: pretty; } or p { text-align: justify; } looks alright. It's just the combination of the two that is broken.

This behavior stems naturally from the implementation approach. The dynamic programming scoring function aims to get each line close to the target width while being penalized for deviations. Crucially, the actual maximum width of a paragraph is fixed: while a line can be arbitrarily shorter, it cannot be any longer without overflowing.

For this reason, the dynamic programming sets the target width slightly narrower than the paragraph's maximum width. This allows lines to both under and overshoot the target, leading to better overall balance. As the original WebKit article explains: "The browser aims to wrap each line sooner than the maximum limit of the text box. It wraps within the range, definitely after the magenta line, and definitely before the red line."

But if you subsequently justify all the way to the red line, the systematic overshoot manifests as too-wide inter-word spacing. The browser's intelligent line-breaking, designed to create visual harmony, ends up creating awkward gaps when combined with justification.

Justifying text-wrap: pretty

This is a small wrinkle in what is otherwise a monumental improvement. WebKit developers deserve tremendous credit for shipping this feature ahead of every other browser vendor, bringing web typography into the modern era. The implementation represents years of careful work to solve a problem that has vexed digital typography since the web's inception.

The current limitation is particularly frustrating because it prevents designers from achieving the exact typographic control they've long sought. Many of us have been waiting for this feature specifically to create the kind of beautiful, justified paragraphs that have been standard in print for centuries. To finally have the tools, only to find them slightly broken in combination, is disappointing.

The good news is that this appears to be a solvable problem. The core text-wrap: pretty implementation works beautifully on its own. The justification algorithm works perfectly in isolation. The issue arises only when these two features interact, suggesting that a relatively targeted fix could resolve the problem without requiring a complete redesign.

For web developers and designers who care about typography, this development is both exciting and frustrating. We're tantalizingly close to having professional-grade typography on the web, but not quite there yet. The Safari team has demonstrated that implementing sophisticated line-breaking algorithms in a browser context is feasible, even with the constraints of dynamic layouts and limited computational resources.

As we look to the future, it's worth considering what other typographic features might follow. If browsers can finally implement intelligent line-breaking, perhaps we'll see proper hyphenation support, advanced ligature handling, or even more sophisticated justification algorithms that account for optical spacing rather than just character counts.

The journey from Gutenberg's manual adjustments to Knuth and Plass's dynamic programming to modern browser implementations spans over 500 years. We've gone from hand-set type to algorithms that can dynamically adjust layouts on devices ranging from watches to wall-sized displays. The fact that we're still refining these techniques in 2025 speaks to both the complexity of the problem and the enduring importance of good typography.

To the WebKit developers reading this: you've accomplished something remarkable in shipping text-wrap: pretty. The web typography community is grateful for your pioneering work. Now, if you could just fix this small wrinkle, we could finally create the beautiful, justified paragraphs we've been dreaming of for decades. The XV-century technology is within our grasp—we just need that final polish to make it perfect.

Justifying text-wrap: pretty

Comments

Loading comments...