I have a flex container with a defined width
. Container has flex-direction: row
and 2 columns. One is fixed width, it is inflexible. The other is flexible and should fit all container's remaining space.
When flexible column content is long enough it overflows the container, exceeds its width.
Why is that happening? And how should I fix it right way?
Note: I already solved it by using flex: 1 0 0 instead of 1 0 auto and it would be just fine. But I just don't understand why it stops exceeding the parent and why it starts wrapping the content? Is it even the right way to do it?
HTML:
<div class="flex-container"> <div class="flex-item inflexible"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. </div> <div class="flex-item flexible"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. </div> </div>
CSS:
.flex-container { display: flex; flex-direction: row; width: 500px; background-color: green; } .flex-item { display: block; margin: 20px 0; } .inflexible { flex: 0 0 auto; width: 100px; background-color: red; } .flexible { flex: 1 0 auto; background-color: blue; }
The JSFiddle
flex: 1 0 200px; If you have one element that has a flex-basis of 200px, flex-grow of 1, and flex-shrink of 0, this element will be at minimum 200px wide, but it will be allowed to grow if there is extra space. In this case, you can think of the flex-basis as being a minimum width.
The flex-shrink property. The flex-shrink property specifies the flex shrink factor, which determines how much the flex item will shrink relative to the rest of the flex items in the flex container when negative free space is distributed.
When you want a flex item to occupy an entire row, set it to width: 100% or flex-basis: 100% , and enable wrap on the container. The item now consumes all available space.
To achieve expected result, specify the width for the .flexible class as well
.flexible { flex: 1 0 auto; width:200px; background-color: blue; word-wrap:break-word; }
http://codepen.io/nagasai/pen/LkJzLz
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With