Html – Flexbox with multiline text ellipsis

cssflexboxhtml

I've been using the white-space: no-wrap, text-overflow: ellipsis, and overflow: hidden CSS properties to create ellipsis truncation for multiline text. However, this doesn't work when using flexbox.

When using flex, text-overflow: ellipsis seems to truncate the height of the flex-item to a single line always.

Is it possible to use some combination of flex and css ellipsis truncation for multiline text?

<div className="flex-container">
  <div className="flex-item">
    <p>long multiline text that i would like to truncate</p>
  </div>
</div>

I can get it working with single line truncation. Setting the height manually in conjunction with flex + white-space: nowrap doesn't work.

Best Answer

You can use this code for multiline truncation for ellipsis

.text-block-div {
  display: block;
  display: -webkit-box;
  max-width: 100%;
  height: 43px;
  margin: 0 auto;
  font-size: 14px;
  line-height: 1;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

You can use your text inside this div having class .text-block-div for ellipsis effect.

Note: The ellipsis effect requires webkit, which means if someone views it using a browser that does not support webkit then they will not see the ... but the text will still be cut off at the correct point, so this effect still works on all major browsers.