Exploring CSS text-wrap: balance, pretty, stable, wrap, and nowrap

Share this article:

CSS text-wrap: balance example

The CSS text-wrap property is a shorthand for text-wrap-mode and text-wrap-style. It controls how text wraps within its container, ensuring better readability and visual appeal. The property supports several values, including balance, pretty, stable, wrap, and nowrap, each offering unique behaviors for text wrapping.

One of the most exciting values is balance, which cleverly balances text across multiple lines, making it look more pleasing and professional. This is especially useful for headings and short paragraphs where uneven line lengths can be visually distracting.

Below, you can interact with a widget that demonstrates how different text-wrap settings behave. You can resize the div to see how the text adjusts in real-time. Additionally, you can change the default text to see how it affects the layout.

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

text-wrap: balance

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

text-wrap: pretty

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

text-wrap: stable

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

text-wrap: wrap

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

text-wrap: nowrap

Understanding text-wrap Values

The text-wrap property supports the following values:

  • balance: Attempts to balance the text across multiple lines, ensuring each line has a similar width. This is ideal for headings and short paragraphs, making text look more pleasing and professional.
  • pretty: Improves the appearance of text wrapping by avoiding awkward line breaks.
  • stable: Results in the same behavior as wrap, but with a key difference: when the user is editing the content, the lines above the edited line remain static. This prevents the entire block of text from re-wrapping, providing a more stable editing experience.
  • wrap: Allows text to wrap normally, breaking lines at appropriate spaces.
  • nowrap: Prevents text from wrapping, forcing it to stay on a single line.

Code Examples

Here are the CSS code snippets for each text-wrap value:

h2 {
    text-wrap: balance;
}
h2 {
    text-wrap: pretty;
}
h2 {
    text-wrap: stable;
}
h2 {
    text-wrap: wrap;
}
h2 {
    text-wrap: nowrap;
}
Takeaway: Use text-wrap: balance to create visually appealing and professional-looking text layouts, especially for headings and short paragraphs.

Benefits of Using text-wrap

  • Improved Readability: balance and pretty text wrapping make it easier for users to read and follow the content.
  • Visual Appeal: The pretty and balance values create aesthetically pleasing layouts.
  • Responsive Design: These properties work well with responsive designs, ensuring text looks good on all screen sizes.

Browser Compatibility

As of now, the text-wrap property is supported in most modern browsers, including Chrome, Firefox, and Edge. For the latest compatibility information, check Can I use.

Try yourself which wrapping method will work best for your project!