3

I'm trying to get an element with display: flex to work with text-overflow: ellipsis so overflown text is truncated with .... The width of the element is defined via its flex-basis from a parent flexbox, see here:

.parent { display: flex; min-width: 0; } .child { display: flex; background: yellow; flex: 0 0 5em; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
<div class="parent"> <div class="child"> abcabcabcabcabcabcabc </div> </div>

I've search similar answers but none seem to cover this exact case. I do not want to introduce any additional elements and I want to keep the display: flex on the element because I need it for vertical centering.

1

1 Answer 1

4

As @misorude said, text-overflow applies to block container elements.

Try the following:

.parent { display: flex; min-width: 0; } .child { display: block; background: yellow; flex: 0 0 5em; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
<div class="parent"> <div class="child"> abcabcabcabcabcabcabc </div> </div>

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

4 Comments

Unfortunately, this won't work in my case as I'd like to keep display: flex on the child so I can use align-items: center to vertically align other content like SVGs in the child.
Then you either have to add another element inside child to contain the text, or center child inside parent
Guess adding a wrapper inside child is the only option here. child is already centered in parent, but I specifically need to center the child content as well.
you don't need to add display:block .. a flex item is already a block level element

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.