Using -webkit-line-clamp to truncate multiline text is incompatible with multiparagraph text

The fundamental problem: “Leakage” of post-ellipsis text

Eventually, the CSS property line-clamp will allow you to use CSS to truncate text after a specified number of lines. But currently (e.g., as of 8/16/2019), line-clamp is just a figment of some working group’s fevered imagination.

Until then, there’s the -webkit-line-clamp property, which does the job OK-ish, but with some quirks. It has good browser support.

It’s well known that care must be taken lest undesired text, that should be truncated and thus invisible, leaks out below the ellipsis into visibility.

Well known: Don’t include bottom padding on text to be truncated

For example, it’s well known that having bottom padding on the text to be truncated can cause such leakage. Per Šime Vidas:

Make sure that the element doesn’t have (bottom) padding, to prevent any out-of-bounds text lines from being rendered. If spacing is required around the truncated text, apply the padding to the parent element instead.

Not so well known: Wrapping the text to be truncated in paragraph tags can cause leakage

When the -webkit-line-clamp property is applied to a container of the p element

I didn’t anticipate the problem I ran into: I had wrapped text in a set of <p> tags—because that’s the semantic kind of guy I am—and text that should have truncated/invisible leaked out after the ellipsis. See Case 1.

Case 1: Here’s text to truncate that is wrapped in a set of <p> tags

Here's a bunch of text that, internally, is wrapped in a set of <p> tags. As you’ll probably detect visually, there is undesirable “leakage” of the text to be truncated below the ellipsis. I’ll keep typing so that there’s enough text to translate. (I can’t afford lipsum ipsum, so I have to roll my own!) I hope all this typing doesn’t give me a repetitive-motion injury. I must keep going to make sure that there’s not only enough text to trigger the ellipsis but also enough text to leak out beyond the ellipsis.

Removing the enclosing p tags fixes this: see Case 2.

Case 2: Here’s text to truncate that is not wrapped in a set of <p> tags

Here's a bunch of text that, internally, is not wrapped in a set of <p> tags. As you’ll probably detect visually, there is not undesirable “leakage” of the text to be truncated below the ellipsis. I’ll keep typing so that there’s enough text to translate. (I can’t afford lipsum ipsum, so I have to roll my own!) I hope all this typing doesn’t give me a repetitive-motion injury. I must keep going to make sure that there’s not only enough text to trigger the ellipsis but also enough text to possibly leak out beyond the ellipsis.

First, let me be perfectly clear: the -webkit-line-clamp property was set on a container that held the p element that held the text; the -webkit-line-clamp property was not set on the p element itself.

Applying the -webkit-line-clamp property directly to the p element eliminates the leakage

The leakage in Case 1 can be eliminated by applying the -webkit-line-clamp property directly to the p element rather than to the p element’s container. (See Case 3.)

Case 3: Here’s text to truncate that is wrapped in a set of <p> tags, but the -webkit-line-clamp property is set on the p element directly

Here's a bunch of text that, internally, is wrapped in a set of <p> tags. However, unlike the earlier examples, the -webkit-line-clamp property is applied to the p element itself, rather than to its container. As you’ll probably detect visually, there is not undesirable “leakage” of the text to be truncated below the ellipsis. I’ll keep typing so that there’s enough text to translate. (I can’t afford lipsum ipsum, so I have to roll my own!) I hope all this typing doesn’t give me a repetitive-motion injury. I must keep going to make sure that there’s not only enough text to trigger the ellipsis but also enough text to possibly leak out beyond the ellipsis.

What if you want to truncate a block of multi-paragraph text?

Suppose you have a block of multi-paragraph text and you’d like to truncate that block of text if it runs over n lines, say for n=10?

Applying -webkit-line-clamp to each paragraph separately doesn’t truncate the block of multiparagraph text; it truncates each paragraph independently

If (a) you wrap each semantic paragraph in a set of p tags and (b) apply the -webkit-line-clamp property directly to the p elements, you have Case 4. There is no undesirable leakage of text. However, this is the same as truncating each paragraph independently based on the number of lines of text for each paragraph separately, not based on the total number of lines of the block of multi-paragraph text.

Case 4: Can you truncate multi-paragraph text after a specified number of lines using -webkit-line-clamp? (Applying -webkit-line-clamp to each paragraph separately)

