cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <h1>
  <a href="https://dollarshaveclub.github.io/shave/" target="_blank">Shave</a>, A modern javascript plugin for truncating text within an html element.
</h1>
<h2>Time elapsed for <a href="https://dollarshaveclub.github.io/shave/" target="_blank">Shave</a> to truncate 50 elements: <span class="elapsed"></span></h2>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p><p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p><p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p><p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>
<p class="mh100">會全用民知多起功在有場館爾期臺多而不雄山選在場地市者電養說化運面遊聲時上教要下素的正一曾又如到性喜色體歷定部手里克業選陽響水起金園際吃權終麼意建本分香來象洋道方業動害有不財是了題此然把經會得善所建那各相標一來依正員由不古達解工面想推的</p>

            
          
!
            
              h1, 
h2,
p {
  margin: 1rem auto;
}
h1, 
h2 {
  max-width: 40rem;
  text-align: center;
}
p {
  max-width: 18.75rem;
  margin: 1rem auto;
}
            
          
!
            
              (function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  typeof define === 'function' && define.amd ? define(factory) :
  (global.shave = factory());
}(this, (function () { 'use strict';

function shave(target, maxHeight, opts) {
  if (!maxHeight) throw Error('maxHeight is required');
  var els = typeof target === 'string' ? document.querySelectorAll(target) : target;
  if (!('length' in els)) els = [els];

  var defaults = {
    character: '…',
    classname: 'js-shave',
    spaces: true
  };
  var character = opts && opts.character || defaults.character;
  var classname = opts && opts.classname || defaults.classname;
  var spaces = opts && opts.spaces === false ? false : defaults.spaces;
  var charHtml = '<span class="js-shave-char">' + character + '</span>';

  for (var i = 0; i < els.length; i++) {
    var el = els[i];
    var span = el.querySelector('.' + classname);

    // If element text has already been shaved
    if (span) {
      // Remove the ellipsis to recapture the original text
      el.removeChild(el.querySelector('.js-shave-char'));
      el.textContent = el.textContent; // nuke span, recombine text
    }

    var fullText = el.textContent;
    var words = spaces ? fullText.split(' ') : fullText;

    // If 0 or 1 words, we're done
    if (words.length < 2) continue;

    // Temporarily remove any CSS height for text height calculation
    var heightStyle = el.style.height;
    el.style.height = 'auto';
    var maxHeightStyle = el.style.maxHeight;
    el.style.maxHeight = 'none';

    // If already short enough, we're done
    if (el.offsetHeight < maxHeight) {
      el.style.height = heightStyle;
      el.style.maxHeight = maxHeightStyle;
      continue;
    }

    // Binary search for number of words which can fit in allotted height
    var max = words.length - 1;
    var min = 0;
    var pivot = void 0;
    while (min < max) {
      pivot = min + max + 1 >> 1;
      el.textContent = spaces ? words.slice(0, pivot).join(' ') : words.slice(0, pivot);
      el.insertAdjacentHTML('beforeend', charHtml);
      if (el.offsetHeight > maxHeight) max = spaces ? pivot - 1 : pivot - 2;else min = pivot;
    }

    el.textContent = spaces ? words.slice(0, max).join(' ') : words.slice(0, max);
    el.insertAdjacentHTML('beforeend', charHtml);
    var diff = spaces ? words.slice(max + 1).join(' ') : words.slice(max);

    el.insertAdjacentHTML('beforeend', '<span class="' + classname + '" style="display:none;">' + diff + '</span>');

    el.style.height = heightStyle;
    el.style.maxHeight = maxHeightStyle;
  }
}

if (typeof window !== 'undefined') {
  var plugin = window.$ || window.jQuery || window.Zepto;
  if (plugin) {
    plugin.fn.shave = function shavePlugin(maxHeight, opts) {
      shave(this, maxHeight, opts);
      return this;
    };
  }
}

return shave;

})));


const startTime = new Date();

shave('p', 70, {spaces: false});

const endTime = new Date();
const elapsed = endTime - startTime;
$('.elapsed').html(`${elapsed} ms`);
console.log($('p').length);

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console