justify-content
, align-content
and align-items
etc. is stretch
), and take the width of the content insideflex-wrap: wrap
. Without it, it’ll overflow, but not wrapWhy not percentages? Percentages are explicit dimensions, you’re telling the browser one fix percentage. The button is always going to be 10%, no matter how big your TV is. Flexbox sizes elements by the size of their content. So the button will be the size of it’s text. You can decide an initial size and then you can decide whether it should grow (flex: auto
) or shrink (flex: none
)
float
s, they wrap with their respective floats, what was floated to the right will wrap and still stay on the right of the next line, with all this empty space on it’s left. It looks a bit weird. Consider meta info section of an article, you wan’t everything right aligned on smaller screens but split on the same line when there is enough space. If you achieve the splitting part with floats, you get the wonky wrap issue. Flex is perfect here. flex-wrap
wraps in the same direction of flex-direction
. example 1// AXIS
2<-> justify-content
3<-> flex-direction
4^ align-items
5^ align-self
6
7// LINES
8flex-wrap
9
10// DIRECTIONS
11order
12flex-flow: flex-direction flex-wrap
13
14// DIMENSIONS
15min-width
16min-height
17flex: flex-grow flex-shrink flex-basis
Make an element a Flex container
1.container {
2 display: flex; /* block-level container */
3 display: inline-flex; /* inline-level container */
4}
Every element inside a flex container automatically becomes a flex item.
You determine direction with either the order
or the flex-direction
properties. Changing the direction moves the flex items independent of their HTML source order.
1flex-direction: row (default) / row-reverse / column / column-reverse
2order: 0 (default)
justif-content
or align-content
, you probably have the direction changed and the main axis has changed as a resultorder
can also take negative values, e.g. -3
flex
determines the dimensions.
1flex: flex-grow flex-shrink flex-basis
2flex: 1; /* all flex items will take one equal part */
If there isn’t enough space, the size will be determined by the content inside.
flex-grow
= how much flex items will grow relative to other items if extra space is available (proportion of extra space that it gets)flex-shrink
= how much item will shrink relative to others if there is not enough space (proportion of overflow that gets shaved off)flex-basis
= the initial starting point before space is distributed (any standard width/height value including auto
)1input {
2 flex: 1 0 40%;
3 /*
4 flex-basis: 40%; start field at 40% wide
5 flex-shrink: 0; don't shrink smaller than starting value
6 flex-grow: 1; give it 1 (equal) share of any available extra width on it's line
7}
align-items
vs. align-content
align-content
sets how multiple lines are spaced apart.
align-content
determines the spacing between lines, while align-items
determines how the items as a whole are aligned within the container. When there is only one line, align-content
has no effect
align-items
interferes with flex items in a row while align-content
interferes with rows themselves. align-content
comes into play when you have flex-wrap: wrap
defined and your items are wrapped (i.e. are on multiple rows)align-content
is for multi-line flex containers, it has no effect when flex items are on the same line.align-items
(cross-axis) is the opposite of justify-content
(main-axis), it aligns flex items inside a flex containerGive it a flex
value. flex
can take three values: flex-grow
, flex-shrink
, flex-basis
1.el {
2 flex: 1; /* Three values: flex-grow (number) | flex-shrink (number) | flex-basis (width/height)*/
3}