CSS Style wordWrap Property: CSS Word Wrap

June 19, 2025

CSS word-wrap Property: Controlling Word Breaking in CSS

The CSS word-wrap property (now standardized as overflow-wrap) is used to specify whether or not the browser may break lines within words in order to prevent overflow when an otherwise unbreakable string is too long to fit in its containing box. This property is essential for handling long words or URLs that can disrupt the layout of your web pages.

What is the word-wrap Property?

The word-wrap property allows you to control how words are broken when they exceed the boundaries of their container. This is particularly useful for dealing with long strings of text, such as URLs or code snippets, where natural breaks are not present. By default, browsers will not break words, leading to potential layout issues. word-wrap ensures that the text wraps to the next line, maintaining the integrity of your design.

Purpose of the word-wrap Property

The primary purpose of the word-wrap property is to:

  • Prevent text from overflowing its container.
  • Ensure long words or URLs are broken and wrapped to the next line.
  • Maintain the visual integrity and responsiveness of your web layout.
  • Improve the readability of text within fixed-width containers.

Syntax and Values

The word-wrap property accepts the following values:

/* Keyword values */
word-wrap: normal;
word-wrap: break-word;

/* Global values */
word-wrap: inherit;
word-wrap: initial;
word-wrap: revert;
word-wrap: revert-layer;
word-wrap: unset;

/* Standardized name */
overflow-wrap: normal;
overflow-wrap: break-word;
Value Description
`normal` Indicates that lines may only break at normal word break points. This is the default behavior.
`break-word` Indicates that if an otherwise unbreakable string is too long to fit in the line box, the browser may break it at an arbitrary point if there are no other otherwise acceptable break points in the line.
`inherit` Specifies that the `word-wrap` property should inherit its value from its parent element.
`initial` Sets the property to its default value (`normal`).
`revert` Reverts the property to the value established by the user-agent stylesheet (browser default).
`revert-layer` Reverts the property to the value established by the previous cascade layer.
`unset` Resets the property to its inherited value if it inherits from its parent or to its initial value if not.

Note: The standardized property name for word-wrap is overflow-wrap. While word-wrap is still widely supported, using overflow-wrap is recommended for future compatibility. 📝

Practical Examples

Let’s explore how to use the word-wrap property with practical examples.

Example 1: Preventing Overflow with break-word

This example demonstrates how word-wrap: break-word can prevent long URLs from overflowing their container.

<!DOCTYPE html>
<html>
  <head>
    <title>word-wrap Example 1</title>
    <style>
      .container_wrap1 {
        width: 200px;
        border: 1px solid #ccc;
        padding: 10px;
      }

      .break-word_wrap1 {
        word-wrap: break-word;
      }
    </style>
  </head>
  <body>
    <div class="container_wrap1">
      <p class="break-word_wrap1">
        This is a long URL:
        https://www.example.com/very/long/path/to/resource/that/exceeds/container/width
      </p>
    </div>
  </body>
</html>

In this example, the break-word class ensures that the long URL wraps within the container, preventing overflow.

Example 2: Using overflow-wrap for Standard Compliance

This example uses the standardized overflow-wrap property to achieve the same result as word-wrap.

<!DOCTYPE html>
<html>
  <head>
    <title>overflow-wrap Example</title>
    <style>
      .container_wrap2 {
        width: 200px;
        border: 1px solid #ccc;
        padding: 10px;
      }

      .break-word_wrap2 {
        overflow-wrap: break-word;
      }
    </style>
  </head>
  <body>
    <div class="container_wrap2">
      <p class="break-word_wrap2">
        This is a long URL:
        https://www.example.com/very/long/path/to/resource/that/exceeds/container/width
      </p>
    </div>
  </body>
</html>

This code snippet demonstrates that overflow-wrap: break-word functions identically to word-wrap: break-word.

Example 3: Handling Long Strings Without Spaces

This example shows how word-wrap handles long strings without spaces, such as a sequence of characters or a very long word.

<!DOCTYPE html>
<html>
  <head>
    <title>Long String Example</title>
    <style>
      .container_wrap3 {
        width: 200px;
        border: 1px solid #ccc;
        padding: 10px;
      }

      .break-word_wrap3 {
        word-wrap: break-word;
      }
    </style>
  </head>
  <body>
    <div class="container_wrap3">
      <p class="break-word_wrap3">
        This is a very long word without spaces:
        Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
      </p>
    </div>
  </body>
</html>

The break-word class ensures that the long string is broken and wrapped within the container, maintaining layout integrity.

Example 4: Using normal Value

This example demonstrates the default behavior of word-wrap: normal, where words are not broken unless there is a natural break point.

<!DOCTYPE html>
<html>
  <head>
    <title>Normal word-wrap Example</title>
    <style>
      .container_wrap4 {
        width: 200px;
        border: 1px solid #ccc;
        padding: 10px;
      }

      .normal_wrap4 {
        word-wrap: normal;
      }
    </style>
  </head>
  <body>
    <div class="container_wrap4">
      <p class="normal_wrap4">
        This is a long URL:
        https://www.example.com/very/long/path/to/resource/that/exceeds/container/width
      </p>
    </div>
  </body>
</html>

In this case, the URL will overflow the container because word-wrap is set to normal, and there are no natural break points within the URL.

Example 5: Inheriting word-wrap from Parent

This example shows how to inherit the word-wrap property from a parent element.

<!DOCTYPE html>
<html>
  <head>
    <title>Inherit word-wrap Example</title>
    <style>
      .parent_wrap5 {
        width: 200px;
        border: 1px solid #ccc;
        padding: 10px;
        word-wrap: break-word;
      }

      .child_wrap5 {
        word-wrap: inherit;
      }
    </style>
  </head>
  <body>
    <div class="parent_wrap5">
      <p class="child_wrap5">
        This is a long URL:
        https://www.example.com/very/long/path/to/resource/that/exceeds/container/width
      </p>
    </div>
  </body>
</html>

The .child class inherits the word-wrap: break-word property from its parent, ensuring that the long URL wraps within the container.

Real-World Applications

The word-wrap property is crucial in various real-world scenarios:

  • Content Management Systems (CMS): Ensuring that user-generated content, such as long URLs or code snippets, does not break the layout.
  • E-commerce Platforms: Handling product descriptions or titles that may contain long, unbroken strings.
  • Documentation Websites: Displaying code examples or technical terms that need to be wrapped correctly.
  • Social Media Platforms: Properly formatting long URLs or hashtags in user posts.

Browser Support

The word-wrap property is widely supported across modern web browsers. However, it’s recommended to use overflow-wrap for better standardization and future compatibility. 🚀

  • Chrome: Supported
  • Edge: Supported
  • Firefox: Supported
  • Safari: Supported
  • Opera: Supported
  • IE: Supported

Conclusion

The CSS word-wrap (or overflow-wrap) property is an essential tool for controlling how words are broken within a container, preventing overflow and maintaining layout integrity. By using word-wrap: break-word, you can ensure that long words or URLs are wrapped to the next line, improving the readability and visual appeal of your web pages. Remember to consider using overflow-wrap for better standardization and future compatibility. 💡