CSS White-Space: Complete Guide to Controlling Whitespace and Line Breaks

June 14, 2025

The CSS white-space property is a powerful tool that controls how browsers handle whitespace characters, line breaks, and text wrapping within elements. Understanding this property is essential for precise text formatting and creating professional-looking web layouts.

In this comprehensive guide, we’ll explore all aspects of the white-space property, from basic concepts to advanced techniques that will help you master text formatting in CSS.

What is CSS White-Space?

The white-space property determines how whitespace inside an element is handled by the browser. Whitespace includes spaces, tabs, line breaks, and other invisible characters that separate words and lines in your HTML content.

By default, browsers collapse multiple whitespace characters into a single space and wrap text automatically. The white-space property gives you control over this behavior, allowing you to preserve formatting exactly as written in your HTML or apply different wrapping rules.

CSS White-Space Values

The white-space property accepts several values, each with distinct behavior:

1. normal (Default)

This is the default behavior where sequences of whitespace are collapsed into a single space, and text wraps automatically to fit the container.

Example:

This text has multiple spaces and will wrap automatically.

CSS:

white-space: normal;

2. nowrap

Whitespace is collapsed like normal, but text will not wrap to new lines. The text will extend beyond the container if necessary.

Example:

This text will not wrap and will extend beyond the container width.

CSS:

white-space: nowrap;

3. pre

Whitespace is preserved exactly as written in the HTML. Text only wraps on explicit line breaks.

Example:

This text preserves
all spaces and
line breaks.

CSS:

white-space: pre;

4. pre-wrap

Whitespace is preserved, but text will wrap when it reaches the container edge or encounters a line break.

Example:

This text preserves spaces but wraps when needed.

CSS:

white-space: pre-wrap;

5. pre-line

Sequences of whitespace are collapsed, but line breaks are preserved and text wraps normally.

Example:

This text collapses spaces
but preserves
line breaks.

CSS:

white-space: pre-line;

6. break-spaces

Similar to pre-wrap, but allows breaking at any preserved whitespace characters.

Example:

This text can break at any preserved space character.

CSS:

white-space: break-spaces;

Interactive Comparison Tool

Use this interactive tool to see how different white-space values affect the same text:

Select White-Space Value:

Result:

This is sample text with multiple spaces.
It also has line breaks to demonstrate different behaviors.

Practical Use Cases

Code Formatting

When displaying code snippets, you want to preserve the exact formatting including spaces and line breaks:

function greet(name) {
    if (name) {
        console.log("Hello, " + name + "!");
    } else {
        console.log("Hello, World!");
    }
}

CSS:

white-space: pre;

Poetry and Formatted Text

For poetry or formatted text where line breaks matter but you want text to wrap responsively:

Roses are red,
Violets are blue,
CSS white-space
Helps format text too.

CSS:

white-space: pre-line;

Navigation Menus

Prevent menu items from wrapping to maintain layout integrity:

CSS:

white-space: nowrap;

Browser Compatibility

The white-space property has excellent browser support:

  • Chrome: Full support from version 1
  • Firefox: Full support from version 1
  • Safari: Full support from version 1
  • Edge: Full support from version 12
  • Internet Explorer: Full support from version 8

The newer break-spaces value has more limited support and was introduced in modern browsers around 2020.

Common Mistakes to Avoid

1. Overusing nowrap

Using white-space: nowrap without considering responsive design can cause horizontal scrolling on mobile devices. Always test on different screen sizes.

2. Forgetting about pre formatting

When using white-space: pre, remember that all whitespace in your HTML will be preserved, including indentation from your code formatting.

3. Not considering accessibility

Ensure that text remains readable and accessible when using different white-space values, especially on smaller screens.

Advanced Techniques

Combining with Text Overflow

Use white-space: nowrap with text-overflow: ellipsis to create elegant text truncation:

This is a very long text that will be truncated with ellipsis

CSS:

white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;

Responsive White-Space

Use media queries to adjust white-space behavior for different screen sizes:


@media (max-width: 768px) {
  .responsive-text {
    white-space: normal;
  }
}

@media (min-width: 769px) {
  .responsive-text {
    white-space: nowrap;
  }
}

Performance Considerations

The white-space property generally has minimal performance impact. However, keep these points in mind:

  • pre and pre-wrap: Can increase memory usage for very large text blocks
  • nowrap: May cause layout shifts if not properly contained
  • break-spaces: Newer value with slightly more complex processing

Conclusion

The CSS white-space property is essential for controlling text formatting and layout in web development. By understanding the different values and their behaviors, you can create more precise and professional-looking text layouts.

Key takeaways:

  • Use normal for standard text that should wrap naturally
  • Use nowrap for single-line text that shouldn’t break
  • Use pre for code or formatted text that needs exact preservation
  • Use pre-wrap when you need both preservation and responsive wrapping
  • Use pre-line for text that needs line breaks but not spaces preserved
  • Consider break-spaces for modern browsers when you need enhanced breaking behavior

Remember to always test your implementations across different devices and browsers to ensure the best user experience. The white-space property is a powerful tool that, when used correctly, can significantly improve the readability and visual appeal of your web content.