flex:
The flex property is three properties in one. It is a shorthand property that includes
flex-grow,
flex-shrink, and flex-basis. It's applied to child flex items along the
main axis.
The flex property dictates the length of the element, relative to the rest of the elements
inside the container. When you add display: flex;
to the parent container, the default flex
values are flex: 0 1 auto;
.
- The first value is the flex-grow property with a default value of
0
.
- The second value is the
flex-shrink property with a default value of
1
.
- The third value is the flex-basis property with a default value of
auto
.
Changing the value of any of these properties determines the size of a child flex item as the size of the
window grows and shrinks. If you don't change
the flex property values, you don't need to add flex to your CSS.
default
The default flex values of flex: 0 1 auto;
mean that the content is
the size its set to in CSS (the flex-basis.) Each child flex
item will shrink at the same rate (flex-shrink.) And the child flex items will not grow
beyond
the width they've been set to (flex-grow.)
To see how flex
works, resize the browser window.
flex-basis
The flex-basis property sets the optimum width to a flex child
item. The default value of auto
means that the flex child items are the size of their content.
Changing the flex-basis property's value sets the width of the child item (in row
orientation.) And as long as there is enough space, the child flex item will be that width. The other child
items withing the parent container with the default flex-basis of auto
will
shrink based on their flex-shrink value.
The flex-basis property is very similar to the width property, although
flex-basis can overwrite the width
property.
If the parent flex container is too small to fit the width set with flex-basis, it will shrink based on its
flex-shrink value. Try reducing the width of the browser window. The center child flex item
continues to be 600px until the parent flex container is too small to maintain this width.
flex-shrink
The flex-shrink property controls how a flex child item shrinks
when the browser window becomes more narrow. The default value is 1
- so when the flex parent
container is smaller than the total width of the flex child items, they'll shrink evenly.
Let's say we have a flex parent container that is 1200px wide and three child flex items that are each 400px
wide - filling the parent flex container.
If the parent container shrinks to 900px, the parent container loses 300px of space. All flex child items will
lose 100px and shrink to 300px by default. We can use the flex-shrink property so that a flex child item can
shrink at a different rate than its siblings.
If one flex child element has a flex-shrink value of 1, a second flex child element has a flex-shrink value
of 2, and a third has a flex-shrink value of 0, the child flex items will shrink at different rates. The 300px
of lost space is divided into 3 equal shares of 100px (1 + 2 + 0). The first item shrinks by 1 share (100px)
and is 300px wide. The second shrinks by 2 shares (200px) and is 200px wide. The third doesn't shrink at all
and is stll 400px wide.
A common way flex-shink is used is to set the flex-shrink value to 0 so that the flex child item doesn't
shrink. If the parent flex container becomes smaller than the width of the child flex item, the item will
"break out" of the container causing an overflow.
Try making the browser window smaller. The width of the center box will stay the same (it's width should be
200px on mobile, 400px on tablet, and 800px on desktop.) The outer boxes will shrink in
size. You can use DevTools to inspect the width of the center box as you resize the window.
flex-grow
The flex-grow property controls how much of the extra space
within a parent flex container a flex child item takes up. With a default value of 0, child items stay the
size they're set to, even if there's additional space within the parent container.
To take up the space within the parent container, the flex-grow values of all of the
flex child items are added together and the extra space is divided into shares. If there is 400px of extra
space in the parent container with three flex child items, and two of them have a flex-grow
value of 1
and the third has the default value of 0
, the 400px of extra space is
divided into 2 shares (1 + 1 + 0). The two child items with a flex-grow value of 1
get a single
share of the extra space - 200px. The third
child item with the default value of 0
doesn't take up any of the extra space
If a single flex child item
has a value of 1 and all others have a value of 0, the flex child item with a value of 1 takes all of the
extra space. Using DevTools, try clicking the flex-grow property off and on to see the width
of the center container change.
flex-basis column
flex-basis sets the box's height as long as there's enough height.
When the height of the parent container is less than the flex-basis value, the child
flex item will shrink at the rate set by the flex-shrink value.
flex-shrink column
The height of the second box is mobile and tablet - 75px, and desktop - 120px.
If the height of the parent container shrinks, the second column's height will not change and will
overflow its container. Try it out in DevTools.
flex-grow column
The height of the second box is set with the flex-grow property, taking up any extra
space in the parent flex container.
If more than one child flex item has a flex-grow value greater than 0, it will share the
extra space.