Boneyard Tools

The -webkit-line-clamp technique explained

How the four line-clamp declarations cooperate, when to use the one-line alternative, and how to avoid the common reasons text fails to truncate.

Four declarations working as a set

Multi-line truncation is not a single property but a small recipe of four rules that only work together. display: -webkit-box switches the container into the old flexible box layout that line clamping is built on. -webkit-box-orient: vertical stacks the lines top to bottom so a line count makes sense. -webkit-line-clamp sets how many lines survive, and overflow: hidden clips what falls past them. Drop any one of these and the effect breaks, which is why the generator always emits the full block.

Why the one-line case is different

Clamping to a single line is a special situation where the box model is overkill. The classic trio of white-space: nowrap, overflow: hidden and text-overflow: ellipsis truncates one line more simply and works everywhere without the -webkit- box layout. Because that approach is often the better fit, the tool appends it as a commented alternative whenever you set the count to one, letting you choose between the box method and the plain one-line method.

Common reasons truncation fails

When clamped text refuses to cut off, the culprit is usually structural rather than the properties themselves. Applying the rule to a wrapper while the real text sits in a nested element can defeat the box formatting, so target the element that directly contains the words. A container wide enough to fit everything on fewer lines than the clamp value will never truncate, since there is nothing to hide. Overriding overflow, changing display on a child, or adding padding that expands the box can all quietly cancel the effect.

Where line clamping fits best

This pattern shines in dense interfaces where text length is unpredictable but layout must stay tidy. Product cards, article teasers, comment previews and table cells all benefit from a firm two or three line cap that keeps rows aligned. Because clamping responds to the actual rendered width, the same rule adapts as the viewport narrows, trimming more aggressively on small screens. For anything longer than a short preview, pair the clamp with a Read more control so users can still reach the hidden content.

Frequently asked questions

Can I animate a line clamp open on hover?

Not smoothly through line-clamp alone, because the property is not animatable. A common workaround is to toggle between a clamped value and a large one, or swap the class entirely, which changes the height in a single step rather than a fluid transition.

Does line clamp work on inline elements?

No. The rule needs a block or inline-block box that becomes the -webkit-box, so apply it to a paragraph, div or similar container. A purely inline span will not clamp until it is given block-level display.