DEV Community

Cover image for Undoubtedly the best CSS Feature, Understanding CSS Text Balancing with "text-wrap-style" properties. 🚀
Aqil Marwan
Aqil Marwan

Posted on • Edited on

Undoubtedly the best CSS Feature, Understanding CSS Text Balancing with "text-wrap-style" properties. 🚀

Fundamentals of text-wrap-style. 📖

It's important to pay close attention to how text flows inside the layout while developing a website. The text-wrap property stands out among the other CSS properties for controlling text wrapping and line breaking because of its unusual methodology, especially when combined with text-wrap: balance. We'll examine the text-wrap attribute in this blog post, along with how it applies specifically to balancing.

Let's examine the text-wrap property from CSS Text Module Level 4, which was added in order to better understand the idea. This property is a useful tool for controlling the way text wraps inside the specified container. You can specify exactly how text breaks and wraps when it approaches the edges of its container by using the text-wrap property.


CSS Syntax 👨‍💻

text-wrap = 
  auto     |
  balance  |
  stable   |
  pretty   
Enter fullscreen mode Exit fullscreen mode

Comparison 🖥️

Observe how text-wrap-style line breaks and text layout differs across various properties in the following snippets; auto, balance, stable, pretty, and normal text accordingly.

T1
T2
T3
T4
T5


Applicability 🖥️

On top of that, here are the simplified illustration of text layout and practical use cases highlighting the suitability of text-wrap-style properties to accommodate various text appearance.
PS: Refer the snippets above ☝️

autowide | flowing
Use Case: In scenarios where automatic text wrapping is preferred.
Description: The auto value allows the browser to automatically determine the most appropriate text wrapping, ensuring readability without manual intervention.

balancenarrow | fair
Use Case: Ideal for achieving a balanced distribution of text across lines.
Description: By using balance, the aim is to evenly distribute text, preventing awkward line breaks and creating a visually symmetric layout.

stableunchanging | constant
Use Case: Suited for situations where stability in text layout is crucial.
Description: The stable value ensures a consistent text layout, beneficial when maintaining a fixed design structure is a priority, minimising text reflow.

prettyconical | aesthetic
Use Case: Best for enhancing the visual appeal of justified text.
Description: When aesthetics matter, pretty comes into play by optimising the presentation of justified text, providing a visually pleasing and polished appearance.


Browser Compatibility 🖱️

Before utilising any text-wrap: style properties, make sure your browser is compatible. It is rare to find support for the text-wrap: style function.
Read Morehttps://developer.mozilla.org/en-US/docs/Web/CSS/text-wrap#browser_compatibility


Common Issues ❌

1. Browser Support
The main support for this feature can only be found in browsers based on Chromium (such as Chrome and Edge) that uses vendor-specific prefixes (-webkit-text-wrap: balance).
Solution → To tackle this issue, it is essential to take into account alternative method when implementing this property.

2. Technical Constraints
Balancing text creates a challenge when used in conjunction with the white-space property, as one requests no wrapping while the other seeks balanced wrapping.
Solution → To address this, disable the settings for white-space property to allow for the reapplication of balanced wrapping.


Conclusion 📝

In conclusion, the CSS attribute text-wrap: balance proves to be a valuable asset along with other properties in your web design even with it's drawbacks, particularly when striving for a well-presented text arrangement. Implementation of text-wrap: balance guarantees a visually pleasing distribution of text, thereby elevating readability and augmenting the overall visual appeal of your web content. It is important to note that while this property contributes to aesthetic improvements, it is essential for you to confirm its compatibility with the web browsers used by your target audience to ensure a smooth and enjoyable user experience.


Illustrative Demo 💻

To enhance the comprehension of text-wrap-style properties, I have integrated a live working example that demonstrates the practicality and distinct text layout variances for each property. This illustration serves as a tool for grasping the applications and visual outcomes which shows how these properties can be effectively utilised in web development.
https://text-wrap-balance.vercel.app/


Resources 📜

https://developer.mozilla.org/en-US/docs/Web/CSS/text-wrap
https://drafts.csswg.org/css-text-4/#text-wrap-style
https://dev.to/kazz/boost-your-productivity-using-markdown-1be
https://caniuse.com/?search=text-wrap%3Abalance

Github Repository 👇

GitHub logo aqilmarwan / text-wrap-balance

CSS Text Balancing with text-wrap:balance

CSS Text Balancing with text-wrap: style

This project is a live working showcase of text-wrap: style properties that demonstrates the practicality and distinct text layout variances associated with each property; including auto, balance, stable and pretty.


Important Note: The flexible illustration of the text layout will only work on Chrome & Edge.


Built with Tailwind & Vercel.

Top comments (0)