Question
How can I limit text to a specific number of lines using CSS and truncate the rest when it overflows vertically?
I know that:
text-overflow: ellipsis;
works for single-line text, but I want the same kind of behavior for multi-line text.
For example, given this text:
Ultrices natoque mus mattis, aliquam, cras in pellentesque
tincidunt elit purus lectus, vel ut aliquet, elementum nunc
nunc rhoncus placerat urna! Sit est sed! Ut penatibus turpis
mus tincidunt! Dapibus sed aenean, magna sagittis, lorem velit
I want the visible result to be limited to 2 lines, like this:
Ultrices natoque mus mattis, aliquam, cras in pellentesque
tincidunt elit purus lectus, vel ut aliquet, elementum...
Is this possible with CSS alone?
Short Answer
By the end of this page, you will understand how multi-line text truncation works in CSS, why text-overflow: ellipsis only handles one line by default, and how to use line clamping techniques such as -webkit-line-clamp to limit text to a fixed number of lines.
Concept
Single-line truncation and multi-line truncation are related, but they are not the same feature.
For one line, CSS truncation is straightforward:
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
This works because the browser keeps everything on one line and hides anything that does not fit horizontally.
For multiple lines, the browser needs a different rule: it must allow wrapping, count visible lines, and then stop rendering after a certain number of lines. That is where line clamping comes in.
A common CSS approach is:
.clamp {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
This tells the browser:
- lay out the text in a vertical box
- allow multiple lines
- show only a fixed number of lines
- hide the rest
Why this matters in real programming:
- card layouts often need titles and descriptions with equal height
- article previews need a clean summary length
- product grids should not break because one item has much longer text
- mobile layouts need predictable text blocks
Without line clamping, long content can stretch containers and make a UI look uneven.
Mental Model
Think of a text container like a small window.
- With single-line ellipsis, the window is only one row high.
- With multi-line clamping, the window is two or three rows high.
- Anything below that window is hidden.
So instead of saying, "cut text after this number of characters," CSS is really saying, "show only this much visible space." That is an important difference.
CSS usually does visual truncation, not text editing. The original text still exists in the HTML; the browser just stops showing part of it.
Syntax and Examples
The most common CSS solution for multi-line truncation is line clamping.
.description {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
Example HTML:
<p class="description">
Ultrices natoque mus mattis, aliquam, cras in pellentesque
tincidunt elit purus lectus, vel ut aliquet, elementum nunc
nunc rhoncus placerat urna! Sit est sed! Ut penatibus turpis
mus tincidunt! Dapibus sed aenean, magna sagittis, lorem velit
</p>
What each property does:
display: -webkit-box;enables an older flexible box layout mode used for this feature-webkit-box-orient: vertical;stacks content vertically-webkit-line-clamp: 2;limits visible text to 2 linesoverflow: hidden;hides extra text
A more complete example:
.card-text {
width: 300px;
font-size: 16px;
: ;
: -webkit-box;
-webkit-: vertical;
-webkit--clamp: ;
: hidden;
}
Step by Step Execution
Consider this example:
<p class="excerpt">
This is a long paragraph that should only appear across two lines in the layout, and the rest should be hidden from view.
</p>
.excerpt {
width: 220px;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
What happens step by step:
- The browser reads the paragraph text.
- Because the width is
220px, the text wraps into multiple lines. display: -webkit-boxchanges how the content is laid out for clamping.-webkit-box-orient: verticaltells the browser to stack lines vertically.-webkit-line-clamp: 2says only 2 lines may be shown.overflow: hiddenhides any content after the second line.
Without overflow: hidden, extra text may still be visible.
Without -webkit-line-clamp, the browser will not know how many lines to keep.
Real World Use Cases
Multi-line text truncation is common in many interfaces:
- Blog cards: show only 2 or 3 lines of summary text
- Product listings: prevent one long product description from stretching the layout
- News feeds: keep preview cards visually consistent
- Mobile apps and responsive pages: fit text into tight spaces
- Search results: display short snippets without showing full content
- Dashboards: keep widgets aligned even when content length varies
Example: an e-commerce card
.product-description {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
This keeps every product card looking the same height even if one description is much longer than the others.
Real Codebase Usage
In real projects, developers often combine line clamping with layout and content patterns.
Common patterns
- Card components: clamp titles or summaries so rows align neatly
- Responsive design: use different clamp values on different screen sizes
- Guarding layout stability: prevent user-generated content from breaking designs
- Progressive enhancement: use line clamp where supported, and allow normal wrapping or simple clipping as fallback
Example with responsive behavior:
.summary {
display: -webkit-box;
-webkit-box-orient: vertical;
overflow: hidden;
-webkit-line-clamp: 2;
}
@media (min-width: 768px) {
.summary {
-webkit-line-clamp: 3;
}
}
In component-based codebases
Developers often wrap this into reusable utility classes:
.line-clamp-2 {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
.line-clamp-3 {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit--clamp: ;
: hidden;
}
Common Mistakes
Here are some common problems beginners run into.
1. Expecting text-overflow: ellipsis to work on multiple lines by itself
Broken example:
.box {
text-overflow: ellipsis;
}
Why it fails:
text-overflowalone is not enough- it mainly works for single-line overflow unless combined with other constraints
2. Forgetting overflow: hidden
Broken example:
.box {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
Fix:
.box {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
3. Using white-space: nowrap for multi-line truncation
Broken example:
Comparisons
| Approach | Best for | Multi-line support | Ellipsis behavior | Notes |
|---|---|---|---|---|
text-overflow: ellipsis + nowrap | Single-line text | No | Yes | Standard one-line truncation |
-webkit-line-clamp | Multi-line text previews | Yes | Yes in supported browsers | Most common CSS-only solution |
max-height + overflow: hidden | Simple clipping fallback | Yes | Not true ellipsis | Less precise |
| JavaScript truncation | Exact text control |
Cheat Sheet
/* Single-line ellipsis */
.single-line {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
/* Multi-line clamp */
.multi-line {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
Quick rules:
text-overflow: ellipsisis mainly for one line- For multiple lines, use
-webkit-line-clamp - Always combine line clamp with
overflow: hidden - Make sure the container has a width or layout constraint so text can wrap
- CSS truncation is visual, not actual text removal
Fallback idea:
.fallback {
line-height: 1.5;
max-height: 3em;
overflow: hidden;
}
Good use cases:
- card titles
- article excerpts
- product descriptions
- dashboard summaries
FAQ
Can CSS truncate text after 2 lines?
Yes. The most common CSS approach is -webkit-line-clamp combined with display: -webkit-box, -webkit-box-orient: vertical, and overflow: hidden.
Why does text-overflow: ellipsis not work for multiple lines?
Because it is designed for single-line overflow behavior unless combined with line-clamping techniques. By itself, it does not count visible lines.
Is multi-line ellipsis supported in all browsers?
Support is generally good for modern browsers using the clamping approach, but behavior can vary. If support is critical, test in your target browsers and consider a fallback.
Can I clamp text to 3 or 4 lines instead of 2?
Yes. Just change the value:
-webkit-line-clamp: 3;
or
-webkit-line-clamp: 4;
Does CSS remove the hidden text from the HTML?
No. CSS only hides it visually. The original text remains in the document.
Should I use JavaScript instead of CSS for truncation?
Use CSS when you only need visual line limiting. Use JavaScript if you need exact control over characters, words, tooltips, or custom truncation rules.
Mini Project
Description
Build a simple article card that shows a title and a summary. The summary should be limited to 2 lines so every card stays the same height, even when the text is much longer.
Goal
Create a reusable text preview card with a 2-line clamped description using CSS.
Requirements
- Create a card container with a fixed width
- Add a heading and a paragraph of long text
- Limit the paragraph to exactly 2 visible lines
- Hide the rest of the overflowing text
- Keep the solution purely in HTML and CSS
Keep learning
Related questions
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
CSS :not() Selector for Elements Without a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common pitfalls.
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.