AI CSS Line Clamp — Truncate Text with Responsive Multi-Line Ellipsis
Card grids, article previews, product descriptions, notification panels — they all share the same problem: variable-length text that needs to fit a fixed-height container. Single-line truncation with text-overflow: ellipsis has been around forever, but real content rarely fits on one line. You need multi-line truncation, and that is where CSS line clamp comes in.
An AI CSS line-clamp generator lets you visually configure how many lines to show, preview the ellipsis behavior with real content, and export production-ready CSS that works across all modern browsers.
The Line Clamp Property Explained
CSS line clamp limits a block of text to a specific number of lines and adds an ellipsis at the truncation point. The implementation uses a combination of properties that work together:
/* Truncate text to 3 lines with ellipsis */
.card-description {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
/* Modern shorthand (emerging standard) */
.card-description-modern {
line-clamp: 3;
overflow: hidden;
}
The -webkit-line-clamp property has an unusual history. It started as a WebKit-only feature, but every major browser now supports it. The -webkit- prefix is still required in production because the unprefixed line-clamp specification is still being finalized. The three properties — display: -webkit-box, -webkit-box-orient: vertical, and -webkit-line-clamp — must all be present for the truncation to work.
Single-Line vs Multi-Line Truncation
Before line clamp existed, developers only had single-line truncation available through a well-known CSS pattern:
/* Single-line truncation */
.single-line {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
/* Multi-line truncation with line clamp */
.multi-line {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
Single-line truncation forces all text onto one line with white-space: nowrap, then hides the overflow and shows an ellipsis. It works perfectly for table cells, navigation labels, and breadcrumbs. But for card descriptions, article excerpts, and comment previews, you need the text to wrap naturally across multiple lines before truncating. That is exactly what line clamp provides.
Responsive Line Clamping
A fixed line count does not always work across screen sizes. A three-line description that looks perfect on desktop might need only two lines on mobile to maintain card proportions. Use media queries or container queries to adjust the clamp value:
/* Responsive line clamp with media queries */
.article-excerpt {
display: -webkit-box;
-webkit-box-orient: vertical;
overflow: hidden;
-webkit-line-clamp: 4;
}
@media (max-width: 768px) {
.article-excerpt {
-webkit-line-clamp: 2;
}
}
/* Container query approach */
@container card (max-width: 300px) {
.article-excerpt {
-webkit-line-clamp: 2;
}
}
Container queries are particularly powerful here because the line count should depend on the card width, not the viewport width. A narrow sidebar card needs fewer lines than a wide featured card, regardless of screen size. For more on responsive layout techniques, see our CSS media query breakpoints guide.
min-height on the text container to prevent layout shifts when some cards have short text that does not reach the clamp limit. This keeps your card grid visually consistent even when content lengths vary dramatically.Card Layout Patterns with Line Clamp
The most common use case for line clamp is card-based layouts. Here is a complete card component that handles text truncation gracefully:
.card {
display: flex;
flex-direction: column;
border-radius: 12px;
overflow: hidden;
background: var(--card);
}
.card-body {
padding: 16px;
flex: 1;
display: flex;
flex-direction: column;
}
.card-title {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
font-weight: 700;
margin-bottom: 8px;
}
.card-description {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
color: var(--muted);
flex: 1;
}
.card-footer {
padding: 12px 16px;
border-top: 1px solid var(--border);
margin-top: auto;
}
This pattern clamps both the title (2 lines) and description (3 lines), ensuring every card in a grid has consistent height. The flex: 1 on the description and margin-top: auto on the footer push the footer to the bottom even when the description is shorter than the clamp limit. For building these layouts visually, try our AI Flexbox Generator.
Accessibility Considerations
Truncated text creates an accessibility challenge: screen readers may read the full text while sighted users only see the truncated version, or worse, the truncated text might cut off critical information. Handle this properly:
<!-- Accessible truncated text -->
<p class="card-description">
This is a long description that will be
visually truncated after three lines...
</p>
<button class="expand-btn" aria-expanded="false">
Read more
</button>
/* Toggle expansion */
.card-description.expanded {
-webkit-line-clamp: unset;
}
/* Hide expand button when text is short */
.card-description:not(.is-truncated) + .expand-btn {
display: none;
}
Always provide a way to access the full text. A “Read more” button, an expandable section, or a link to the full content page ensures that no information is permanently hidden. Use JavaScript to detect whether the text is actually truncated (compare scrollHeight to clientHeight) and only show the expand button when needed.
Detecting Truncation with JavaScript
You cannot tell from CSS alone whether text has been truncated. A small JavaScript check lets you conditionally show expand controls:
function isTruncated(element) {
return element.scrollHeight > element.clientHeight;
}
document.querySelectorAll('.card-description').forEach(el => {
if (isTruncated(el)) {
el.classList.add('is-truncated');
}
});
Line Clamp with Different Content Types
Line clamp works with plain text, but be careful with rich content. HTML elements inside a clamped container can cause unexpected behavior:
- Inline elements like
<strong>,<em>, and<a>work fine inside clamped text - Block elements like
<p>,<div>, and<ul>break the line clamp behavior entirely - Images and other replaced elements inside clamped containers produce unpredictable results
- Line breaks (
<br>) count as line endings and affect the clamp count
For rich content truncation, strip the HTML to plain text before applying line clamp, or use a JavaScript-based solution that can handle nested elements. The CSS-only approach works best with simple text content. For handling text that includes different languages and scripts, check our CSS word-break guide and CSS hyphens guide.
Combining Line Clamp with Other Text Properties
Line clamp works well alongside other text styling properties. Here are combinations that produce polished results:
/* Elegant truncated text */
.polished-excerpt {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
/* Typography refinements */
font-size: 0.95rem;
line-height: 1.6;
color: var(--muted);
hyphens: auto;
word-break: break-word;
text-align: justify;
}
/* Fade effect instead of hard ellipsis */
.fade-truncate {
position: relative;
max-height: calc(1.6em * 3);
overflow: hidden;
}
.fade-truncate::after {
content: '';
position: absolute;
bottom: 0;
right: 0;
width: 40%;
height: 1.6em;
background: linear-gradient(to right, transparent, var(--bg));
}
The fade effect is an alternative to the ellipsis that some designers prefer. Instead of a hard “...” cutoff, the text fades out at the bottom. This does not use line clamp at all — it relies on a fixed max-height calculated from the line height and desired line count. For more text styling techniques, explore our CSS text shadow guide.
Preview multi-line text truncation with real content, adjust line counts for different breakpoints, and export clean CSS with all required vendor prefixes.
Try AI CSS Line Clamp Generator →
The AI CSS Line Clamp Generator lets you experiment with different line counts, preview truncation behavior with your own content, and generate production-ready CSS that includes all the required properties and vendor prefixes for cross-browser compatibility.