CSS Word Wrap

The CSS word-wrap property is used to specify how long words should be handled when they overflow the container. This is especially useful when dealing with long strings of text or URLs that might otherwise cause layout issues. The property controls whether text should break and wrap to the next line or overflow beyond its container.

Key Points on CSS Word Wrap:

Syntax for CSS Word Wrap Property:

Syntax Example

/* Word wrap syntax */
            word-wrap: break-word;

Examples of CSS Word Wrap Property:

The following examples demonstrate the usage of the word-wrap property:

Code Example: Word Wrap with Break Word

div {
            word-wrap: break-word;
            width: 200px;
            border: 1px solid #ccc;
        }

Output

This is a long sentence that will break and wrap onto the next line if it exceeds the width of the container.

Code Example: Default Word Wrap

div {
            word-wrap: normal;
            width: 200px;
            border: 1px solid #ccc;
        }

Output

This is a long sentence that will overflow the container if it exceeds the width without wrapping.

Common Word Wrap Values:

When using word-wrap: break-word;, it’s important to test different screen sizes and content types, especially if you are dealing with long URLs or strings of text, to ensure the layout is clean and readable.