DEV Community

Cover image for Complete Flexbox Tutorial w/ Cheat Sheet
Joy Shaheb
Joy Shaheb

Posted on Edited on Originally published at freecodecamp.org

Complete Flexbox Tutorial w/ Cheat Sheet

Let's refresh Our CSS Flexbox Memory. Here's a Tutorial & Cheat Sheet of everything you can do with CSS flexbox. Let's Go 🎖ïļ

The original Article at FreeCodeCamp

Table of Contents --

You can watch this tutorial on YouTube as well if you like:

FlexBox Architecture

So how does Flexbox architecture work? The flex-items [Contents] are distributed along the Main Axis and Cross Axis. And, depending on the flex-direction property, the layout position changes between rows and columns.

Flex Box model Architecture

FlexBox Chart --

This chart contains every possible property and value you can use when using flexbox. You can reference it while doing the project and experiment with different values.

Flex Box property Value Chart

How to Set Up the Project

Alt Text

For this project, you need to know little bit of HTML, CSS, and how to work with VS code. Follow along with me ->

  1. Create a folder named "Project-1" & Open VS Code
  2. Create index.html & style.css files
  3. Install Live Server & Run Live Server.

Or, you can just open Codepen & start coding.

At the end of this tutorial, you can make website layouts more accurately.

HTML

In HTML, write these lines of code inside the body tag 👇

<div class="container">
    <div class="box-1"> A </div>
    <div class="box-2"> B </div>
    <div class="box-3"> C </div>
</div>
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

CSS

target the .container class & all the boxes. Then style the boxes so that all of them looks similar. Like this 👇

Note : don't forget to put the height of the container.

.container{
   height : 100vh;
}

[class ^="box-"]{
    width: 140px;
    height: 140px;
    background-color: skyblue;
    border: 2px solid black;

// To view the letter better
    font-size: 65px;
}
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

But Wait....

Alt Text

Before starting, you need to understand the relationship between parent & child classes.

Alt Text

Flexbox works on the parent class, not on the children classes.

Here, the .container class is the parent & our .box-* classes are our children.

so, apply the display: flex inside .container class. And place the letters at the center of the box like this ->

.container{
    display : flex;
    height : 100vh;

// To place some gap between boxes
    gap : 25px;
}
[class ^="box-"]{
// Code from previous step are here

// Placing text at center 
    display : flex;
    justify-content : center;
    align-items : center;
}
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

And.... we're all set! Let's start coding. 😊

Alt Text

flex-direction

The Direction/Orientation in which flex-items are distributed inside flex-container.

Flex Direction

Flex Direction

To recreate these results, Let's write on CSS These lines ->

Note : We'll write inside .container class

.container{
//code from setup stage are here

// Change the value  👇 here to see results
    flex-direction : row;
}
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

justify-content

This property arranges flex-items along the MAIN AXIS inside the flex-container

justify content

justify content

To recreate these results, Write on CSS These lines ->

.container{
//code from setup stage are here

//  Change the value  👇 here to see results
    justify-content: flex-start;
}
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

align-content

This property arranges flex-items along the CROSS AXIS inside the flex-container. This is similar to justify-content

align content

align content

Please note that, without the flex-wrap property, this property doesn't work, here's the demo ->

.container{

//  Change the value  👇 here to see results
    align-content: center;

// without this line, align-content won't work
    flex-wrap: wrap;
}
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

align-items

This property distributes Flex-items along the Cross Axis

align items

To recreate these results, Let's write on CSS ->

.container{
//code from setup stage are here

// Change the value 👇 here to see results
    align-items: flex-end;
}
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

align-self

This property works on the children classes. It positions the selected item along the Cross Axis

align self

In total we have 6 values

  • flex-start
  • flex-end
  • center
  • baseline
  • stretch
  • auto

To recreate the results, select any .box-* & write

.box-2{
// Change the value 👇 here to see results
     align-self : center;
}
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

flex - grow | shrink | wrap | basis

The properties that we're discussing right now will work when we resize the window. Let's dive right in.

  • flex-grow : grows the size of a flex-item based on width of the flex-container.
  • flex-shrink : The ability for a flex item to shrink based on width of the flex-container. Opposite of flex-grow.

flex grow flex shrink flex wrap

To achieve these results, follow me ->

Please Note : flex-grow & flex-shrink works on children classes. So, we will target all our boxes ->

.box-1{
    flex-grow: 1;
}
.box-2{
    flex-grow: 5;
}
.box-1{
    flex-grow: 1;
}
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

Resize the window & see the results.

To duplicate result of flex-shrink, write ->

Please note : Delete the flex-wrap property, otherwise it won't work.