Here's a first paragraph of two paragraphs of multi-paragraph text. Internally, each paragraph of text is wrapped in a set of <p> tags. the -webkit-line-clamp property is applied to the p element itself, rather than to its container. As you’ll probably detect visually, there is not undesirable “leakage” of the text to be truncated below the ellipsis. I’ll keep typing so that there’s enough text to translate. (I can’t afford lipsum ipsum, so I have to roll my own!) I hope all this typing doesn’t give me a repetitive-motion injury. I must keep going to make sure that there’s not only enough text to trigger the ellipsis but also enough text to possibly leak out beyond the ellipsis.

Here's a second paragraph of two paragraphs of multi-paragraph text. Internally, each paragraph of text is wrapped in a set of <p> tags. the -webkit-line-clamp property is applied to the p element itself, rather than to its container. As you’ll probably detect visually, there is not undesirable “leakage” of the text to be truncated below the ellipsis. I’ll keep typing so that there’s enough text to translate. (I can’t afford lipsum ipsum, so I have to roll my own!) I hope all this typing doesn’t give me a repetitive-motion injury. I must keep going to make sure that there’s not only enough text to trigger the ellipsis but also enough text to possibly leak out beyond the ellipsis.

Applying -webkit-line-clamp to the container can produce a big mess

So what if, instead, you apply the -webkit-line-clamp property to the container of the multiple paragraphs, rather than to each p element separately? This can be a huge mess! 😱 (See Case 5, which shows that the leakage from the first paragraph overlays the text of the second page.)

Case 5: Can you truncate multi-paragraph text after a specified number of lines using -webkit-line-clamp? (Applying -webkit-line-clamp to the container)

Here's a first paragraph of two paragraphs of multi-paragraph text. Internally, each paragraph of text is wrapped in a set of <p> tags. the -webkit-line-clamp property is applied to the p element itself, rather than to its container. As you’ll probably detect visually, there is not undesirable “leakage” of the text to be truncated below the ellipsis. I’ll keep typing so that there’s enough text to translate. (I can’t afford lipsum ipsum, so I have to roll my own!) I hope all this typing doesn’t give me a repetitive-motion injury. I must keep going to make sure that there’s not only enough text to trigger the ellipsis but also enough text to possibly leak out beyond the ellipsis.

Here's a second paragraph of two paragraphs of multi-paragraph text. Internally, each paragraph of text is wrapped in a set of <p> tags. the -webkit-line-clamp property is applied to the p element itself, rather than to its container. As you’ll probably detect visually, there is not undesirable “leakage” of the text to be truncated below the ellipsis. I’ll keep typing so that there’s enough text to translate. (I can’t afford lipsum ipsum, so I have to roll my own!) I hope all this typing doesn’t give me a repetitive-motion injury. I must keep going to make sure that there’s not only enough text to trigger the ellipsis but also enough text to possibly leak out beyond the ellipsis.

Removing the p elements loses the paragraph structure, even after adding br elements

To eliminate the mess of overlying text in Case 5, we can simply remove the p elements that enclose each paragraph.

However, then we love the paragraph structure of the text. See Case 6.

In the first instance, you’d lose line break between what should be separate paragraphs. Even if you manually enter br tags to replace the p elements, you’ll lose any vertical spacing (padding or margin) that you would otherwise have separating paragraphs. You’d also lose any first-line indentation.

Case 6: Can you truncate multi-paragraph text after a specified number of lines using -webkit-line-clamp? (Applying -webkit-line-clamp to the container)

Here's a first paragraph of two paragraphs of multi-paragraph text. Internally, each paragraph of text is wrapped in a set of <p> tags. the -webkit-line-clamp property is applied to the p element itself, rather than to its container. As you’ll probably detect visually, there is not undesirable “leakage” of the text to be truncated below the ellipsis. I’ll keep typing so that there’s enough text to translate. (I can’t afford lipsum ipsum, so I have to roll my own!) I hope all this typing doesn’t give me a repetitive-motion injury. I must keep going to make sure that there’s not only enough text to trigger the ellipsis but also enough text to possibly leak out beyond the ellipsis.
Here's a second paragraph of two paragraphs of multi-paragraph text. Internally, each paragraph of text is wrapped in a set of <p> tags. the -webkit-line-clamp property is applied to the p element itself, rather than to its container. As you’ll probably detect visually, there is not undesirable “leakage” of the text to be truncated below the ellipsis. I’ll keep typing so that there’s enough text to translate. (I can’t afford lipsum ipsum, so I have to roll my own!) I hope all this typing doesn’t give me a repetitive-motion injury. I must keep going to make sure that there’s not only enough text to trigger the ellipsis but also enough text to possibly leak out beyond the ellipsis.