The hidden defaults behind display: flex
A single declaration on a parent element triggers a set of styles on every child that the author never typed. Given markup like this:
<div class="parent">
<div class="child">Child</div>
<div class="child">Child</div>
<div class="child">Child</div>
</div>
a rule reading display: flex on .parent is effectively accompanied by a full set of defaults on .child — as if the following had been written by hand:
.child {
flex: 0 1 auto; /* Default flex value */
}
Those properties are defaults, designed to be overridden, and not knowing they exist is a frequent cause of confusing, hard-to-manage layouts. The flex keyword itself is a shorthand, like background, bundling several declarations into one line:
body {
background: url(sweettexture.jpg) top center no-repeat fixed padding-box content-box red;
}
Longhand is often easier to read, but for flexbox the specification recommends the shorthand, because its sub-properties interact with one another. In most work none of this matters — two lines such as display: flex plus justify-content: space-between lay items side by side and space them evenly, without touching child styles at all. Understanding the parts only becomes necessary when the defaults stop producing the wanted result.
The three values in the shorthand
The default child styles can be read as a single shorthand:
.child {
flex: 0 1 auto;
}
or, translated:
/* This is just how I think about the rule above in my head */
.child {
flex: [flex-grow] [flex-shrink] [flex-basis];
}
/* or... */
.child {
flex: [max] [min] [ideal size];
}
Ideal size, minimum size, expansion. In order: flex-grow, flex-shrink and flex-basis.
flex-grow and the content underneath it
flex-grow defaults to 0 because most flex items should size themselves from their content rather than expand. In the demo below, each element is contenteditable, so typing into one shows how the default behaves:
.parent {
display: flex;
}
Setting it to 1 changes that rule:
.child {
flex: 1 1 auto;
}
Every item now gets an equal share of the parent, but only when the content lengths are equal. This is equivalent to writing:
.child {
flex-grow: 1;
}
— with the other two values merely falling back to their defaults. Grow can also be uneven. The second and third values simply get omitted here:
.child-three {
flex: 3 1 auto;
}
/* or we could just write... */
.child-three {
flex-grow: 3;
}
The first two children share space in equal proportion, while the third grows toward three times the space of the others. (Reading "max, min, ideal size" off the shorthand takes practice.)
Content still intrudes on all of this: with flex-grow at 3, adding more text to an item can push its column wider than expected. The content of a flex item always influences how the three properties combine.
flex-shrink
flex-shrink, the second value, governs how far an element may become smaller; the default of 1 means it holds the same amount of space at all times. Set it to 0 and the element refuses to shrink:
.child {
flex: 0 0 auto;
}
flex-basis
The final value, flex-basis, states an ideal size. Its default, auto, means "use my height or width," so with a flex parent defined only by display: flex:
.parent {
display: flex;
}
.child {
flex: 0 1 auto;
}
each child renders at the width of its own content. Giving the children width: 100%, setting flex-basis: 100%, or setting flex-grow: 1 will all make them fill the parent — a consequence of how the values affect each other, and the reason the shorthand is recommended over independent longhands. A declaration such as:
.child-three {
flex: 0 1 1000px;
}
asks for flex-basis: 1000px if space allows, and otherwise a proportional share. On narrow screens the third element will not actually measure 1000px: the basis is a suggestion, and the still-active flex-shrink pulls it down to the size of its siblings. More content in the other children changes the result as well. Disabling shrink keeps the width:
.child-three {
flex: 0 0 1000px;
}
The element never narrows below 1000px, and will overflow its parent to keep that promise.
Overflow, wrapping and takeaways
All of the above is altered by flex-wrap on the parent:
.parent {
display: flex;
flex-wrap: wrap;
}
.child-three {
flex: 0 0 1000px;
}
Flex items default to a single line; flex-wrap: wrap discards that rule, so items that no longer fit break onto a new line instead of shrinking into the same space.
The three properties affect one another, so an incomplete understanding of one leaves all of them only partly understood. The practical summary:
- Prefer the
flexshorthand. - Read it as max, min and ideal size.
- Remember that an element's content affects how the values work together.



