Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Using <code>-webkit-line-clamp</code> to truncate multiline text is incompatible with multiparagraph text</h1>

<div class="explanation">
  <h2>The fundamental problem: “Leakage” of post-ellipsis text</h2>
  <p>Eventually, the CSS property <a href="https://drafts.csswg.org/css-overflow-3/#line-clamp"><code>line-clamp</code></a> will allow you to use CSS to truncate text after a specified number of lines. But currently (e.g., as of 8/16/2019), <code>line-clamp</code> is just a figment of some working group’s fevered imagination.</p>
  <p>Until then, there’s the <code>-webkit-line-clamp</code> property, which does the job OK-ish, but with some quirks. It has <a href="https://caniuse.com/#search=line-clamp">good browser support</a>.</p>
  <p>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.</p>
  <h3>Well known: Don’t include bottom padding on text to be truncated</h3>
  <p>For example, it’s well known that having bottom padding on the text to be truncated can cause such leakage. Per <a href="https://webplatform.news/issues/2019-05-17">Šime Vidas</a>:</p>
  <blockquote>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.</blockquote></p>
<h3>Not so well known: Wrapping the text to be truncated in paragraph tags can cause leakage</h3>
<h4>When the <code>-webkit-line-clamp</code> property is applied to a container of the <code>p</code> element</h4>
<p>I didn’t anticipate the problem I ran into: I had wrapped text in a set of <code>&lt;p></code> tags—because that’s the semantic kind of guy I am—and text that should have been truncated/invisible leaked out after the ellipsis. See <a href="#case1">Case 1</a>.</p>

<div class="case">
<h2 id="case1">Case 1: Here’s text to truncate that is wrapped in a set of <code>&lt;p></code> tags</h2>

<div class="text-to-truncate">
      <p>Here's a bunch of text that, internally, is wrapped in a set of <code>&lt;p></code> 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 <i>lipsum ipsum</i>, 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.</p>
</div>
 </div>

<p>Removing the enclosing <code>p</code> tags fixes this: see <a href="#case2">Case 2</a>.</p>

<div class="case"><h2 id="case2">Case 2: Here’s text to truncate that is <em>not</em> wrapped in a set of <code>&lt;p></code> tags</h2>

<div class="text-to-truncate">
      Here's a bunch of text that, internally, is <em>not</em> wrapped in a set of <code>&lt;p></code> tags. As you’ll probably detect visually, there is <em>not</em> 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 <i>lipsum ipsum</i>, 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.
</div></div>

<p>First, let me be perfectly clear: the <code>-webkit-line-clamp</code> property was set on a container that held the <code>p</code> element that held the text; the <code>-webkit-line-clamp</code> property was not set on the <code>p</code> element itself.</p>
<h4>Applying the <code>-webkit-line-clamp</code> property directly to the <code>p</code> element eliminates the leakage</h4>
The leakage in <a href="#case1">Case 1</a> can be eliminated by applying the <code>-webkit-line-clamp</code> property directly to the <code>p</code> element rather than to the <code>p</code> element’s container. (See <a href="#case3">Case 3</a>.)

<div class="case">
  <h2 id="case3">Case 3: Here’s text to truncate that <em>is</em> wrapped in a set of <code>&lt;p></code> tags, <em>but</em> the <code>-webkit-line-clamp</code> property is set on the <code>p</code> element directly</h2>

<div>
      <p class="truncate-paragraph">Here's a bunch of text that, internally,  <em>is</em> wrapped in a set of <code>&lt;p></code> tags. However, unlike the earlier examples, the <code>-webkit-line-clamp</code> property is applied to the <code>p</code> element itself, rather than to its container. As you’ll probably detect visually, there is <em>not</em> 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 <i>lipsum ipsum</i>, 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.</p>
</div>
</div>
<h2>What if you want to truncate a block of multi-paragraph text?</h2>
Suppose you have a block of multi-paragraph text and you’d like to truncate that block of text if it runs over <i>n</i> lines, say for <i>n</i>=10?

<h3>Applying <code>-webkit-line-clamp</code> to each paragraph separately doesn’t truncate the <em>block</em> of multiparagraph text; it truncates each paragraph independently</h3>
<p>If (a) you wrap each semantic paragraph in a set of <code>p</code> tags and (b)
 apply the <code>-webkit-line-clamp</code> property directly to the <code>p</code> elements, you have <a href="#case4">Case 4</a>. 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.</p>

<div class="case">
  <h2 id="case4">Case 4: Can you truncate multi-paragraph text after a specified number of lines using <code>-webkit-line-clamp</code>? (Applying <code>-webkit-line-clamp</code> to each paragraph separately)</h2>

<div>
      <p class="truncate-multiparagraph">Here's a first paragraph of two paragraphs of multi-paragraph text. Internally, each paragraph of text <em>is</em> wrapped in a set of <code>&lt;p></code> tags. the <code>-webkit-line-clamp</code> property is applied to the <code>p</code> element itself, rather than to its container. As you’ll probably detect visually, there is <em>not</em> 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 <i>lipsum ipsum</i>, 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.</p>
             <p class="truncate-multiparagraph">Here's a second paragraph of two paragraphs of multi-paragraph text. Internally, each paragraph of text <em>is</em> wrapped in a set of <code>&lt;p></code> tags. the <code>-webkit-line-clamp</code> property is applied to the <code>p</code> element itself, rather than to its container. As you’ll probably detect visually, there is <em>not</em> 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 <i>lipsum ipsum</i>, 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.</p>
</div>
</div>
<h3>Applying <code>-webkit-line-clamp</code> to the container can produce a big mess</h3>
<p>So what if, instead, you apply the <code>-webkit-line-clamp</code> property to the container of the multiple paragraphs, rather than to each <code>p</code> element separately? This can be a <strong>huge mess</strong>! 😱 (See <a href="#case5">Case 5</a>, which shows that the leakage from the first paragraph overlays the text of the second page.)</p>

<div class="case">
  <h2 id="case5">Case 5: Can you truncate multi-paragraph text after a specified number of lines using <code>-webkit-line-clamp</code>? (Applying <code>-webkit-line-clamp</code> to the container)</h2>

<div class="text-to-truncate">
      <p>Here's a first paragraph of two paragraphs of multi-paragraph text. Internally, each paragraph of text <em>is</em> wrapped in a set of <code>&lt;p></code> tags. the <code>-webkit-line-clamp</code> property is applied to the <code>p</code> element itself, rather than to its container. As you’ll probably detect visually, there is <em>not</em> 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 <i>lipsum ipsum</i>, 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.</p>
       <p>Here's a second paragraph of two paragraphs of multi-paragraph text. Internally, each paragraph of text <em>is</em> wrapped in a set of <code>&lt;p></code> tags. the <code>-webkit-line-clamp</code> property is applied to the <code>p</code> element itself, rather than to its container. As you’ll probably detect visually, there is <em>not</em> 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 <i>lipsum ipsum</i>, 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.</p>
</div>
</div>
<h3>Removing the <code>p</code> elements loses the paragraph structure, even after adding <code>br</code> elements</h3>
<p>To eliminate the mess of overlying text in <a href="#case5">Case 5</a>, we can simply remove the <code>p</code> elements that enclose each paragraph.</p>
<p>However, then we love the paragraph structure of the text. See <a href="#case6">Case 6</a>.</p>
<p>In the first instance, you’d lose line break between what should be separate paragraphs. Even if you manually enter <code>br</code> tags to replace the <code>p</code> 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.</p>
<div class="case">
  <h2 id="case6">Case 6: Can you truncate multi-paragraph text after a specified number of lines using <code>-webkit-line-clamp</code>? (Applying <code>-webkit-line-clamp</code> to the container)</h2>

<div class="text-to-truncate-long">
Here's a first paragraph of two paragraphs of multi-paragraph text. Internally, each paragraph of text <em>is</em> wrapped in a set of <code>&lt;p></code> tags. the <code>-webkit-line-clamp</code> property is applied to the <code>p</code> element itself, rather than to its container. As you’ll probably detect visually, there is <em>not</em> 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 <i>lipsum ipsum</i>, 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.<br/>
  
Here's a second paragraph of two paragraphs of multi-paragraph text. Internally, each paragraph of text <em>is</em> wrapped in a set of <code>&lt;p></code> tags. the <code>-webkit-line-clamp</code> property is applied to the <code>p</code> element itself, rather than to its container. As you’ll probably detect visually, there is <em>not</em> 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 <i>lipsum ipsum</i>, 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.
</div>
</div>


</div>
              
            
!

CSS

              
                :root {
  --color-blackish: #222 ;
  --color-whitish: #eee ;
}

.text-to-truncate {
  padding: 0 ;
  overflow: hidden ;
  display: -webkit-box ;
  -webkit-box-orient: vertical ;
  -webkit-line-clamp: 3 ;
  margin: 2em ;
}

.text-to-truncate-long {
  padding: 0 ;
  overflow: hidden ;
  display: -webkit-box ;
  -webkit-box-orient: vertical ;
  -webkit-line-clamp: 15 ;
  margin: 2em ;
}

.text-to-truncate-long br {
  margin-bottom: 2em ;
}

p.truncate-paragraph {
  padding: 0 ;
  overflow: hidden ;
  display: -webkit-box ;
  -webkit-box-orient: vertical ;
  -webkit-line-clamp: 3 ;
  margin: 2em 
}

p.truncate-multiparagraph {
  padding: 0 ;
  overflow: hidden ;
  display: -webkit-box ;
  -webkit-box-orient: vertical ;
  -webkit-line-clamp: 3 ;
  margin: 2em 
}

body {
  font-family: Lato, sans-serif ;
  font-size: 16px;
  background-color: var( --color-blackish ) ;
  color: var( --color-whitish ) ;
  max-width: 50em ;
  padding-left: 20px ;
  margin-left: 20px ;
}

.explanation {
  max-width: 50em ;
  background-color: var( --color-blackish ) ;
  width: 100% ;
  color: var( --contrast-blackish ) ;
  font-size: 1.1em ;
  line-height: 1.5 ;
}
.explanation a,
.explanation a:link,
.explanation a:visited {
  color: pink ;
}

.explanation h2 {
  color: orange ;
}

.explanation h3 {
  color: limegreen ;
}

.explanation h4 {
  color: yellow ;
}

code {
  color: powderblue ;
}

hr {
  margin-top: 3em ;
}

.case {
  border: 2px solid white ;
  padding: 1em ;
  margin: 1em ;
}

.case h2 {
  font-size: 1rem ;
}
              
            
!

JS

              
                /* <crickets> */
              
            
!
999px

Console