Course contentsShow
← Web Development
Lesson 248 of 2,9824. CSS Layout FoundationsFree lesson

text-overflow and ellipsis for truncated text

Handle overflowing text gracefully with ellipsis and text-overflow properties.

What You'll Learn

You'll learn how to gracefully handle text that's too long to fit its container by adding an ellipsis (three dots) to indicate the text continues beyond what's visible.

The Problem: Text Spilling Out

Sometimes text is too long for its container—like a username in a narrow sidebar or a product title in a card. When combined with overflow: hidden, the text just gets cut off abruptly, which looks unfinished. Users can't tell if there's more content or if that's the complete text.

The text-overflow Property

The text-overflow property tells the browser how to display overflowing text. It has two main values:

  • clip (default): Text is simply cut off at the container edge
  • ellipsis: Text is cut off and "..." is added to show there's more

Important: text-overflow only works when three conditions are met:

  1. overflow: hidden (or scroll/auto)
  2. white-space: nowrap (prevents text from wrapping to multiple lines)
  3. A width constraint (explicit or implicit)

Typical Usage Pattern

.truncated-text {
  width: 200px;              /* constrain the width */
  white-space: nowrap;       /* force text to one line */
  overflow: hidden;          /* hide overflow */
  text-overflow: ellipsis;   /* show ... for hidden text */
}

Now a long string like "This is a very long username that won't fit" becomes "This is a very long use..."

Think of It Like a Window

Imagine looking through a narrow window at a long banner. overflow: hidden is like having curtains that hide the rest. text-overflow: ellipsis adds a visual hint—like a sign saying "more beyond this point."

Key Takeaway: Use text-overflow: ellipsis with overflow: hidden and white-space: nowrap to elegantly truncate single-line text with three dots, signaling to users that more content exists.