css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <section>
  <h2>Multi-line Elipsis: Cross-Browser Javascript and CSS Solution</h2>
    <p>The text below will be truncated with an ellipsis in all browsers. This relies on a bit of Javascript and some CSS to set the number of lines to hide. The Javascript for this pen will loop through all instances on a page but if you only need to handle one instance, you can remove the <code>.forEach</code> and just have it check one.</p>
  <div class="container">
    <p>There's a lot more text in here than what you'll ever see. Pellentesque habitant testalotish 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>
  
  <div class="container">
    <p>Not much text? No clipping.</p>
  </div>
  
  <div class="container">
    <p>Just a couple of lines of text? It'll likely be shown in its entirety on a wider window but as you shrink down it'll continue to truncate to two lines like you'd expect. That's the difference between this and other solutions I've seen.</p>
  </div>
  <p>I'd recommend adding a window resize event listener in here but I'm trying o keep this pen simple. If you need a CSS only version, check out <a href="https://codepen.io/nilsynils/pen/KgaRBo">my other pen on the topic here</a>.</p>
</section>
            
          
!
            
              /* Up here is all the styles to make this pen not look crappy. The meat of this is down below. */
@import 'https://fonts.googleapis.com/css?family=Raleway:100,300,400,700';

body {
  font-family: Raleway;
  font-weight: 400;
  background: lightCyan;
  display: flex;
  justify-content: center;
}

section {
  width: 80%;
  margin: auto;
}

h2 {
  text-align: center;
}

/* This is the important part - particularly the height of the .container */
.container {
  border: 1px solid lightGrey;
  padding: 0 1.5em;
  border-radius: 3px;
  background: #fff;
  margin-bottom: 2em;
  // width: 300px; // Width is optional - works with or without
  height: 3em; // Use multiples of the line-height (1.5em)
  overflow: hidden; 
  line-height: 1.5em;
}

.container p { 
  padding: 0; 
  margin: 0; 
}
            
          
!
            
              const containers = document.querySelectorAll('.container');
Array.prototype.forEach.call(containers, (container) => {  // Loop through each container
    var p = container.querySelector('p');
    var divh = container.clientHeight;
    while (p.offsetHeight > divh) { // Check if the paragraph's height is taller than the container's height. If it is:
        p.textContent = p.textContent.replace(/\W*\s(\S)*$/, '...'); // add an ellipsis at the last shown space
    }
})
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console