.box-1{
    flex-shrink: 1;
}
.box-2{
    flex-shrink: 5;
}
.box-1{
    flex-shrink: 1;
}
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

Now, Resize the window & see the results.

  • flex-wrap : Amount of Flex-item you want in a line/row.

flex wrap flex grow flex shrink

This works on the .container parent class. So, write ->

.container{
    //other codes are here 

// Change value 👇 here to see results
    flex-wrap : wrap;
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
  • flex-basis : This is similar to adding width to a flex-item, but only more flexible. flex-basis: 10em; it will set the initial size of a flex-item to 10em. Its final size will be based on available space, flex-grow, and flex-shrink.

Short Hands

Alt Text

  • flex : It is a shorthand to flex-grow, flex-shrink and flex-basis combined.

flex flex basis

You can try this by writing ->

Please Note : It only works on the children classes

.box-2{
    flex : 2 1 30em;
}
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
  • flex-flow : Short hand to flex-direction & flex-wrap

Alt Text

You can try this by writing ->

Please Note : It only works on the parent class

.container{
    flex-flow : row wrap;
}
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

place-content

This is the shorthand of justify-content & align-content

Alt Text

Let's duplicate the results

Note : It works on the parent class

.container{
    place-content : center flex-end;
}
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

Credits

Kitty Avatars

Credits

Read Next :

Conclusion

Here's Your Medal For reading till the end âĪïļ

Suggestions & Criticisms Are Highly Appreciated âĪïļ

Alt Text

Alt Text

Top comments (81)

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
cadams profile image
Chad Adams • • Edited

This tool is awesome. Makes flexbox easy to understand. I use it alot.
demos.scotch.io/visual-guide-to-cs...

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
jamesthomson profile image
James Thomson •
🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
jamesthomson profile image
James Thomson •

This is also good for beginners to see how common components can be structured. flexboxpatterns.com/

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” Thread
 
ca969 profile image
ca969 •

Wow, that's a really good resource. Thank you for sharing.

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” Thread
 
joyshaheb profile image
Joy Shaheb •

Most welcome âĪïļ

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
joyshaheb profile image
Joy Shaheb •

A legend made this software, thank you for sharing this 🏅🙏

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
kyril96340413 profile image
Kyril •

Thanks

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
ronaldohoch profile image
Ronaldo Hoch •

Niiiice!
Why not this is a PWA?

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
madza profile image
Madza •

nice visuals, may I ask what you used? 👀

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
joyshaheb profile image
Joy Shaheb •

I took Images from Flaticon.com
I gave them credits before conclusion part

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
madza profile image
Madza •

Awesome, thanks! 🙏âĪ

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
yabasha profile image
Bashar Ayyash •

Can you please tell me whats the name of icons pack you used? I am a member at Flaticon, I liked the icons you used.

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
qq449245884 profile image
qq449245884 •

Dear Joy Shaheb may I translate your article into Chinese?I would like to share it with more developers in China. I will give the original author and original source.

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
joyshaheb profile image
Joy Shaheb •

Sure. Best of luck âĪïļðŸŽ–ïļ

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
qq449245884 profile image
qq449245884 •

Thank You Very Much

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
mbustosp profile image
Matías Bustos •

Really good job! Thank you. It would be amazing if you could do something similar about Grids 😊

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
joyshaheb profile image
Joy Shaheb •

Thank you for the well wishes. I'll think about Grids in future. Till then, stay tuned âĪïļ

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
kyril96340413 profile image
Kyril •

Thank you for sharing this.

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
joyshaheb profile image
Joy Shaheb •

Most welcome âĪïļ

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
rehan124 profile image
Rehan Jamali •

Thank you sir... that's cool..but could I download or save it offline?? Bcz I have some Net issues

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
joyshaheb profile image
Joy Shaheb •

Sure,

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
rehan124 profile image
Rehan Jamali •

How could I download it offline?

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” Thread
 
joyshaheb profile image
Joy Shaheb •

Click on the image. Then download/save it/ take screenshots

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
nordyj profile image
Jamie Nordmeyer •

Awesome reference. Thanks for this!

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
alfreddagenais profile image
Alfred Dagenais •

Wow, very simple to understand with pictures!! 😍 Finally, I'll get all info easily with flexbox! Thank

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
joyshaheb profile image
Joy Shaheb •

Most welcome âĪïļ

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
dnjosh10 profile image
Dumte Nwidoobee Joshua •

This tool makes me feel happy and confident. When next I will use my flexbox now, no more struggling with positioning child elements

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: joyshaheb/flexbox cheat sheets in css edl - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
afif profile image
Temani Afif •

note that order can be used with negative values