Introduction to CSS Flexbox

Contributorscontributors pic

In this article, I'll teach you CSS Flexbox basics so you can make your own responsive sites. I'll explain how each of Flexbox's properties work, and I'll give you a cheatsheet that covers everything you can do with Flexbox. Let's Go 🎖️

First, What is Flexbox?

what is flexbox

When you're building a house, you need a blueprint. In the same way, we need a blueprint when we're making websites. And Flexbox is the blueprint.

The Flexbox model allows us to layout the content of our website. Not only that, it helps us create the structures needed for creating responsive websites for multiple devices.

Here's a demo of one project using Flexbox as the main blueprint.

what is flexbox

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.

what is flexbox

Flexbox Chart

This chart contains every possible property and value you can use when you're working with Flexbox. You can reference it while doing your projects and experiment with different values.

what is flexbox

How to Set Up the Project

For this project, you need to know little bit of HTML, CSS, and how to work with VS code. Follow along with me as we complete the following tasks:

1. Create a folder named "Project-1" & Open VS Code

2. Create index.html and style.css files

3. Install Live Server and run it.

Or, you can just open Codepen and start coding.

At the end of this tutorial, you will be able to make accurate and beautiful website layouts.

a. HTML

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

Expand
Wrap
Lines
HTML

b. CSS

Target the .container class and all the boxes. Then style the boxes so that all of them look similar, like this: 👇

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

Expand
Wrap
Lines
HTML

But Wait.....

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

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

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

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

Expand
Wrap
Lines
HTML

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

1. flex-direction property

This property allows us to set the direction and orientation in which our flex-items should be distributed inside the flex-container.

To recreate these results, let's write these lines in our CSS:

Please note that we'll write them inside the .container class.

Expand
Wrap
Lines
HTML

CSS Demo: flex-direction

flex-direction: row;flex-direction: row-reverse;flex-direction: column;flex-direction: column-reverse;
1
2
3
4

2. justify-content property

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

jcjc1

To recreate these results, write these lines in your CSS:

Expand
Wrap
Lines
HTML

CSS Demo: justify-content

justify-content: flex-start;justify-content: flex-end;justify-content: center;justify-content: space-between;justify-content: space-around;justify-content: space-evenly;
1
2
3
4

3. align-content property

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

acac1

Please note that without the flex-wrap property, this property doesn't work. Here's a demo:

Expand
Wrap
Lines
HTML

CSS Demo: align-content

align-content: flex-start;align-content: flex-end;align-content: center;align-content: space-between;align-content: space-around;align-content: space-evenly;
1
2
3
4
5
6
7
8
9
10

4. align-items property

This property distributes Flex-items along the Cross Axis.

ai

To recreate these results, let's write the following code in CSS:

Expand
Wrap
Lines
HTML

CSS Demo: align-items

align-items: flex-start;align-items: flex-end;align-items: center;align-items: stretch;align-items: baseline;
1
2
3
4

5. align-self property

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

as

In total we have 6 values:

1. flex-start

2. flex-end

3. center

4. baseline

5. stretch

6. auto

To recreate the results, select any .box-* and write the following code:

Expand
Wrap
Lines
HTML

CSS Demo: align-self

align-self: flex-start;align-self: flex-end;align-self: center;align-self: stretch;align-self: baseline;align-self: auto;
1
2
3
4

Take a Break

So far so good. Take a break!

brk

6. flex - grow | shrink | wrap | basis properties

The properties we'll discuss now will work when we resize the window. Let's dive right in.

a. flex grow

This property grows the size of a flex-item based on the width of the flex-container.

b. flex-shrink

This property helps a flex item shrink based on the width of the flex-container. It's the opposite of flex-grow.

fgs

To achieve these results, follow me.

Please note that flex-grow and flex-shrink work on child classes. So, we will target all our boxes like this:

Expand
Wrap
Lines
HTML

CSS Demo: flex-grow

flex-grow: 1;flex-grow: 0;
1
2
3
4

Resize the window and you'll see the results.

To duplicate the result of flex-shrink, write the following code:

Please note that you need to delete the flex-wrap property first, otherwise it won't work.

Expand
Wrap
Lines
HTML

CSS Demo: flex-shrink

flex-shrink: 1;flex-shrink: 0;
1
2
3
4

Now, resize the window and you'll see the results.

c. flex-wrap

This property helps you set the number of flex-items you want in a line or row.

fw

This works on the .container parent class. So, write the following code:

Expand
Wrap
Lines
HTML

CSS Demo: flex-wrap

flex-wrap: no-wrap;flex-wrap: wrap;flex-wrap: wrap-reverse;
1
2
3
4
5
6
7
8
9
10

d. flex-basis

This is similar to adding width to a flex-item, but only more flexible. flex-basis: 10em, for example, will set the initial size of a flex-item to 10em. Its final size will be based on the available space, flex-grow, and flex-shrink.

CSS Demo: flex-basis

flex-basis: 0;flex-basis: auto;flex-basis: 200px;
1
2
3
4

7. Shorthand Flexbox Properties

Time to Investigate Flexbox Shorthands

a. flex

This is the shorthand for the flex-grow, flex-shrink and flex-basis properties combined.

fs

You can try this by writing the following code:

Please note that it only works on the child classes:

Expand
Wrap
Lines
HTML

CSS Demo: flex

flex: 1;flex: 2;flex: 1 30px;flex: 1 1 100px;
1
2
3
4

b. flex-flow

This is the shorthand for the flex-direction and flex-wrap properties:

ff

You can try this by writing the following code:

Please note that it only works on the parent class.

Expand
Wrap
Lines
HTML

CSS Demo: flex-flow

flex-flow: row wrap;flex-flow: row-reverse nowrap;flex-flow: column wrap;flex-flow: column wrap-reverse;
1
2
3
4
5
6
7
8

c. place-content

This is the shorthand for the justify-content and align-content properties:

pc

Let's duplicate the results:

Please note that it works on the parent class.

Expand
Wrap
Lines
HTML

CSS Demo: place-content

place-content: end space-between;place-content: space-around start;place-content: start space-evenly;place-content: end center;place-content: end;
1
2
3
4
5

More Resources

If you want to exercise your Flexbox knowledge, you can read this article of mine where you'll be building five responsive layouts using Flexbox. Here's a demo:

demo

Conclusion

Here's your medal for reading till the end ❤️

letslearntogethermern365daysofcontentcssflexbox
profile
Mumbai, India

I am a MERN Stack Developer and I love to code.

Discussion

Write
Preview

More Shows

intro pic
Blog

How to create a Responsive Website

Exaplaination about how to create a responsive website

picc

Suraj - 3 min read

intro pic
Blog

Free Resources For Your Web Developement Career

Uploaded so many free resource for your web developement career

picc

Suraj - 3 min read

intro pic
Blog

12 Steps You Need To Take Before Deploying ...

Explained 12 steps you nened to tak before deploying your website

picc

Suraj - 3 min read

intro pic
Blog

How to Become a Front-End Developer?

Exaplained how we can become a Fron-Eend developer

picc

Suraj - 3 min read

pic

Suraj Ambekar

@surajambekar

I am a MERN Stack Developer and I love to code.

Tags

letslearntogethermern365daysofcontentcssflexbox

More on Showwcase

pic

Suraj Ambekar

How to Become a Front-End Developer?

636 views
pic

Suraj Ambekar

12 Steps You Need To Take Before Deploying Your Website

346 views
pic

Suraj Ambekar

Free Resources For Your Web Development Career

342 views
pic

Suraj Ambekar

How to create a Responsive Website

590 views
pic

Suraj Ambekar

Introduction to JavaScript

337 views