1

I have a <p> tag to display some information but it should be restricted to 2 lines and if the text is greater then it should show "..." at end. I tried many solutions but i am not able to get exactly what i am looking for. i have tried below css.

.mw {
   overflow: hidden;
   text-overflow: ellipsis;
   display: -webkit-box;
   -webkit-box-orient: vertical;
   -webkit-line-clamp: 2;
   line-height: 20px;       
   max-height: 40px;       
   width:100px;
   border:1px solid #ccc;
}

but i am getting three different outputs for three different text in <p>.

TestingTestingTestingTestingTestingTesting
enter image description here

TestingTesting TestingTestingTestingTesting
enter image description here

TestingTesting TestingTesting TestingTesting
enter image description here

4 Answers 4

2

I think the issue is caused by the length of the word(s) "TestingTestingTestingTestingTestingTesting" Using word-wrap (word-wrap: break-word;) would alleviate the issue: example here

Sign up to request clarification or add additional context in comments.

Comments

0

.mw {
   overflow: hidden;
   text-overflow: ellipsis;
   display: block;
   -webkit-box-orient: vertical;
   -webkit-line-clamp: 2;
   line-height: 20px;       
   max-height: 40px;       
   width:100px;
   border:1px solid #ccc;
}
<p class="mw">
  TestingTestingTestingTestingTestingTesting
</p>

use display: block for truncation.

NOTE: clamp css will not work on Firefox and IE

Ref: https://caniuse.com/#search=clamp

Comments

0

Add word-break: break-all;.

.mw {
   overflow: hidden;
   text-overflow: ellipsis;
   display: -webkit-box;
   -webkit-box-orient: vertical;
   -webkit-line-clamp: 2;
   line-height: 20px;       
   max-height: 40px;       
   width:100px;
   border:1px solid #ccc;
   word-break: break-all;
}
<p class="mw">TestingTestingTestingTestingTestingTesting</p>

<p class="mw">TestingTesting TestingTestingTestingTesting
</p>

<p class="mw">TestingTesting TestingTesting TestingTesting</p>

Comments

0

Try this it will work on all browsers it will ellipsis after two lines.

.mw { 
      display: block;
      /* Fallback for non-webkit */
      display: -webkit-box;
      max-height: 28pt;
      /* Fallback for non-webkit */
      font-size: 10pt;
      line-height: 1.4;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
      -ms-text-overflow: ellipsis;
      -o-text-overflow: ellipsis;
      text-overflow: ellipsis;
    }
<p class="mw">all long text will come here.all long text will come here.all long text will come here.all long text will come here.all long text will come here.all long text will come here.all long text will come here.all long text will come here.all long text will come here.all long text will come here.</p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.