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>Line Clampin'</h1>
<h2>Weird WebKit Flexbox Way</h2>
<div class="module line-clamp">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</div>
<h2>Fade Out Way</h2>
<div class="module fade">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</div>
<h2>Opera Overflow Way</h2>
<div class="module last-line">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</div>
<h2>Clamp.js Way</h2>
<div class="module js">
<p id="clampjs">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</div>
<h2>ftellipsis Way</h2>
<div class="module js ftellipsis" id="ftellipsis">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</div>
<h2>TextOverflowClamp.js Way</h2>
<div class="module js textoverflowclamp" id="textoverflowclamp">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</div>
@import url(https://fonts.googleapis.com/css?family=Open+Sans);
body {
padding: 20px;
font: 1.2em/1.2em 'Open Sans', sans-serif;
}
.module {
width: 250px;
margin: 0 0 1em 0;
overflow: hidden;
}
.module p {
margin: 0;
}
.line-clamp {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
}
.fade {
position: relative;
height: 3.6em; /* exactly three lines */
}
.fade:after {
content: "";
text-align: right;
position: absolute;
bottom: 0;
right: 0;
width: 70%;
height: 1.2em;
background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 50%);
}
.last-line {
height: 3.6em; /* exactly three lines */
text-overflow: -o-ellipsis-lastline;
}
.ftellipsis {
height: 3.6em;
}
h1 {
margin: 0 0 1em 0;
}
h2 {
font-size: 1.2em;
}
// https://github.com/josephschmitt/Clamp.js
var module = document.getElementById("clampjs");
$clamp(module, {clamp: 3});
// https://github.com/ftlabs/ftellipsis
var element = document.getElementById('ftellipsis');
var ellipsis = new Ellipsis(element);
ellipsis.calc();
ellipsis.set();
// https://codepen.io/Merri/pen/Dsuim
/**
* TextOverflowClamp.js
*
* Updated 2013-05-09 to remove jQuery dependancy.
* But be careful with webfonts!
*/
// bind function support for older browsers without it
// https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
// the actual meat is here
(function(w, d){
var clamp, measure, text, lineWidth,
lineStart, lineCount, wordStart,
line, lineText, wasNewLine,
ce = d.createElement.bind(d),
ctn = d.createTextNode.bind(d);
// measurement element is made a child of the clamped element to get it's style
measure = ce('span');
(function(s){
s.position = 'absolute'; // prevent page reflow
s.whiteSpace = 'pre'; // cross-browser width results
s.visibility = 'hidden'; // prevent drawing
})(measure.style);
clamp = function (el, lineClamp) {
// make sure the element belongs to the document
if(!el.ownerDocument || !el.ownerDocument === d) return;
// reset to safe starting values
lineStart = wordStart = 0;
lineCount = 1;
wasNewLine = false;
lineWidth = el.clientWidth;
// get all the text, remove any line changes
text = (el.textContent || el.innerText).replace(/\n/g, ' ');
// remove all content
while(el.firstChild !== null)
el.removeChild(el.firstChild);
// add measurement element within so it inherits styles
el.appendChild(measure);
// http://ejohn.org/blog/search-and-dont-replace/
text.replace(/ /g, function(m, pos) {
// ignore any further processing if we have total lines
if(lineCount === lineClamp) return;
// create a text node and place it in the measurement element
measure.appendChild(ctn(text.substr(lineStart, pos - lineStart)));
// have we exceeded allowed line width?
if(lineWidth < measure.clientWidth) {
if(wasNewLine) {
// we have a long word so it gets a line of it's own
lineText = text.substr(lineStart, pos + 1 - lineStart);
// next line start position
lineStart = pos + 1;
} else {
// grab the text until this word
lineText = text.substr(lineStart, wordStart - lineStart);
// next line start position
lineStart = wordStart;
}
// create a line element
line = ce('span');
// add text to the line element
line.appendChild(ctn(lineText));
// add the line element to the container
el.appendChild(line);
// yes, we created a new line
wasNewLine = true;
lineCount++;
} else {
// did not create a new line
wasNewLine = false;
}
// remember last word start position
wordStart = pos + 1;
// clear measurement element
measure.removeChild(measure.firstChild);
});
// remove the measurement element from the container
el.removeChild(measure);
// create the last line element
line = ce('span');
// give styles required for text-overflow to kick in
(function(s){
s.display = 'inline-block';
s.overflow = 'hidden';
s.textOverflow = 'ellipsis';
s.whiteSpace = 'nowrap';
s.width = '100%';
})(line.style);
// add all remaining text to the line element
line.appendChild(ctn(text.substr(lineStart)));
// add the line element to the container
el.appendChild(line);
}
w.clamp = clamp;
})(window, document);
// the only bit of jQuery
$(window).bind('load', function() {
clamp(document.getElementById('textoverflowclamp'), 3);
});
Also see: Tab Triggers