Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flex item overflows container due to long word even after using word-wrap

.parent{   width:100%;   display:flex;   flex-direction:row;   flex-wrap:nowrap;   padding:1em;   background-color:red;   box-sizing:border-box; } .child1{   background-color:mistyrose;   padding:1em; }  .child2{   background-color:powderblue;   padding:.5em;   word-wrap:break-word;   max-width:500px; } .child3{   background-color:powderblue;   padding:.5em;   word-wrap:break-word;  }
<div class="parent">  <div class="child1">    question  </div>   <div class="child2">       somethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomething        </div> </div>  <div class="parent">   <div class="child1">     question   </div>   <div class="child3">    somethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomething   </div> </div>          

The main issue with the above code is ,child3 overflows but if I give a max-width in child2it will not overflow the parent. In both cases I used word-wrap: break-word;

You can check the code here http://jsfiddle.net/vbj10x4k/

I need to know why it happens and how to solve it without using max-width/width to fixed pixel values.I need it to be responsive.

like image 976
Sachin Avatar asked Mar 22 '16 09:03

Sachin


People also ask

How do you wrap text in a Flex container?

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.

Is it possible to break the long words and wrap it on the next line using CSS?

The word-break property in CSS is used to specify how a word should be broken or split when reaching the end of a line. The word-wrap property is used to split/break long words and wrap them into the next line.

Why is my Flexbox not wrapping?

If you are using flexbox and want the content to wrap, you must specify flex-wrap: wrap . By default flex items don't wrap. To have the images be three-across, you should specify flex-basis: 33.33333% .


1 Answers

Instead of setting a max-width for your flex item, use a min-width declaration.

By default, if no min-width is set, the item's content min-width is assumed and the flex item's width will never be smaller. By setting a min-width of e.g. 40%, the item will shrink to at most 40% of the flex parent.

.child2, .child3 {   min-width: 40%; } 

.parent{    width:100%;    display:flex;    flex-direction:row;    flex-wrap:nowrap;    padding:1em;    background-color:red;    box-sizing:border-box;  }  .child1{    background-color:mistyrose;    padding:1em;  }  .child2{    background-color:powderblue;    padding:.5em;    word-wrap: break-word;    overflow-wrap: break-word;    min-width: 40%;  }  .child3{    background-color:lightyellow;    padding:.5em;    word-wrap: break-word;    overflow-wrap: break-word;    min-width: 40%;  }
<div class="parent">    <div class="child1">      question    </div>    <div class="child2">     somethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomethingsomething      </div>    <div class="child3">      Works well with long URLs: <a href="#"> https://thisisadamnlongurlcontainingneitherhyphensoradditionalperiods.com</a>    </div>  </div>

See the code snippet above or the external demo: http://jsfiddle.net/vbj10x4k/5/

like image 132
Paul Avatar answered Oct 05 '22 13:10

Paul