Disable text wrapping inside an element using CSS

To disable text wrapping inside an element, use the CSS white-space property with the nowrap value. This forces all text content to remain on a single line, preventing it from wrapping to the next line even if it overflows the container.

Syntax

selector {
    white-space: nowrap;
}

Example: Preventing Text Wrapping

The following example demonstrates how to disable text wrapping using the white-space: nowrap property −

<!DOCTYPE html>
<html>
<head>
<style>
    .normal-text {
        width: 200px;
        border: 1px solid #ddd;
        padding: 10px;
        margin: 10px 0;
    }
    
    .no-wrap {
        width: 200px;
        border: 1px solid #ddd;
        padding: 10px;
        margin: 10px 0;
        white-space: nowrap;
        overflow: hidden;
    }
</style>
</head>
<body>
    <div class="normal-text">
        This is a long line of text that will wrap normally when it reaches the container boundary.
    </div>
    
    <div class="no-wrap">
        This is a long line of text that will not wrap and will stay on a single line.
    </div>
</body>
</html>
The first box shows text wrapping normally across multiple lines within the 200px width container. The second box shows the same text staying on a single line without wrapping, with overflow hidden.

Conclusion

The white-space: nowrap property is an effective way to keep text on a single line. Remember to handle potential overflow with overflow: hidden or text-overflow: ellipsis when using this property.

Updated on: 2026-03-15T12:10:23+05:30

663 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements