Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex item exceeds its container

Tags:

css

flexbox

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

like image 262
Unicorn Avatar asked Aug 02 '16 14:08

Unicorn


People also ask

How can you cause a flex item to grow in size?

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.

Which properties can control the size of Flex items?

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.

How do you make a flex item full width?

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.


1 Answers

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

like image 110
Naga Sai A Avatar answered Oct 14 '22 07:10

Naga Sai A