Recently I have learned some basic rules about flex
layout based on this post A complete guide to Flexbox.
Introduction
Right beginning: display: flex
only affects the items inside the container, but of course for the container, it is a block
element. So the container will possess one line of the page.
Also, it is a 1-dimensional layout, mainly focuses on line layout. Really friendly to responsive design.
For comparison, there is another layout style named grid
. It is covered by another post A complete guide to Grid. Will discuss that in future posts.
Main Properties
display | container
1 | .container { |
inline-flex
will only affect the container to be an inline
element. The items inside the container are placed as inline
elements no matter it is flex
or inline-flex
.
flex-direction | container
1 | .container { |
flex-order | item
1 | .item { |
Place items in the sequence of flex-order
.
flex-grow | item
1 | .item { |
If all items flex-grow
is 0, no extra space will be distributed to all items.
flex-wrap | container
1 | .item { |
nowrap
will put all items in one line.
flex-shrink | item
1 | .item { |
flex-flow | container
Combination for flex-direction
and flex-wrap
.
1 | .container { |
flex-basis | item
1 | .item { |
length
would be a real valid number like 80px
.
If it is a valid length value, it means the item should use this as its width.
If it is 0
, the extra space around content in an item is not counted in when distributing the rest space in line of flex container.
If it is auto
, the extra space around content in an item is distributed based on its flex-grow
value.
See this picture to figure out its usage.
justify-content | container
1 | .container { |
flex | item
Combination for flex-grow
, flex-shrink
and flex-basis
.
1 | .item { |
flex-shrink
and flex-basis
are optional. By default, 0 1 auto
.
Incompatible Setting
Note that float
, clear
and vertical-align
is not working under flex
layout.