1

Does anyone know how to avoid overflowing of long words in flex container?

.wrap{
  display:flex;
  overflow-wrap: break-word;
  max-width:100%;
}
<div class="wrap">
  <div class="a">
    a
  </div>
  <div class="b">
    ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  </div>
</div>

I only found this thread Break long words inside flex item but it doesn't solve the problem

Thank you

David Holada
  • 109
  • 1
  • 2
  • 14
  • 1
    What do you want it to look like? overflow:hidden? – Esko Sep 11 '18 at 12:01
  • 1
    you can break the line with word-break: break-all – Chris Li Sep 11 '18 at 12:01
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. – Paulie_D Sep 11 '18 at 12:05
  • 1
    add `min-width:0` to `b` – Temani Afif Sep 11 '18 at 12:35
  • I don't want neither overflow: hidden nor word-break: break-all. I need to make a new line when the long word "ffff..." hits the end of its ".wrap" container which shouldn't be wider than 100% (max-width:100%). But I don't want to accomplish this by "break-all" because this property crops not only long words but even short words. – David Holada Sep 11 '18 at 13:21
  • Thank you Temani Afif. That solved my problem! – David Holada Sep 11 '18 at 14:28

2 Answers2

2

Nice and Easy - word-break: break-all;

Just add this. Nothing more nothing less :)

word-break: break-all;

enter image description here

See the result here:

.wrap{
  display:flex;
  overflow-wrap: break-word;
  max-width:100%;
  word-break: break-all;
}
<div class="wrap">
  <div class="a">
    a
  </div>
  <div class="b">
    ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
  </div>
</div>
Electron
  • 969
  • 1
  • 8
  • 22
  • But break-all will break even shorter words...change "ffff" to for example "animals animals" (x100) and the end of line will look like "ani" or "anima" and so on. – David Holada Sep 11 '18 at 13:25
  • Yes. There is no word length variable. However, you could write some JavaScript to first count how many letters a work has, then if the condition is met, say 20 letters, it would then insert the `word-break: break-all;` style. – Electron Sep 11 '18 at 13:32
  • Something like this: https://stackoverflow.com/questions/20345070/if-else-conditional-based-on-character-count-jquery – Electron Sep 11 '18 at 13:54
  • Ok, thanks, but I need to solve this ideally without JS. I create new thread which describes my problem better https://stackoverflow.com/questions/52277368/flexbox-affects-overflow-wrap-behavior – David Holada Sep 11 '18 at 14:02
0

Did you try to do like this?

 .wrap{
      display: flex;
      max-width: 150px;
    }

    .b{
      word-break: break-all;
    }
Udara Kasun
  • 2,182
  • 18
  • 25