HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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><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><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><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><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><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><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><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><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><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><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><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><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><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>
: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 ;
}
/* <crickets> */
Also see: Tab Triggers