CSS line-clamp for Card Blurbs
Lesson 27 of 41 in the CSS3 learning path.
How do I cut a paragraph after two lines with an ellipsis?
line-clamp limits a block to a number of lines and hides the rest. The standard property is line-clamp. WebKit still wants the older trio in some engines.
Cards in a grid stay even when one blurb is a sentence and the next is a paragraph. The full text belongs on the lesson page.
Syntax & Parameters
.blurb { display: -webkit-box; -webkit-line-clamp: 2; overflow: hidden; }
Example
Interactive preview available.card {
width: 16rem;
font: 1rem/1.4 sans-serif;
border: 1px solid #e2e8f0;
border-radius: 12px;
padding: 1rem;
}
.card h2 {
margin: 0 0 .4rem;
font-size: 1.1rem;
}
.blurb {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
Expected Output
A card whose blurb stops after two lines. Extra sentences are not visible.
Line-by-Line Walkthrough
- The card has a fixed width.
- The blurb is the text that may be long.
- display -webkit-box is required for the older clamp.
- line-clamp 2 keeps two lines.
- box-orient vertical is part of that older pattern.
- overflow hidden clips the rest.
- The lesson page should show the full blurb.
- Do not clamp a heading that is the only name of the course.
Common Pitfalls & Security
Clamping hides text.
- Clamping the only copy of a warning hides the warning.
- Without overflow hidden the extra lines still show in some engines.
- Clamping a flex child can fail if min-height is auto. Set min-height: 0 if the clamp never clips.
- Users cannot select the hidden lines. Do not clamp text they must copy.
Best Practices & Modern Standards
Clamp teasers, not the canonical paragraph. Link the card to the full lesson.
Prefer the unprefixed line-clamp where you have checked support, and keep the WebKit trio until your browser set is ready.
Real-World Use
Catalog cards show two lines of each blurb so a long description does not stretch one card and wreck the row.
Frequently Asked Questions
Does this add an ellipsis?
Engines that implement the clamp draw an ellipsis when text is cut.
Can I clamp to one line?
Yes. Set the clamp to 1. A single-line title can also use nowrap and text-overflow.
Is the hidden text in the DOM?
Yes. It is still in the HTML. Do not put secrets in a clamped paragraph.
What if I need the full text?
Link to the lesson page. Do not rely on hover to reveal required content.