Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource


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


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.


Save Automatically?

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.


                <div class="text" data-lines="3"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vitae corporis iure reprehenderit sapiente eligendi esse quia provident. Aspernatur quisquam praesentium reprehenderit, culpa cum ullam, ex.</p>Eos expedita odit, similique tempora quod explicabo adipisci, soluta mollitia cum sequi autem. Aperiam <span>est similique voluptatem quibusdam aspernatur</span> porro quam ratione, exercitationem eos reprehenderit.</div>


  width 300px
  font-family Arial
  font-size 14px
  line-height 22px



let textNode = document.querySelector('.text');

let style = window.getComputedStyle(textNode);
const lineHeight = parseInt(style.getPropertyValue('line-height'), 10);
let lines = textNode.dataset.lines;
let currentHeight = textNode.offsetHeight;
let limit = textNode.contentText.length * (lines * lineHeight / currentHeight);


const excludeTags = [

function getRestrictText(text, limit = 180) {
	const fragment = document.createElement('div');
	fragment.innerHTML = text;

	const ctx = {limit, nodesToRemove: []}

	traverseAndCut(fragment, ctx);
	ctx.nodesToRemove.forEach(node => node.parentNode.removeChild(node));

	return fragment.innerHTML + '...';

function traverseAndCut(parent, ctx) {
	for (let i = 0, length = parent.childNodes.length; i < length; i++) {
		const node = parent.childNodes[i];

		if (node.nodeType === Node.TEXT_NODE) {
			const inlinedText = node.textContent.replace(/(\t|\n)+/g, '');

			if (ctx.limit < inlinedText.length) {
				const words = inlinedText.split(/\s+/);

				node.textContent = words
					.reduce((result, word) => {
						ctx.limit -= word.length + 1;
						if (ctx.limit > 0) result.push(word);
						return result;
					}, [])
					.join(' ')
					.replace(/[\s,.]+$/, '');

				ctx.limit = 0;
			} else {
				node.textContent = inlinedText;
				ctx.limit -= inlinedText.length;

		if (node.nodeType === Node.ELEMENT_NODE) {
			if (~excludeTags.indexOf(node.nodeName.toLowerCase())) continue;
			traverseAndCut(node, ctx);
			if (!node.textContent.length) ctx.nodesToRemove.push(node);

function renderNode(tag, text) {
	const node = document.createElement(tag);
	node.textContent = text;
	return node;

const targetHTMLString = `
	<div class="c-entry-content">
		<p>Nintendo, we should try to remember, is a vast international corporation that wants nothing more than to liberate your hard-earned money from your wallet. But hell, if it isn't just slightly more endearing than its competitors. It's cute little touches like the secret message&nbsp;<a href="">discovered this weekend</a> hidden inside the&nbsp;<a href="">Switch's Pro Controller</a> — tiny text that reads "thx2 allgamefans!" — that keeps Nintendo cemented in the hearts of "allgamefans!" everywhere.</p>
		<p>It's not easy to find the message: you'll need to tilt the Switch Pro controller's right thumb stick down, and wiggle it around a bit. Pushing the stick in until it clicks is also a good idea that should, in the right light, show you the cute little note that seems to be printed on the circuit board below. If you still can't find it, Japanese Twitter users @y_beat and @geo_stream have provided handy visual hints.</p>

const resultHTMLString = getRestrictText(targetHTMLString);

const resultFragment = document.createElement('div');
resultFragment.innerHTML = resultHTMLString;

const resultTextString = resultFragment.textContent;
const resultTextStringLength = resultTextString.length;

const resultNode = document.createElement('div');

resultNode.appendChild(renderNode('p', `Count: ${resultTextStringLength}`));
resultNode.appendChild(renderNode('pre', resultTextString));
resultNode.appendChild(renderNode('pre', resultHTMLString));