146

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,804
  • 11
  • 41
  • 56
Alex
  • 66,732
  • 177
  • 439
  • 641

8 Answers8

257

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
  • 61,425
  • 11
  • 89
  • 138
  • 49
    shorthand: `flex: 0 0 200px;` – Dan Aug 15 '16 at 13:08
  • 38
    Simply add `flex-shrink: 0` property and css width will be respected. – Ashish Rawat Jan 22 '18 at 10:04
  • This combined with `order: 2` (to switch it to the right) worked flawlessly to do a side panel. Thanks! – rmolinamir Dec 19 '18 at 21:08
  • 1
    Just wanted to add in that I only needed `flex-shrink: 0` to fix my personal issue (where `flex: auto` alone didn't fix it) – DragonJawad Mar 01 '19 at 14:18
  • Then why when I change the direction of the flex container, the flex-grow: 0; not working? The shape is out of shape. Can you tell me why :/ ? – hayley Jun 01 '21 at 09:54
  • 2
    Just a note for anyone copy pasting, these grow and shrink have to be applied to the FLEX CHILD and not the container. – vigilante_stark Oct 25 '21 at 15:48
  • Perfect, thank you. For my case, it worked without providing flex-basis. Just to keep in mind in case someone is still having problems. – Gogol Feb 03 '22 at 11:41
  • @hayley If the flex direction is column, you set a fixed width and this answer affects height. – ThaJay Jan 26 '23 at 09:13
94

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
  • 41,537
  • 9
  • 68
  • 107
BassMHL
  • 8,523
  • 9
  • 50
  • 67
17

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

gpgekko
  • 3,506
  • 3
  • 32
  • 35
17

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

#main {
   flex: auto; 
   background: lightblue; 
  -webkit-order: 2;
   order: 2;     
}
Adrift
  • 58,167
  • 12
  • 92
  • 90
3

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

Amir Hassan Azimi
  • 9,180
  • 5
  • 32
  • 43
2

Also (if nothing from above works)

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

Vasilis Siourdas
  • 309
  • 1
  • 11
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.

Llama D'Attore
  • 323
  • 1
  • 12
-3

Add display:inline-block; in left class

The Codesee
  • 3,714
  • 5
  • 38
  • 78
rjdmello
  • 865
  • 2
  • 9
  • 14