line-clamp
Line clamp a DOM element in vanilla JavaScript
- Truncates in pure JavaScript; does not rely on
-webkit-line-clamp
- Works even if the given element contains nested DOM nodes
- Supports appending a custom string instead of an ellipsis (
…
)
Usage
HTML:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
CSS:
JavaScript:
const element = document
Boom:
Lorem ipsum dolor sit amet, consectetur…
Limitations
- The element is assumed to have a pixel line-height, obtained via
window.getComputedStyle
.
API
const lineClamp =
lineClamp(element, lineCount [, options])
Returns true
if text was truncated, else returns false
.
options
is an optional configuration object.
- Set
options.ellipsis
to change the string to be appended to the truncated text. Defaults to…
.
See Usage.
Installation
$ yarn add line-clamp