Flexbox Tutorial

See the freeCodeCamp article for a follow along: Learn basic Flexbox in just 10 minutes

Level 1 — Basic


1) Creating a flex container

{ display: flex; }

Luke, I am your father.

Div One

Cookie! Cookie! Cookie!

Div Two

With our powers combine, we are captain planet!

Div Three

2) Put flex items in a single column

{ flex-direction: column; }

Luke, I am your father.

Div One

Cookie! Cookie! Cookie!

Div Two

With our powers combine, we are captain planet!

Div Three

{ flex-direction: column-reverse; }

Luke, I am your father.

Div One

Cookie! Cookie! Cookie!

Div Two

With our powers combine, we are captain planet!

Div Three

Level 2 — Beginner


1) Align flex items to the right

{ justify-content: flex-end; }

You must unlearn what you have learned.

Div One

You name it! I hate it!

Div Two

Cowabunga dudes!

Div Three

2) Center-align flex items

{ justify-content: center;}

You must unlearn what you have learned.

Div One

You name it! I hate it!

Div Two

Cowabunga dudes!

Div Three

3) Spread out flex items

{ justify-content: space-evenly; }

You must unlearn what you have learned.

Div One

You name it! I hate it!

Div Two

Cowabunga dudes!

Div Three

{ justify-content: space-between; }

You must unlearn what you have learned.

Div One

You name it! I hate it!

Div Two

Cowabunga dudes!

Div Three

justify-content: space-around;

You must unlearn what you have learned.

Div One

You name it! I hate it!

Div Two

Cowabunga dudes!

Div Three

4) Align flex items in a second direction

{ justify-content: center; align-items: center; }

You must unlearn what you have learned.

Div One

You name it! I hate it!

Div Two

Cowabunga dudes!

Div Three

5) Align a particular flex item

.flex-container10 {justify-content: center; align-items: center;}

.three10 {align-items: flex-end};

You must unlearn what you have learned.

Div One

You name it! I hate it!

Div Two

Cowabunga dudes!

Div Three

Level 3 — Intermediate


1) Allow flex items to wrap into other rows/columns

{ flex-wrap: wrap; }

Bleep! Bleep!

Div One

Elmo's world! La! La! La! La!

Div Two

I have the power!

Div Three

2) Reverse wrapping

{ flex-wrap: wrap-reverse; }

Bleep! Bleep!

Div One

Elmo's world! La! La! La! La!

Div Two

I have the power!

Div Three

3) Justify position of lines of elements

{ flex-wrap: wrap; align-content: flex-start; }

Bleep! Bleep!

Div One

Elmo's world! La! La! La! La!

Div Two

I have the power!

Div Three

Level 4 — Advanced


1) Expand elements

.flex-item:nth-of-type(1){ flex-grow: 1;}

.flex-item:nth-of-type(2) { flex-grow: 2;}

No!!!!!!!!No!!!

Div One

One! Two! The number two! Ha aha ha ha!

Div Two

Da-da-da! Daaa!

Div Three

2) Shrink elements

.flex-item:nth-of-type(1){ flex-shrink: 1;}

.flex-item:nth-of-type(2) { flex-shrink: 2;}

No!!!!!!!!No!!!

Div One

One! Two! The number two! Ha aha ha ha!

Div Two

Da-da-da! Daaa!

Div Three

3) Set the size of elements

.flex-item:nth-of-type(1){ flex-basis: 200px;}

.flex-item:nth-of-type(2) { flex-basis: 10%;}

No!!!!!!!!No!!!

Div One

One! Two! The number two! Ha aha ha ha!

Div Two

Da-da-da! Daaa!

Div Three

4) Put flex-grow, flex-shrink, and flex-basis together

.flex-item:nth-of-type(1) { flex: 1 0 100px; }

.flex-item:nth-of-type(2) { flex: 2 0 10%; }

No!!!!!!!!No!!!

Div One

One! Two! The number two! Ha aha ha ha!

Div Two

Da-da-da! Daaa!

Div Three