Text Wrap in CSS: A Complete Guide

Master modern and traditional CSS text wrapping techniques including text-wrap: balance, text-wrap: pretty, overflow-wrap, and word-break for perfect layouts.

Understanding Text Wrapping in CSS

Text wrapping determines how text flows within its container and where line breaks occur. Proper text wrapping ensures content remains readable across different screen sizes and container widths. Without proper wrapping controls, text can overflow containers, create awkward line breaks, or break long words in unpredictable ways.

CSS provides several properties for controlling text wrapping, from the modern text-wrap shorthand to established properties like overflow-wrap and word-break. Understanding when to use each property helps create layouts that look polished on any device.

Common text wrapping challenges:

  • Long URLs breaking layout integrity
  • Headings with awkward single-word line breaks
  • Paragraphs with poor readability on narrow screens
  • Technical terms and international text

Our web development team applies these text wrapping techniques across all responsive websites we build, ensuring consistent typography across devices.

Modern CSS Text-Wrap Values

CSS Text Module Level 4 introduced powerful new values for intelligent text layout

text-wrap: balance

Distributes text across lines evenly, ideal for headings and short text blocks where visual harmony matters.

text-wrap: pretty

Optimizes body copy for readability by minimizing widows and orphans using a slower, quality-focused algorithm.

text-wrap: stable

Keeps lines static during editing in contenteditable areas, preventing confusing reflow while users type.

text-wrap: nowrap

Prevents all line breaks, useful for tabular data or navigation items that should remain on a single line.

text-wrap: balance

The balance value tells the browser to distribute text across lines as evenly as possible, making each line have a similar number of characters. This creates visually balanced text blocks that look more polished and intentional.

Best used for:

  • Headings (h1, h2, h3)
  • Blockquotes
  • Captions
  • Short text blocks (6 lines or fewer)
h1 {
 text-wrap: balance;
}

The browser considers the entire text block and finds line break positions that minimize the difference in line lengths. Performance is optimized by limiting the algorithm to short text blocks. Learn more about text-wrap values on MDN

text-wrap: pretty

The pretty value optimizes text wrapping for readability in longer passages such as paragraphs and articles. Unlike balance, which focuses on equal line lengths, pretty prioritizes avoiding typographic problems like widows (single words on their own line) and orphans.

Best used for:

  • Body copy
  • Articles and blog posts
  • Product descriptions
  • Multi-line text blocks
article p {
 text-wrap: pretty;
 hyphens: auto;
}

The browser uses a slower algorithm that considers factors like hyphenation opportunities and line length consistency. The performance cost is negligible for most web content. For comprehensive CSS text guidance, see MDN's text-wrap documentation.

The text-wrap shorthand combines both text-wrap-mode (controlling whether text wraps) and text-wrap-style (controlling the wrapping style). See our guide on text-wrap-style for details on the style component. When building websites with our responsive web design services, we apply text-wrap: pretty to ensure optimal readability across all device sizes.

Traditional Wrapping Properties

While text-wrap handles general text layout, properties like overflow-wrap and word-break remain crucial for handling long, unbreakable strings like URLs, technical terms, and international text.

overflow-wrap (word-wrap)

Controls whether the browser can break within words to prevent overflow. The break-word value causes a break only where overflow would occur, breaking words only when necessary.

.break-long-words {
 overflow-wrap: break-word;
}

word-break

Controls how text breaks at word boundaries. The break-all value breaks words at any character boundary, useful for CJK text or preventing any overflow.

.break-all-text {
 word-break: break-all;
}

white-space

Controls whitespace handling including whether text wraps. The pre-wrap value preserves whitespace but wraps at the container edge.

.pre-wrap-text {
 white-space: pre-wrap;
}

These properties work alongside modern text-wrap values to provide comprehensive text control. The text-wrap-mode property specifically controls whether text wraps at all. See our text-wrap-mode guide for detailed coverage. Our CSS architecture incorporates both approaches for maximum browser compatibility.

Hyphenation with CSS

CSS provides native hyphenation support through the hyphens property, which allows browsers to insert hyphens at appropriate break points within words.

.hyphenated-text {
 hyphens: auto;
 language: en;
}

Related properties:

  • hyphenate-character: Specify the hyphen character (default is the hyphen minus)
  • hyphenate-limit-chars: Set minimum word length and characters before/after hyphen
  • hyphenate-limit-lines: Control maximum number of consecutive hyphenated lines

Hyphenation creates more natural-looking line breaks, especially for justified text or narrow columns. See MDN's guide on wrapping and breaking text for detailed documentation.

Combining text-wrap: pretty with hyphens: auto provides the best results for body copy, as the pretty algorithm considers hyphenation opportunities when determining optimal line breaks.

The wbr Element

The <wbr> (word break opportunity) HTML element represents a position where the browser may break a line if necessary. Unlike <br> which forces a line break, <wbr> merely suggests a break point.

<p>
 https://www.example.com/very/long/path/to/resource/
 <wbr>
 that/needs/careful/handling
</p>

Use cases:

  • Long URLs and file paths
  • Compound technical terms
  • Snake_case or kebab-case identifiers
  • Any content where you want to indicate acceptable break points

The wbr element is particularly useful when working with technical documentation, code snippets, or any content containing long identifiers that need controlled breaking behavior.

Complete Text Wrapping Example
1/* Modern approach with fallbacks */2h1, h2, h3 {3 text-wrap: balance;4 overflow-wrap: break-word;5}6 7article p {8 text-wrap: pretty;9 hyphens: auto;10 language: en;11}12 13/* Content with long words or URLs */14.code-content, .url-content {15 overflow-wrap: break-word;16 word-break: break-all;17}18 19/* Editable content */20[contenteditable] {21 text-wrap: stable;22}23 24/* Feature query for older browsers */25@supports not (text-wrap: balance) {26 h1 {27 max-width: 20em;28 }29}

Frequently Asked Questions

Need Help with Your Web Development?

Our team builds custom websites with modern CSS techniques for optimal performance and user experience.

Sources

  1. MDN Web Docs - Wrapping and breaking text - Comprehensive guide covering overflow-wrap, word-break, hyphens, and wbr element
  2. MDN Web Docs - text-wrap property - Official CSS property reference
  3. W3C CSS Text Module Level 4 - W3C specification for text wrapping properties
  4. Chrome for Developers - CSS text-wrap: pretty - Chrome-specific implementation details