Free Developer Tool

CSS Line-Clamp Generator

Generate copy-paste CSS to truncate text to any number of lines with an ellipsis — with a live preview and an exact, Pretext.js-computed answer for whether your text actually overflows.

Live preview
Pretext measures multiline text entirely through arithmetic, so you can compute the exact truncation point before the browser paints a single frame.
Generated CSS
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
line-height: 24px;
max-height: 48px;
Full text lines
Fits fully
// what it does

Truncate Text to N Lines, the Reliable Way

Clamping text to a fixed number of lines keeps cards, list items, and previews tidy. The CSS line-clamp pattern handles the visual cut, but it leaves one question unanswered: will my text actually be truncated, or does it already fit? That depends on the font and the exact container width — and guessing wrong means either a missing ellipsis or a clipped word.

This CSS line clamp generator gives you both halves: the production CSS to paste, and a real measurement of how many lines your sample text wraps to at the chosen width, so you can see whether the clamp engages.

// the css

The Line-Clamp CSS Pattern

/* Clamp to 3 lines */
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;

/* Single line */
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;

The CSS line clamp generator above swaps between these two patterns automatically based on the line count you pick, and adds a matching max-height as a fallback.

// how line-clamp works

How CSS Line-Clamp Works

CSS line-clamp limits a block of text to a fixed number of lines and appends an ellipsis when the text is longer. The line-clamp effect is achieved with the legacy flexbox box model: display: -webkit-box plus -webkit-box-orient: vertical tells the browser to lay the text out as vertical lines, and -webkit-line-clamp caps how many of those lines are shown. The CSS line clamp generator above writes this block for you so you never have to remember the exact incantation.

For a single line you do not need line-clamp at all — the older text-overflow: ellipsis pattern is simpler. This CSS line clamp generator automatically switches to that single-line pattern when you set the clamp to one line, and uses the multi-line line-clamp block for two or more.

A standard, unprefixed line-clamp property is also shipping in browsers, but the prefixed line-clamp form remains the most compatible today, which is why this CSS line clamp generator outputs it.

// gotchas

Line-Clamp Gotchas to Avoid

Line-clamp has a few sharp edges. The element must remain a -webkit-box, so applying padding or changing the display type can break the line-clamp. The ellipsis only appears when the content genuinely overflows the clamped line count — if the text already fits, no ellipsis shows, which is why this CSS line clamp generator also tells you whether your sample text overflows.

Line-clamp also clamps purely visually: it does not expose, in JavaScript, where the cut happened. If you need the exact truncation point for a tooltip or a “read more” toggle, CSS line-clamp alone is not enough. Pair the line-clamp CSS from this generator with Pretext.js, which measures the real wrapped line boundaries so you know precisely where the line-clamp will cut. That combination — line-clamp for the paint, Pretext for the math — gives you a robust truncation system.

Finally, line-clamp respects the element's line height, so set a predictable line height when you copy the line-clamp CSS. The generator includes a matching max-height as a safety net so the line-clamp degrades gracefully in older engines.

// use cases

Where Line-Clamp Is Used

Card descriptions

Clamp product or article summaries to two or three lines so a grid of cards stays even.

Feed previews

Line-clamp post bodies in a timeline and reveal the full text on tap.

Notifications & toasts

Keep messages to a single clamped line with an ellipsis in tight UI.

Table cells

Apply line-clamp so long values do not blow out row heights.

// faq

Frequently Asked Questions

How do I truncate text to a number of lines in CSS?

Use the line-clamp pattern: display: -webkit-box; -webkit-line-clamp: N; -webkit-box-orient: vertical; overflow: hidden;. The browser shows N lines and appends an ellipsis. This generator outputs that block for the line count you choose and previews it live.

How do I truncate to a single line with an ellipsis?

For one line you do not need -webkit-line-clamp. Use white-space: nowrap; overflow: hidden; text-overflow: ellipsis;. Select 1 line in this tool and it generates exactly that.

Is -webkit-line-clamp supported in all browsers?

Yes — despite the -webkit- prefix, line-clamp is supported in all modern browsers (Chrome, Edge, Safari, Firefox). The standard line-clamp property is also shipping. The prefixed form remains the most compatible and is what this tool generates.

How do I know if my text will actually be truncated?

That depends on the real wrapped line count, which depends on the font and container width. This tool measures your sample text with Pretext.js and tells you how many lines it needs and whether it exceeds your clamp — so you know before shipping whether the ellipsis will appear.

Does line-clamp guarantee where the cut happens?

CSS clamps at the rendered line boundary but does not tell you the cut point in JavaScript. If you need the exact character where truncation lands (for tooltips or "read more" logic), measure with Pretext.js layoutWithLines() and read the line boundaries directly.

// summary

A CSS Line Clamp Generator That Measures, Too

Most of what a CSS line clamp generator does is print the boilerplate. This one goes further: it also measures your text, so you know whether the clamp will actually engage. Choose a line count, copy the CSS it produces, and preview the result live.

A measurement-aware CSS line clamp generator removes the guesswork from truncation. Bookmark it for cards, feeds, and tables, and pair it with Pretext.js when you need the exact cut point in code — the fastest way to ship clean, clamped text.

// more free tools

More Pretext Text Tools

All four tools are powered by Pretext.js, the open-source JavaScript text-measurement engine. Try the interactive playground to explore the full API.

CSS Line-Clamp Generator — Truncate Text with Ellipsis