9

Facebook uses this CSS:

{flex: 1;  width: 0;}

but the div still has width.

Image here

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Steve Phuc
  • 1,168
  • 2
  • 11
  • 12

1 Answers1

10

You're asking about two different properties and functions.

flex: 1 is equivalent to flex: 1 1 0, both of which are shorthand equivalents of:

  • flex-grow: 1
  • flex-shrink: 1
  • flex-basis: 0

When you add width: 0, it's actually redundant and unnecessary in a row-direction container, like the one in the question.

flex-basis: 0 = width: 0 in this case.

If this flex-basis / width rule were by itself, then you would have your expected behavior: The element would have 0 width.

However, these properties only set the initial main size of a flex item, before free space is distributed by other flex properties.

With flex-grow: 1, the item will consume all available space on the line after having factored in flex-basis / width.

More information:

Community
  • 1
  • 1
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • 6
    According to the spec it might be redundant, but at least in chromium, you sometimes have to set `width:0` even when using `flex-basis:0`. https://bugs.chromium.org/p/chromium/issues/detail?id=464210 – Håken Lid Jul 22 '19 at 20:08
  • @HåkenLid, correct. I cover this issue in [this post](https://stackoverflow.com/q/34352140/3597276), under the section "Browser Bugs" in my answer. – Michael Benjamin Jul 23 '19 at 15:35