line-clamp
Truncate multi-line text in a DOM element.
Features
- Exit if we detect that no truncation is necessary (ie. content does not overflow container).
- Allows use of a custom string instead of an ellipsis.
Limitations
Usage
HTML
<div class="line-clamp-wrapper">
<div class="line-clamp">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
</div>
CSS
.line-clamp-wrapper {
height: 60px;
overflow: hidden;
}
.line-clamp {
width: 100px;
line-height: 20px;
overflow-wrap: break-word;
word-wrap: break-word;
}
JavaScript
import lineClamp from 'line-clamp';
lineClamp(element, { lineCount: 3 });
Example
To run the example, do:
$ git clone https://github.com/yuanqing/line-clamp
$ npm install
$ npm install --global gulp
$ gulp example --open
API
import lineClamp from 'line-clamp';
lineClamp(element, options)
See Usage above for the accompanying CSS.
Installation
Install via npm:
$ npm i --save line-clamp
Prior art
License
MIT