112

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

Hunter Turner
  • 6,592
  • 11
  • 37
  • 55
Alex
  • 64,868
  • 164
  • 416
  • 621

8 Answers8

217

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

vals
  • 58,159
  • 11
  • 81
  • 129
58

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.

Kevin Workman
  • 40,517
  • 9
  • 64
  • 103
BassMHL
  • 7,147
  • 9
  • 47
  • 62
15

Remove the width on .container > div and use flex: auto; on #main: fiddle

#main {
   flex: auto; 
   background: lightblue; 
  -webkit-order: 2;
   order: 2;     
}
Adrift
  • 55,121
  • 12
  • 89
  • 88
9

Give the #left div a min-width of 200px, should do the job.

gpgekko
  • 3,256
  • 3
  • 32
  • 32
1

add display: contents on the element or div you want to maintain the width.

Amir Hassan Azimi
  • 8,357
  • 5
  • 31
  • 40
1

Also (if nothing from above works)

Check your min-width and max-width. It fixed the same problem for me by increasing their range.

0

Solved this with a flex not respecting min-width when there was not enough content to fill that width.

Added the CSS rule box-sizing: initial; on the same flex element that had the non-working min-width declaration.

-4

Add display:inline-block; in left class

The Codesee
  • 3,628
  • 3
  • 32
  • 71
rjdmello
  • 859
  • 2
  • 9
  • 14