Ronalds Vilciņš

CSS text-wrap property

We often encounter situations where text refuses to stay within the bounds of its container. Long, unbroken words or strings without spaces can overflow, causing design problems and affecting readability. This is where the versatile CSS text-wrap property comes to the rescue.

What is text-wrap?

The text-wrap property provides control over how text is allowed to break within an element to fit its container. Let’s explore all its possible values:

  • normal: The default behavior. Text will wrap where spaces and normal word breaks occur.
  • nowrap: Text will never wrap, potentially extending outside the container if a single word is too long.
  • break-word: Allows long words or strings of text to be broken at arbitrary points if necessary to prevent them from overflowing the container.
  • balance (Experimental): This value attempts to achieve more visually balanced line lengths by breaking words in the middle if needed. Browser support and performance can be inconsistent.
  • pretty (Experimental): Prioritizes optimal word wrapping for readability, potentially breaking words mid-way. May have slower performance than normal.
  • stable (Experimental): A more conservative word-wrapping approach, avoiding breaks in undesirable places, while prioritizing performance.

Examples in action

Let’s see how these values work in practice:

HTML:

<div class="container">
  <p class="normal">This is a paragraph with normal text-wrap behavior.</p>
  <p class="nowrap">Thisisaverylongwordwithoutspaces.</p>
  <p class="break-word">Anotherverylongworddemonstratingwordbreaking.</p>
</div>

CSS:

.container {
  width: 250px;
  border: 1px solid black;
}

.normal {
  text-wrap: normal;
}

.nowrap {
  text-wrap: nowrap;
}

.break-word {
  text-wrap: break-word;
}
  • We define a container with a fixed width to illustrate the text-wrapping behaviors.
  • The paragraphs demonstrate how the normal, nowrap, and break-word values of text-wrap impact the handling of long words or unbroken text.

Common use cases

  • Narrow Columns: Maintain readability in columns with limited space by using break-word to prevent long words from disrupting the layout.
  • Preventing Overflow in Fixed Containers: Use normal or break-word to ensure that text stays within containers with fixed dimensions.
  • User Input Fields: If you expect long, unbroken input from the user, consider nowrap to keep the input on a single line for easy visual scanning.

Key considerations

  • Browser Support: Be aware of potential inconsistencies in support and performance for the ‘balance’, ‘pretty’, and ‘stable’ values across different browsers.
  • Legacy Considerations: For broader compatibility, consider also using word-wrap (from CSS3) or overflow-wrap.
  • Hyphenation: For even more refined line-breaking control, explore the hyphens property in combination with text-wrap.

The CSS text-wrap property is great for managing text overflow situations. Understanding its different values empowers you to create cleaner, more polished, and user-friendly web designs.