"It's a more efficient way to lay out, align and distribute space among items in a container"
even if their size is unknown
Imagine you have three items in a container...
This is one potential way to arrange them
Here's another
And another
And another
And another
What About
Going Vertical?!
flex-direction
flex-wrap
justify-content
align-items
order
flex
align-self
align-content
flex-grow
flex-shrink
A STARTING POINT
Four DIV's Inside A Container DIV
FIRST BIT OF MAGIC
.container {
display: flex;
}
SOME TERMINOLOGY
Main Axis
Cross Axis
flex container
flex item
flex item
specifies how items are placed in the flex container, defining the main axis and its direction
flex-direction
Main Axis
Cross Axis
flex-direction: row;
flex-direction
flex-direction: row-reverse;
Main Axis
Cross Axis
flex-direction: row-reverse;
flex-direction
flex-direction: row-reverse;
Main Axis
Cross Axis
flex-direction: column;
flex-direction
flex-direction: row-reverse;
Main Axis
Cross Axis
flex-direction: column-reverse;
specifies whether items are forced into a single line OR can be wrapped into multiple lines
Let's make our boxes much larger
.box {
width: 300px;
}
WHAT IS GOING ON?? THOSE AREN'T 300px!
Add flex-wrap to our flex container
.container {
flex-wrap: wrap;
}
There we go!
wrap-reverse reverses the start/end of the cross axis
.container {
flex-wrap: wrap-reverse;
}
Cross Axis
Defines how space is distributed between items in flex container
Along the main axis
.container {
justify-content: flex-end;
}
.container {
justify-content: center;
}
.container {
justify-content: space-between;
}
.container {
justify-content: space-around;
}
For a very tiny, non-scary exercise
Defines how space is distributed between items in flex container
Along the cross axis
.container {
align-items: flex-start;
}
Cross Axis
.container {
align-items: flex-end;
}
Cross Axis
.container {
align-items: stretch;
}
Cross Axis
.container {
align-items: center;
}
Cross Axis
.container {
align-items: baseline;
}
Cross Axis
Defines how space is distributed BETWEEN ROWS in flex container
Along the cross axis
.container {
align-content: stretch;
}
Cross
Axis
.container {
align-content: flex-start;
}
Cross
Axis
.container {
align-content: flex-end;
}
Cross
Axis
.container {
align-content: space-between;
}
Cross
Axis
.container {
align-content: space-around;
}
Cross
Axis
.container {
align-content: center;
}
Cross
Axis
flex-direction
flex-wrap
justify-content
align-items
order
flex
align-self
align-content
flex-grow
flex-shrink
flex-basis
allows you to override align-items on individual flex items
.container {
align-items: flex-start;
}
.box-2 {
align-self: flex-end;
}
.container {
align-items: center;
}
.box-2, .box-3 {
align-self: flex-start;
}
.container {
align-items: flex-end;
}
.box-3 {
align-self: stretch;
}
order
specifies the order used to lay out items in their flex container
1
2
3
4
order
assign it to the individual items
1
2
3
4
.box-1 {
order: 2
}
1
2
3
4
WHAT?!
😱
1
2
3
4
.box-3 {
order: -1
}
1
2
3
4
order
to make box-1 second in line...
1
2
3
4
.box-1 {order: 2;}
.box-2 {order: 1;}
.box-3, .box-4 {order: 3;}
Defines how a flex item will grow or shrink to fit the available space in a container
(it's actually a shorthand property for 3 other properties)
flex: Â
   <'flex-grow'>
     <'flex-shrink'>
        <'flex-basis'>
Sort of like width, but not.
Specifies the ideal size of a flex item BEFORE it's placed into a flex container.
Dictates how the unused space should be spread amongst flex items
.box{
flex-grow: 1;
}
To make all boxes share space evenly..
.box{
flex-grow: 1;
}
.box-2{
flex-grow: 2;
}
.box{
flex-grow: 9;
}
.box-2{
flex-grow: 18;
}
100px
100px
800px
1
2
box-1, box-2 {width: 100px}
500px
500px
1
2
box-1, box-2 {
width: 100px;
flex-grow: 1;
}
260px
740px
1
2
box-1, box-2 {width: 100px};
box-1 {
flex-grow: 1;
}
box-2 {
flex-grow: 4;
}
Not 4 times as large!
Dictates how items should shrink when there isn't enough space in contanier
flex: Â
   <'flex-grow'>
     <'flex-shrink'>
        <'flex-basis'>
The Holy Grail Layout
At least 800px
The Holy Grail Layout
640px
breakpoint