1

This is best illustrated with a simple example.

I have a container with display: flex and flex-direction: column, with a single div inside with height: 300px and flex: 1.

Chrome renders the nested div at 300px tall, but Firefox renders it as a single line. Is this just a nuance between the implementation of flexbox between the two browsers, or is this bad code somehow? If a nuance, what's the best way to mitigate?

.container {
  display: flex;
  flex-direction: column;
}

.container > div {
  background-color: #666;
  color: white;
  flex: 1;
  height: 300px;
}
<div class="container">
  <div>Single line in Firefox, but 300px tall in Chrome!</div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Matt
  • 23,363
  • 39
  • 111
  • 152

1 Answers1

3

The flex: 1 shorthand rule breaks down as follows:

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

Chrome sees this, but overrides flex-basis with height: 300px.

Firefox sees this, but does not override flex-basis with height: 300px.

The simple cross-browser solution is to get rid of the height rule and just use:

flex: 1 0 300px

In terms of the spec, Firefox has the correct behavior:

7.1. The flex Shorthand

When a box is a flex item, flex is consulted instead of the main size property to determine the main size of the box.

The flex item’s main size property is either the width or height property.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701