Reading history

CSS Overflow, Text Ellipsis and Line Clamp

6 min read 1 views

Control content that does not fit: overflow hidden, scroll and auto, single-line ellipsis, multi-line line-clamp, overflow-wrap for long links and white-space.

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 overflow values 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.
  • visible is the default: the text spills out of the box and may overlap whatever comes next.
  • hidden cuts 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.
  • scroll always shows scrollbars, even when they are not needed. Depending on the system, the scrollbars may only appear while scrolling.
  • auto adds 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-x and overflow-y set each direction separately. overflow-x: auto on 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: nowrap keeps the text on one line, overflow: hidden cuts off the rest, and text-overflow: ellipsis shows “…” where it was cut.
  • For several lines, -webkit-line-clamp: 3 with display: -webkit-box and -webkit-box-orient: vertical limits the paragraph to three lines and ends the third with “…”. Despite the -webkit- prefix, this works in all current browsers. The unprefixed line-clamp is 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: anywhere lets the browser break them at any character when there is no other choice.
  • white-space: pre keeps 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 valueEffect
white-space: normalDefault: collapse spaces and wrap lines
white-space: nowrapCollapse spaces but never wrap
white-space: preKeep spaces and line breaks, never wrap
white-space: pre-wrapKeep spaces and line breaks, wrap when needed
overflow-wrap: anywhereBreak long words only if they would overflow
word-break: break-allBreak any word at any character, even short ones
hyphens: autoAdd hyphens at sensible points (needs a correct lang attribute)
text-wrap: balanceEven out line lengths in short headings

Common Mistakes

  1. Using text-overflow: ellipsis alone. Without nowrap and overflow: hidden it does nothing.
  2. Setting overflow: hidden on the page body to hide a horizontal scrollbar. This hides the real problem, an element that is too wide, and can cut off content.
  3. Hiding text the reader needs. If information matters, give it room or link to the full version.
  4. 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

  • overflow chooses between spilling, cutting, or scrolling; auto is usually best.
  • One-line ellipsis: nowrap + overflow: hidden + text-overflow: ellipsis.
  • line-clamp limits text to several lines.
  • overflow-wrap: anywhere stops long links from breaking layouts.

Next: How to Center Anything in CSS.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *