Real content rarely fits neatly. A product name is longer than its card, a user pastes a link with no spaces, or a description runs on for ten lines. CSS gives you control over what happens to content that does not fit: let it spill out, cut it off, add scrollbars, shorten it with “…”, or break long words. This lesson covers the overflow property and the text properties that decide how lines wrap.
In this lesson you will learn
- The four
overflowvalues and when to use each - Adding an ellipsis to one line with
text-overflow - Limiting text to a set number of lines with
line-clamp - Breaking long words and links with
overflow-wrap - Controlling spaces and line breaks with
white-space
You should know the box model and display types.
The overflow Property
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>overflow</title>
<style>
body { font-family: sans-serif; margin: 12px; display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; }
.wrap { width: 150px; }
.box { height: 60px; border: 2px solid #6a1b9a; padding: 4px; background: #f3e5f5; }
.v { overflow: visible; } /* default: content spills out */
.h { overflow: hidden; } /* extra content is cut off */
.s { overflow: scroll; } /* scrollbars always shown */
.a { overflow: auto; } /* scrollbars only when needed */
code { font-weight: bold; }
</style>
</head>
<body>
<div class="wrap"><code>visible</code><div class="box v">This text is much too long to fit inside a box that is only sixty pixels tall, so some of it has nowhere to go.</div></div>
<div class="wrap"><code>hidden</code><div class="box h">This text is much too long to fit inside a box that is only sixty pixels tall, so some of it has nowhere to go.</div></div>
<div class="wrap"><code>scroll</code><div class="box s">This text is much too long to fit inside a box that is only sixty pixels tall, so some of it has nowhere to go.</div></div>
<div class="wrap"><code>auto</code><div class="box a">This text is much too long to fit inside a box that is only sixty pixels tall, so some of it has nowhere to go.</div></div>
</body>
</html>
Result
- Each box has a fixed height of 60px, and the text needs more room than that.
visibleis the default: the text spills out of the box and may overlap whatever comes next.hiddencuts the content off at the edge of the padding box. Nothing can be scrolled, so use it only when the hidden part does not matter, for example for decorative shapes.scrollalways shows scrollbars, even when they are not needed. Depending on the system, the scrollbars may only appear while scrolling.autoadds scrollbars only when the content is too big. It is the best choice for scrolling areas such as a chat window or a wide table.overflow-xandoverflow-yset each direction separately.overflow-x: autoon a wrapper is the usual way to make a wide table scroll sideways on phones.
Ellipsis, Line Clamping and Wrapping
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Text overflow and wrapping</title>
<style>
body { font-family: sans-serif; margin: 12px; font-size: 14px; }
.card { width: 230px; border: 1px solid #b0bec5; padding: 6px 8px; margin-bottom: 10px; }
.one-line { /* single-line ellipsis needs all three */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.three-lines { /* multi-line clamp */
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
line-clamp: 3;
overflow: hidden;
margin: 0;
}
.url { overflow-wrap: anywhere; } /* break long words or links anywhere */
.pre { white-space: pre; font-family: monospace; }
</style>
</head>
<body>
<div class="card one-line">Introduction to Web Development with HTML, CSS and JavaScript</div>
<div class="card"><p class="three-lines">CSS controls how text behaves when it does not fit. This paragraph is clamped
to three lines, and the browser adds an ellipsis at the end of the third line so the card keeps
the same height as its neighbours, however long the description becomes.</p></div>
<div class="card url">https://example.com/learn/webdevelopment/averylongaddresswithoutanyspacesorhyphens</div>
<div class="card pre">Name Marks
Rina 92
Arif 85</div>
</body>
</html>
Result
- A single-line ellipsis needs three properties together:
white-space: nowrapkeeps the text on one line,overflow: hiddencuts off the rest, andtext-overflow: ellipsisshows “…” where it was cut. - For several lines,
-webkit-line-clamp: 3withdisplay: -webkit-boxand-webkit-box-orient: verticallimits the paragraph to three lines and ends the third with “…”. Despite the-webkit-prefix, this works in all current browsers. The unprefixedline-clampis listed for the future. - The clamp is applied to the paragraph, not the card. If the card’s own padding were part of the clamped box, a slice of the fourth line could show in the padding.
- Long words with no break points, such as web addresses, push past the edge by default.
overflow-wrap: anywherelets the browser break them at any character when there is no other choice. white-space: prekeeps every space and line break exactly as written in the HTML, like the<pre>element. By default, HTML collapses runs of spaces into one.
Text Wrapping Reference
| Property and value | Effect |
|---|---|
white-space: normal | Default: collapse spaces and wrap lines |
white-space: nowrap | Collapse spaces but never wrap |
white-space: pre | Keep spaces and line breaks, never wrap |
white-space: pre-wrap | Keep spaces and line breaks, wrap when needed |
overflow-wrap: anywhere | Break long words only if they would overflow |
word-break: break-all | Break any word at any character, even short ones |
hyphens: auto | Add hyphens at sensible points (needs a correct lang attribute) |
text-wrap: balance | Even out line lengths in short headings |
Common Mistakes
- Using
text-overflow: ellipsisalone. Withoutnowrapandoverflow: hiddenit does nothing. - Setting
overflow: hiddenon the page body to hide a horizontal scrollbar. This hides the real problem, an element that is too wide, and can cut off content. - Hiding text the reader needs. If information matters, give it room or link to the full version.
- Fixing heights on text boxes. Text grows when users zoom or translate the page; prefer
min-height.
Practice Exercises
Exercise 1. Make a code block scroll sideways instead of overflowing on small screens.
Show answer
pre {
overflow-x: auto;
white-space: pre;
}
Exercise 2. Limit every news summary inside .news p to two lines.
Show answer
.news p {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
line-clamp: 2;
overflow: hidden;
}
Frequently Asked Questions
Why does my page scroll sideways on a phone?
An element is wider than the screen. Common causes are fixed widths in pixels, large images without max-width: 100%, and long links without overflow-wrap: anywhere. Find the wide element instead of hiding the scrollbar.
Can screen readers read text hidden by an ellipsis?
Yes. The ellipsis only changes how the text is drawn; the full text is still in the page. Sighted users, however, cannot see it, so add a title or a link to the full text when the hidden part matters.
Summary
overflowchooses between spilling, cutting, or scrolling;autois usually best.- One-line ellipsis:
nowrap+overflow: hidden+text-overflow: ellipsis. line-clamplimits text to several lines.overflow-wrap: anywherestops long links from breaking layouts.
