When A Line Doesn't Break: Mastering CSS Text Wrapping and Line Break Control

Learn how to control text wrapping and line breaks in CSS using white-space, overflow-wrap, word-break, and modern CSS properties for robust web layouts.

Text wrapping is fundamental to how content flows on the web, but there are countless scenarios where you need to prevent, control, or customize line breaks. Whether you're building navigation menus, displaying code snippets, handling long URLs, or creating print-style layouts, CSS provides a powerful arsenal of properties to control exactly how text behaves when it reaches the edge of its container. This guide explores the complete toolkit for managing line breaks in CSS, from the foundational white-space property to modern text wrapping controls used in our front-end development services.

The Foundation: Understanding Text Overflow

When text content exceeds the width of its container, browsers face a fundamental decision: should the text wrap to a new line, or should it extend beyond the container boundaries? By default, CSS wraps text at word boundaries, allowing content to flow naturally across multiple lines. However, this default behavior isn't always desirable. Long unbroken strings--such as URLs, code snippets, or technical terms--can overflow their containers and break layouts. Understanding how to control this behavior is essential for building professional, responsive web interfaces using our web development services.

The default overflow behavior in CSS is intentionally visible. When text cannot wrap and exceeds its container, the browser displays the overflow rather than hiding it. This "messy overflow" is actually a feature, not a bug--it ensures that important content remains visible even when styling issues occur. As noted in MDN documentation, messy overflow is at least noticeable, and in the worst case, visitors can still read the content even if it looks a bit strange. This philosophy prioritizes content accessibility over aesthetic perfection, but modern web development often requires more controlled approaches.

The min-content Keyword

When debugging text overflow issues, the min-content keyword provides a useful diagnostic tool:

.box {
 width: min-content;
}

This approach causes the container to shrink to fit the content, which can reveal whether overflow issues stem from container constraints or content characteristics. For production layouts, however, you'll typically want more explicit control over wrapping behavior.

The white-space Property: Your Primary Tool for Line Break Control

The white-space property is the cornerstone of CSS text control, governing how whitespace within an element is handled and whether text wraps or remains on a single line. This single property addresses both newline handling and text wrapping, giving you comprehensive control over how text behaves within its container. The property accepts five distinct values, each with different behavior for newlines, whitespace, and text wrapping. Understanding these values and their interactions is essential for implementing the exact text behavior your design requires.

white-space Value Comparison

ValueNew LinesSpaces/TabsText Wrapping
normalCollapseCollapseWrap
nowrapCollapseCollapseNo wrap
prePreservePreserveNo wrap
pre-wrapPreservePreserveWrap
pre-linePreserveCollapseWrap

white-space: normal (Default)

The normal value represents standard text behavior, where browsers collapse multiple spaces into one and wrap text at word boundaries to fit within containers. This is the default for all elements and is appropriate for most body content--paragraphs, articles, descriptions, and any text that should flow naturally across multiple lines.

.element {
 white-space: normal;
}

white-space: nowrap (Prevent All Wrapping)

The nowrap value prevents text from wrapping entirely, keeping all content on a single line regardless of container width. This is one of the most frequently used values for UI components that require consistent single-line presentation. This setting collapses whitespace normally (multiple spaces become one) but prevents any line breaks. Content extends beyond the container boundaries if necessary, which can cause horizontal scrolling or overflow issues if not managed properly. Common use cases include navigation menu items, table headers, buttons, tags, badges, and any UI element where text should remain on one line for visual consistency.

When using nowrap, always pair it with overflow handling to ensure content remains accessible:

.nowrap-with-ellipsis {
 white-space: nowrap;
 overflow: hidden;
 text-overflow: ellipsis;
}

white-space: pre (Preserve Formatting)

The pre value preserves exactly how text appears in the HTML source, including newlines, tabs, and multiple spaces. Text will not wrap until a line break exists in the source code. This behavior mirrors the <pre> HTML element and is essential for displaying code snippets where formatting matters.

white-space: pre-wrap (Preserve But Wrap)

The pre-wrap value offers a compromise between preserving formatting and preventing overflow. It preserves newlines and whitespace like pre, but also wraps text when it reaches the container boundary. This is useful when you want to preserve intentional formatting--poetry, addresses, or formatted text--while ensuring content remains contained within its layout.

white-space: pre-line (Preserve Lines, Collapse Spaces)

The pre-line value preserves newlines from the source but collapses consecutive whitespace like normal text. This creates behavior where line breaks are honored but extra spaces are removed. This setting is useful for poetry, lyrics, or any content where line breaks are semantically important but multiple spaces are not.

Handling Long Words: overflow-wrap and word-break

Even with white-space control, long unbroken strings like URLs, technical terms, or international words can cause overflow issues. Two CSS properties specifically address this challenge: overflow-wrap (formerly word-wrap) and word-break.

overflow-wrap: break-word

The overflow-wrap property determines whether the browser should insert line breaks within otherwise unbreakable strings to prevent overflow:

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

When set to break-word, the browser breaks long words only when necessary--when the word would otherwise overflow its container. If there's enough space for the word to fit on its own line, it won't be broken. This provides a balance between preventing overflow and maintaining readability.

word-break: break-all vs keep-all

The word-break property takes a more aggressive approach to word breaking, which is essential for website optimization. The break-all value breaks words at any character to prevent overflow:

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

This is useful for CJK (Chinese, Japanese, Korean) text or when you need to ensure no horizontal overflow under any circumstances. The keep-all value, by contrast, only breaks at language-appropriate boundaries, which is useful for languages where word separation differs from English.

Comparing the two approaches reveals their different behaviors. With overflow-wrap: break-word, a long word only breaks if it cannot fit on its current line. With word-break: break-all, the word breaks immediately when it reaches the container edge, even if breaking would create a very short first line.

