Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining text-overflow:ellipsis with display:flex [duplicate]

Tags:

html

css

flexbox

I'd like to use display: flex; for centering while also having a ... overflow when text overflows. It seems that as soon as I introduce the display: flex, the ellipsis stops working. Any suggestions on how to combine these two? JSFiddle: https://jsfiddle.net/silverwind/sw78h02b/1/

.target {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: red;
  margin: 2rem;
  display: flex; /* remove this to get ellipsis to show */
}
<div class="target">
  Text here is very very long that it will overflow the width of the containing element. Adding another sentence to make sure it will overflow.
</div>
like image 979
silverwind Avatar asked Aug 03 '17 16:08

silverwind


People also ask

How do I add an ellipsis to a text-overflow?

To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: ''; . This keyword value will display an ellipsis ( '…' , U+2026 HORIZONTAL ELLIPSIS ) to represent clipped text.

How do I wrap text in display flex?

As you only want the text itself to wrap you need to use flex-wrap: nowrap; to keep . right on the same line. The text will automatically wrap when there is not enough space.

What is text-overflow ellipsis?

The text-overflow property specifies how overflowed content that is not displayed should be signaled to the user. It can be clipped, display an ellipsis (...), or display a custom string.


1 Answers

Put your text inside span and use display: flex on parent and text-overflow on span.

.target {
  background: red;
  margin: 2rem;
  display: flex; 
}
span {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
<div class="target">
  <span>Text here is very very long that it will overflow the width of the containing element. Adding another sentence to make sure it will overflow.</span>
</div>
like image 196
Nenad Vracar Avatar answered Oct 02 '22 16:10

Nenad Vracar