Growing and Shrinking
The Flex Shorthand
- Shorthand properties are CSS properties that let you set the values of multiple other CSS properties simultaneously. Using a shorthand property, you can write more concise (and often more readable) stylesheets, saving time and energy.~Source: Shorthand properties on MDN
flexis actually a shorthand forflex-grow,flex-shrinkandflex-basis.flex: 1equates to:flex-grow: 1,flex-shrink: 1,flex-basis: 0.- So when we put
flex: 1on our divs, we were actually specifying a shorthand offlex: 1 1 0.
Flex-Grow
flex-growis used as the flex-item’s “growth factor”.- When we applied
flex: 1to every div inside our container, we were telling every div to grow the same amount. - If we instead add
flex: 2to just one of the divs, then that div would grow to 2x the size of the others.
Flex-Shrink
flex-shrinksets the “shrink factor” of a flex item.flex-shrinkonly gets applied if the size of all flex items is larger than their parent container.- For example, if our 3 divs from above had a width declaration like:
width: 100px, and.flex-containerwas smaller than300px, our divs would have to shrink to fit. - If you do not want an item to shrink then you can specify
flex-shrink: 0;.
Flex-Basis
- To put it simply: In a Flex row,
flex-basisdoes the same thing aswidth. In a Flex column,flex-basisdoes the same thing asheight. flex-basissets the initial size of a flex item, so any sort offlex-growing orflex-shrinking starts from that baseline size.- Using
autoas a flex-basis tells the item to check for a width declaration (i.e.width: __px). - There is a difference between the default value of
flex-basisand the way theflexshorthand defines it if noflex-basisis given. The actual default value forflex-basisisauto, but when you specifyflex: 1on an element, it interprets that asflex: 1 1 0. - If you want to only adjust an item’s
flex-growyou can simply do so directly, without the shorthand.
Basic Values of flex
-
flex: initial flex: auto Equivalent to flex: 0 1 auto.Equivalent to flex: 0 1 auto.flex: none flex: <positive-number>Equivalent to flex: 0 0 auto.Equivalent to flex: <positive-number> 1 0.
Basic Syntax
-
By default flex items don't shrink below their minimum content size. To change this, set the item's
min-widthormin-height. -
/* Keyword values */ flex: auto; flex: initial; flex: none; /* One value, unitless number: flex-grow flex-basis is then equal to 0. */ flex: 2; /* One value, width/height: flex-basis */ flex: 10em; flex: 30%; flex: min-content; /* Two values: flex-grow | flex-basis */ flex: 1 30px; /* Two values: flex-grow | flex-shrink */ flex: 2 2; /* Three values: flex-grow | flex-shrink | flex-basis */ flex: 2 2 10%; /* Global values */ flex: inherit; flex: initial; flex: revert; flex: revert-layer; flex: unset; -
Try resizing the flex containers below from the bottom-right corners:
-
flex-wrap
- By default, flex items will try to fit into one line but
flex-wrap: wrapwill ignore that entirely. Now, if those flex items can’t fit in the same space, they’ll break onto a new line.
- By default, flex items will try to fit into one line but