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..
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.
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% .
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.
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.
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
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.
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