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

              
                <div class="single">
  <h2>单行文本截断</h2>
  <p class="row">
    <label>原始文本:</label>
    <span class="ellipsis">这是一行非常长的文本,需要被截断至宽度为200px。</span>
  </p>
  <p class="row">
    <label>CSS截断:</label>
    <span class="ellipsis css">这是一行非常长的文本,需要被截断至宽度为200px。</span>
  </p>
  <p class="row">
    <label>JS截断:</label>
    <span class="ellipsis js">这是一行非常长的文本,需要被截断至宽度为200px。</span>
  </p>
</div>
<div class="multiple">
  <h2>多行文本截断</h2>
  <p class="row">
    <label>原始文本:</label>
    <span class="ellipsis">这是一段非常长的文本,需要被截断至宽度为200px,但是和上面不同的是,允许将文本折行,最多三行。</span>
  </p>
  <p class="row">
    <label>CSS截断:</label>
    <span class="ellipsis css">这是一段非常长的文本,需要被截断至宽度为200px,但是和上面不同的是,允许将文本折行,最多三行。</span>
  </p>
  <p class="row">
    <label>JS截断:</label>
    <span class="ellipsis js">这是一段非常长的文本,需要被截断至宽度为200px,但是和上面不同的是,允许将文本折行,最多三行。</span>
  </p>
</div>
              
            
!

CSS

              
                body {
  background-color: #fafafa;
}

.single,
.multiple {
  display: flex;
  flex-direction: column;
  border: 1px solid black;
  padding: 10px;

  .row {
    display: flex;

    label {
      width: 100px;
    }
  }
  .ellipsis {
    background-color: #fdd835;

    &.css {
      max-width: 200px;
      // width: 200px;
    }

    &.js.use-pseudo::after {
      content: "\2026";
    }
  }
}

.single {
  margin-bottom: 10px;
}

.single .ellipsis.css {
  // display: inline-block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.multiple .ellipsis.css {
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

              
            
!

JS

              
                window.onload = () => {
  // 单行文本截断
  const singleEle = document.querySelector(".single .ellipsis.js");
  truncateText(singleEle, 200);
  // 多行文本截断
  const multipleEle = document.querySelector(".multiple .ellipsis.js");
  truncateText(multipleEle, 200, 3);
};

function truncateText(element, width, lineNum = 1) {
  const canvas = document.createElement("canvas");
  const context = canvas.getContext("2d");
  context.font = window.getComputedStyle(element).font;

  // Why trim? see: https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Whitespace
  // 某些情况下,可能需要将前后被trim的空白字符还原
  const text = element.textContent.trim();

  if (context.measureText(text).width <= width) return;

  const ellipsis = "\u2026"; // https://www.compart.com/en/unicode/U+2026
  let result = "",
    startIndex = 0;

  for (let i = 0; i < lineNum; i++) {
    let left = startIndex,
      right = text.length - 1;

    while (left <= right) {
      const mid = Math.floor((left + right) / 2);

      // 仅为最后一行的文本增加ellipsis符号
      const testText =
        lineNum === i + 1
          ? text.slice(startIndex, mid + 1) + ellipsis
          : text.slice(startIndex, mid + 1);

      if (context.measureText(testText).width <= width) {
        left = mid + 1;
      } else {
        right = mid - 1;
      }
    }

    result += text.slice(startIndex, right + 1);
    startIndex = right + 1;

    if (startIndex > text.length - 1) {
      break;
    } else {
      if (lineNum === i + 1) {
        // result += ellipsis;
        element.classList.add("use-pseudo");
      }
    }
  }

  element.textContent = result;
  element.style.width = `${width}px`;
}

              
            
!
999px

Console