Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex item filled with text overflowing flex container [duplicate]

Tags:

html

css

flexbox

I am facing almost the same problem as in this question:

  • How to keep a flex item from overflowing due to its text?

And this solution works perfectly:

.container {
    display: -webkit-flex; 
}

.container>div:first-child{
    white-space:nowrap;
   -webkit-order:1;
   -webkit-flex: 1;
   -webkit-flex: 0 1 auto; /*important*/
    text-overflow: ellipsis;
    overflow:hidden;
    min-width:0px; /* new algorithm mises the calculation of the width */
}

.container > div:last-child {
    -webkit-flex: 1;
    -webkit-order:2;
    background: red;
    -webkit-flex:1 0 auto; /*important*/
}
.container > div:first-child:hover{
    white-space:normal;
}
<div class="container">
    <div>foo barfoo bar</div>
    <div>foo bar</div>
</div>

<div class="container">
        <div>foo barfoo barfoo barfoo barfoo barfoo barfoo bar
        foo barfoo barfoo barfoo barfoo barfoo barfoo bar
        foo barfoo barfoo barfoo barfoo barfoo barfoo bar</div>
    <div>foo bar</div>
</div>
<div class="container">
    <div>foo barfoo bar</div>
    <div>foo bar</div>
</div>

<div class="container">
        <div>foo barfoo barfoo barfoo barfoo barfoo barfoo bar
        foo barfoo barfoo barfoo barfoo barfoo barfoo bar
        foo barfoo barfoo barfoo barfoo barfoo barfoo bar</div>
    <div>foo bar</div>
</div><div class="container">
    <div>foo barfoo bar</div>
    <div>foo bar</div>
</div>

(jsFiddle version)

But if I put the presented div in a div with display: flex, my problem comes back.

Here is the full code which does not work. The problem is exactly the same as in the above question.

.container {
    display: flex; 
}

.container>div:first-child{
    white-space:nowrap;
   -webkit-order:1;
   -webkit-flex: 1;
   -webkit-flex: 0 1 auto; /*important*/
    text-overflow: ellipsis;
    overflow:hidden;
    min-width:0px; /* new algorithm mises the calculation of the width */
}

.container > div:last-child {
    -webkit-flex: 1;
    -webkit-order:2;
    background: red;
    -webkit-flex:1 0 auto; /*important*/
}


.test-flex {
  display: flex;
}
<div class="test-flex">
  <div class="container">
          <div>foo barfoo barfoo barfoo barfoo barfoo barfoo bar
          foo barfoo barfoo barfoo barfoo barfoo barfoo bar
          foo barfoo barfoo barfoo barfoo barfoo barfoo bar</div>
      <div>foo bar</div>
  </div>
</div>
like image 397
Taldakus Avatar asked Nov 15 '16 13:11

Taldakus


People also ask

How do you wrap text in 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.

Which flex-wrap value will allow my flex items to all stay in one line?

nowrap: The default value of wrap-flex is nowrap. It is used to specify that the item has no wrap. It makes item wrap in single lines.

Which flex property will you use to flex items on multiple lines?

The flex-wrap CSS property sets whether flex items are forced onto one line or can wrap onto multiple lines.


1 Answers

... if I put the presented div in a div with display: flex, my problem comes back. ... The problem is exactly the same as in the above question.

The solution is also the same as in that question: Add min-width: 0 to the flex item.

As described in the accepted answer to that question, a flex item by default cannot be smaller than the size of its content.

Now that you've created a new primary container (.test-flex), you've made the previous primary container (.container) a flex item.

You now need to override the default setting on flex items (min-width: auto) to allow .container to shrink past the size of its children (the divs with the text).

You can do that with:

  • min-width: 0, or
  • overflow: hidden

.container {
  display: flex;
  min-width: 0;  /* NEW */
}
.container>div:first-child {
  white-space: nowrap;
  order: 1;
  flex: 0 1 auto;
  text-overflow: ellipsis;
  overflow: hidden;
  min-width: 0px;
}
.container > div:last-child {
  order: 2;
  background: red;
  flex: 1 0 auto;
}
.test-flex {
  display: flex;
}
<div class="test-flex">
  <div class="container">
    <div>foo barfoo barfoo barfoo barfoo barfoo barfoo bar foo barfoo barfoo barfoo barfoo barfoo barfoo bar foo barfoo barfoo barfoo barfoo barfoo barfoo bar</div>
    <div>foo bar</div>
  </div>
</div>
like image 183
Michael Benjamin Avatar answered Oct 18 '22 00:10

Michael Benjamin