level up some UI chops

The Flex Shorthand Doesn't Play Nice

Flexbox flex shorthand pitfall

So many people jump straight to using the flex shorthand property right away and fall face first into the most common flexbox pitfall.

.all-the-things {
  flex: 1 0 300px;
}

This one shorthand sets flex-grow, flex-shrink, and flex-basis all in one. The first problem people have with this shorthand is not fully understanding all of those underlying properties being set. You’re better off treating the flex shorthand as a convenience tool once you (and your team) have reached master proficiency with flexbox. Once you’re there, just remember grow, shrink, basis in that order.

Changing Defaults

Besides needing to understand all of flexbox, a big gotcha with this shorthand is that it sets different defaults. This can bite you in a couple of ways. Say you’re using flex to set just the flex-basis for an item:

See the pen by Dave Geddes (@geddski) on CodePen.

This set the flex-basis to 30px like we told it to, but it also changed the flex-grow from 0 to 1 implicitly, making the items grow unexpectedly. If you’re going to use this shorthand, remember that its default is flex-grow: 1 where the usual flexbox default is flex-grow: 0.

Another default that gets changed if you leave it off is flex-basis. Say you’re using flex to set just the flex-grow and flex-shrink properties, telling it to neither grow nor shrink:

See the pen by Dave Geddes (@geddski) on CodePen.

Even though we didn’t tell it to, this also changed the item’s flex-basis from its usual default of auto to a new value of 0. This new value made our items no longer take up any horizontal space (besides border size) despite have a width declared. To understand why see the difference between width and flex-basis.

Shorthand’s shorthands

The final gotcha with the flex shorthand is that it accepts some magical keywords that set all three properties for you in a single word. For example:

.item {
  flex: auto;
}

is the same as:

.item {
  flex: 1 1 auto;
}

So if you’re going to use this shorthand you need to memorize what the initial, none, auto, and <integer> values map to, as well as what those properties actually do. It’s super convenient to be able to do so much with one line of CSS, but only use this once you’ve really mastered flexbox.

See more Flexbox Pitfalls and How to Avoid Them

Challenge

Once you’ve gotten the hang of flex-grow, flex-shrink, and flex-basis take your flexbox chops to the next level by trying out the flex shorthand. Commit its keyword shorthands to memory so you don’t have to look them up when you come across them in your project. Flexbox Zombies chapter 11 will help.

Dead Tired of Looking up Flexbox?

Flexbox is incredibly powerful. But it's also crazy hard to master. So we all end up depending on a cheat sheet and guessing in the dev tools. Enough of that! Time to master it once and for all, in a way that actually sticks, so you can build any layout you can imagine with flexbox.