Back to course

Lesson 35: Handling Overflowing Text (white-space, overflow)

CSS Mastery: From Zero to Hero in 100 Lessons

35. Handling Overflowing Text (white-space, overflow)

Sometimes text content exceeds the bounds of its container. CSS offers tools to manage this overflow gracefully.

1. Controlling Wrapping (white-space)

This property dictates how whitespace inside an element is handled and if content should wrap.

ValueDescription
normal (default)Whitespace collapses, and text wraps when necessary.
nowrapText will never wrap, causing it to overflow the container horizontally.
prePreserves both whitespace and line breaks (similar to the HTML <pre> tag).

css .no-wrap { white-space: nowrap; /* Forces content onto a single line */ }

2. Controlling Overflow (overflow)

When content (text or images) exceeds the element's box, the overflow property controls what happens.

ValueDescription
visible (default)Overflowing content is displayed outside the box.
hiddenOverflowing content is clipped and hidden.
scrollAlways adds scrollbars (horizontal and vertical), even if not needed.
autoAdds scrollbars only if the content overflows (best practice).

css .container-clip { height: 200px; overflow: hidden; /* Cuts off content that goes beyond 200px height */ }

.scroll-box { width: 300px; height: 150px; overflow: auto; /* Scrollbars appear if content needs them */ }

Note: You can control overflow directionally using overflow-x (horizontal) and overflow-y (vertical).