6

I need to display a text as paragraph, but display only three lines and truncate the test while adding ...(three dots) in the paragraph end.

zavg
  • 9,385
  • 4
  • 43
  • 65
Sundeep Saluja
  • 1,009
  • 2
  • 12
  • 35

4 Answers4

14

Calculate max length of string which can fit into 3 lines and use the following script

var maxLength = 140;
var result = yourString.substring(0, maxLength) + '...';
zavg
  • 9,385
  • 4
  • 43
  • 65
11

I use the same logic than @zavg with some improvements:

  • Use the single character.
  • Respect the maximum size.

function truncate(source, size) {
  return source.length > size ? source.slice(0, size - 1) + "…" : source;
}

var res = truncate('Truncate text to fit in 3 lines', 14);
console.log(res);
aloisdg
  • 19,231
  • 5
  • 81
  • 97
7

.class-name {
        display: block;
        white-space: nowrap;
        width: 15em;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    <div style="height: 15vh; padding: 10px;
                background:#dee0e5">
    <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry Lorem Ipsum is simply dummy text of the printing and typesetting industry</span>
    </div>

<div style="height: 15vh;padding: 10px; margin-top:5px;
                background:#aff8af">
    <span class="class-name">Lorem Ipsum is simply dummy text of the printing and typesetting industry Lorem Ipsum is simply dummy text of the printing and typesetting industry</span>
    </div>
1

Try the following css property:

.your-class-name {
  text-overflow: ellipsis;
  -webkit-line-clamp: 3;
  line-clamp: 3;
}
closure
  • 7,354
  • 1
  • 21
  • 23