Coder Social home page Coder Social logo

yuanqing / line-clamp Goto Github PK

View Code? Open in Web Editor NEW
75.0 2.0 13.0 205 KB

:clamp: Line clamp a DOM element in vanilla JavaScript

Home Page: https://codepen.io/lyuanqing/pen/VQQVry

License: MIT License

JavaScript 100.00%
line-clamp truncate ellipsis javascript

line-clamp's Introduction

line-clamp npm Version Build Status Bundle Size

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

Editable demo (CodePen)

HTML:

<div class="line-clamp">
  Lorem ipsum dolor sit amet, <strong>consectetur adipiscing</strong> elit.
</div>

CSS:

.line-clamp {
  width: 100px;
  line-height: 20px;
}

JavaScript:

const element = document.querySelector('.line-clamp')
lineClamp(element, 3)

Boom:

<div class="line-clamp" style="overflow: hidden; overflow-wrap: break-word; word-wrap: break-word;">
  Lorem ipsum dolor sit amet, <strong>consectetur…</strong>
</div>

Limitations

API

const lineClamp = require('line-clamp')

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

Prior art

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.