0

I want to truncate a text in a html page.

style.css:

p {
 height: 30%;
 overflow: hidden;
 text-overflow: ellipsis;
}

index.html

<p>
    hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
</p>

I want to get a result such:

hhhhhhhhhhhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhhhhhhhh...

But I can't got this result using the style.css mentioned below.How can I fix it?

para club101
  • 57
  • 1
  • 1
  • 10
  • Possible duplicate of [Applying an ellipsis to multiline text](https://stackoverflow.com/questions/33058004/applying-an-ellipsis-to-multiline-text) – CBroe May 29 '17 at 11:05

4 Answers4

4

you maybe looking for this

p{
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    height:56px;
}
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa</p>

Note: its only works on webkit browsers

Rahul
  • 4,294
  • 1
  • 10
  • 12
3

The CSS-Rule word-wrap: break-word; will force even long words like yours to break at the end of the div. You can also limit the width of the div to have a similar effect as you postet.

p {
  height: 30%;
  overflow: hidden;
  text-overflow: ellipsis;
  word-wrap: break-word;
  width: 200px;
}
<p>
 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
</p>
CoderPi
  • 12,985
  • 4
  • 34
  • 62
0

Here is what it is normally used when it is expected that long words will be introduced that will break your layout.

p { overflow-wrap: break-word }

This will cause that lines will break between any two characters. You can also use:

word-break: break-all

... if you expect Chinese, Japanese, and Korean to be inserted in the field.

catalin
  • 31
  • 4
0

Using Bootstrap:

<!-- Block level -->
<div class="row">
  <div class="col-2 text-truncate">
    Praeterea iter est quasdam res quas ex communi.
  </div>
</div>

<!-- Inline level -->
  <span class="d-inline-block text-truncate" style="max-width:150px;">
    Praeterea iter est quasdam res quas ex> communi.
</span>

Will give you:

Praeterea iter e...

Praeterea iter est q...

More at: Bootstrap Text

Cimei
  • 55
  • 1
  • 7