The <wbr> Element: Manual Line Break Opportunities

For precise control over where text can break, the HTML <wbr> (word break opportunity) element allows you to indicate positions where the browser may insert a line break if necessary. Unlike <br>, which forces a line break, <wbr> creates a soft break opportunity.

<p>
 Llanfair<span class="break-opportunity"></span>pwllgwyngyll<span class="break-opportunity"></span>
 gogerychwyrndrobwllllantysiliogogogoch
</p>

This technique is particularly valuable for long URLs, where you might want breaks at logical points--after domain segments, before file extensions, or at path separators--while maintaining readability when the URL is fully visible.

.url-text {
 overflow-wrap: break-word;
}

Combined with overflow-wrap: break-word, the <wbr> element gives you fine-grained control over where breaks occur while letting the browser decide whether to break based on available space.

Controlled Hyphenation with the hyphens Property

The hyphens property controls whether hyphenation is allowed when words are broken across lines, improving readability for wrapped text while visually indicating where breaks occurred:

.hyphenated-text {
 hyphens: auto;
}

With hyphens: auto, the browser automatically inserts hyphens at appropriate breaking points according to the language of the content. For manual control, use hyphens: manual and insert soft hyphens (&shy; or &#173;) or hard hyphens (&#2010;) in your HTML:

<p>
 Llanfair&amp;shy;pwllgwyngyll&amp;shy;gogerychwyrndrobwllllantysiliogogogoch
</p>

Hard hyphens always appear, while soft hyphens only display when a break occurs at that position. Additional hyphenation control is available through the hyphenate-limit-chars property, which sets the minimum word length for hyphenation and the minimum characters before and after the hyphen.

Modern CSS: text-wrap-mode

CSS Text Module Level 4 introduced the text-wrap-mode property as a more explicit way to control wrapping mode. With Baseline 2024 support across major browsers, this property provides a clear, purpose-built approach to text wrapping control:

.single-line-text {
 text-wrap-mode: nowrap;
}

.wrapped-text {
 text-wrap-mode: wrap;
}

This property works alongside text-wrap-style (which controls wrap balancing) and can be combined into the text-wrap shorthand. The white-space property continues to support these behaviors through its longhand properties, ensuring backward compatibility while providing modern alternatives.

Code Examples and Practical Patterns

Navigation Menu Items

.nav-item {
 white-space: nowrap;
 overflow: hidden;
 text-overflow: ellipsis;
 max-width: 200px;
}

Code Display

Our front-end development services often require precise code formatting. Use these techniques for displaying code snippets:

.code-display {
 white-space: pre;
 font-family: 'Fira Code', 'Consolas', monospace;
 overflow-x: auto;
 background-color: #f5f5f5;
 padding: 1rem;
 border-radius: 4px;
}

Long URLs

For website optimization, handling long URLs that break layouts is crucial:

.long-url {
 overflow-wrap: break-word;
 word-break: break-all;
}

Tags and Badges

.tag {
 display: inline-block;
 white-space: nowrap;
 padding: 0.25rem 0.5rem;
 background-color: #e0e7ff;
 border-radius: 9999px;
 font-size: 0.875rem;
}

Multi-line Text with Preserved Breaks

.poetry {
 white-space: pre-wrap;
 font-style: italic;
 max-width: 60ch;
}

Performance Considerations

The text wrapping properties discussed in this guide--white-space, overflow-wrap, word-break, and hyphens--are CSS layout properties that the browser's rendering engine evaluates during the layout phase. In most cases, these properties have minimal performance impact comparable to other CSS layout properties.

However, some considerations apply:

  • Hyphenation (hyphens: auto) requires the browser to consult language dictionaries and calculate break points, which can have slight performance implications for large amounts of text
  • Complex wrapping scenarios with many long words may require additional layout passes
  • Combining multiple properties (e.g., white-space: nowrap with overflow-wrap: break-word) creates more complex evaluation logic

For optimal performance, apply text wrapping properties with appropriate specificity, use class-based selectors rather than inline styles where possible, and test with actual content to identify any performance issues in your specific use case.

Accessibility Considerations

When controlling text wrapping, accessibility should be a primary concern:

Testing with Real Content

Test with actual content lengths, including edge cases--very short text, very long text, and text with special characters. What works for sample text may fail with real user content.

Respecting User Zoom

When using white-space: nowrap or text-overflow: ellipsis, test with text zoomed up to 200% to ensure content remains accessible. Some users with visual impairments rely on zoom functionality, and aggressive wrapping prevention can create issues.

Overflow Handling

Ensure that overflow handling--whether ellipsis or scrolling--maintains content accessibility. Ellipsis truncates content, which may hide important information. Scrolling requires users to interact to see all content, which may not be obvious.

Color and Contrast

When overflow causes background color changes, verify that color contrast remains sufficient for readability.

Best Practices Summary

  1. Use white-space: nowrap sparingly--only when single-line presentation is truly required for UI consistency

  2. Always pair nowrap with overflow handling (text-overflow: ellipsis or overflow: auto) to ensure content accessibility

  3. Prefer overflow-wrap: break-word over word-break: break-all for most long-word scenarios, as it only breaks when necessary

  4. Use white-space: pre or pre-wrap for code and formatted content where whitespace matters

  5. Consider hyphens: auto for long-form content in languages that support it, improving readability

  6. Test across viewport sizes to ensure text wrapping behaves as expected on all devices

  7. Use the <wbr> element for fine-grained control over break points in long strings like URLs

Frequently Asked Questions

Build Professional Websites with Modern CSS

Our expert developers use the latest CSS techniques to create responsive, accessible, and performant [web applications](/services/web-application-development/). Contact us to discuss your project.