6

Why -webkit-line-clamp is not showing proper ellipsis with text-align:justify. It's working fine with text-align:left/right.

Please suggest any trick.

div {
    text-align: justify;
    width:150px;
    padding: 0 5px;
    line-height: 18px;
    display: -webkit-box;
    display: -moz-box;
    display: -ms-box;
    max-width: 100%;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}
<div>Why Line Clamp is not working with text align justify</div>
akash
  • 2,117
  • 4
  • 21
  • 32

4 Answers4

4

I solved with:

/* autoprefixer: off */
-webkit-box-orient: vertical;
/* autoprefixer: on */
Alessio Campanelli
  • 970
  • 11
  • 19
3

Webkit flexbox is half-baked and not standardized! I wouldn't say that this is a bug. Even Opera has it's own way to handle clamping, which is super annoying.

.last-line {
  height: 3.6em; /* exactly three lines */
  text-overflow: -o-ellipsis-lastline;
}

You could use clamp.js to handle your ellipsis but if you're a fan of pure CSS solutions like me - try that: CSS Ellipsis: How to Manage Multi-Line Ellipsis in Pure CSS

Mibit
  • 316
  • 1
  • 10
3

Check out this Fiddle I don't know why in your CSS it is not working, have changed some and did some Experiments, working for me. :)

CSS:

.line-clamp {
    text-align: justify;
    width : 155px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;  
    overflow: hidden;
    text-overflow: ellipsis;
}

HTML

<div class="line-clamp">Why Line Clamp is not working with text align justify</div>

Hope this will Work for you, or Help you to Solve your Issue.

Hardik Bharadava
  • 472
  • 1
  • 9
  • 22
1

The solution you're trying to use is a very old technique. It uses an old version of display: flex;, and should be avoided.

You can use the JavaScript technique, clamp.js, but I've also created this pure CSS version: http://codepen.io/n3ptun3/pen/meYKgZ?editors=110

CSS

p {
  position: relative;
  width: 400px;
  height: 120px;
  line-height: 30px;
  margin: 0px;
  padding: 0px;
  overflow: hidden;
  text-align: justify;
}

p::after {
  content: "...";
  background-color: #fff;
  height: 30px;
  width: 20px;
  position: absolute;
  bottom: 0;
  right: 0;
}

Of course this only works on a solid background, but most of the time that'll be the case with body text.

Here are the main points:

  1. Set the line-height to whatever you like.
  2. Set the height to a multiple of the line-height.
  3. Set the overflow to hidden.
  4. Add the ellipsis to the after pseudo-element and set its background color to that of the paragraph's background color.
neptune
  • 164
  • 6