Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Width ignored on flexbox items

Tags:

css

width

flexbox

http://jsfiddle.net/XW9Se/

I've set width: 200px; on the left <div> but if I view it with the browser inspector tool it appears that the real width is random or something. It keeps changing depending on the window size.

Why doesn't the width take effect?

EDIT: If I remove width: 100% the width stays fixed. But I need that so the #main div takes the remaining width :( Is there any way to have the sidebar @ fixed width and the other <div> fill the rest of the container width? width: auto; on #main doesn't work..

like image 758
Alex Avatar asked Jan 28 '14 13:01

Alex


People also ask

How do I fix the width on my flexbox?

A flexbox item can be set to a fixed width by setting 3 CSS properties — flex-basis, flex-grow & flex-shrink. flex-basis : This property specifies the initial length of the flex item. flex-grow : This property specifies how much the flex item will grow relative to the rest of the flex items.

Why max width is not working on Flex?

This is because flex items no longer exist in a block formatting context, where block level elements automatically take width: 100% . In a flex formatting context, elements have different defaults. Therefore, to get max-width to work on a flex item, also give the item width: 100% .

Does Flex override width?

flex-basis is limited by both max-width/max-height and min-width/min-height. When declared, flex-basis will override the width / height property set on a flex container.

How do you get flex to carry the whole 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.


2 Answers

The answer from Adrift is perfect; but a change to make it more flex would be

#left{     flex-basis: 200px;     flex-grow: 0;     flex-shrink: 0; } 

And remove the width property entirely.

It would be the flex way to say that the element will have an invariable width of 200px

like image 200
vals Avatar answered Oct 21 '22 08:10

vals


My preferred way of dealing with this situation is to add:

flex-shrink: 0; 

This way you may continue using width in your Flex container and you've given it specific information about how you wish this flex item to behave.

Another option, depending on the requirement is to use min-width, which is respected when using flex.

like image 42
BassMHL Avatar answered Oct 21 '22 06:10

BassMHL