Learn and Master Flexbox by building commonly used web components
Tags: javascript, webdev, tutorial, beginners
Published: 2021-06-17 04:16:15

Hello, welcome to this tutorial. Today, I will be explaining how to build commonly used web components with Flexbox. 

After the introduction, I will walk you through building the following:
*  A navigation bar
*  A contact card 
*  A credit card checkout page

So let's jump right into it! 🚀

<img width="100%" style="width:100%" src="https://media.giphy.com/media/YTbZzCkRQCEJa/giphy.gif"/>

## Introduction

Flexbox model provides an efficient way to layout, align and arrange elements on a webpage. Learning flexbox may seem to be a pain in the butt at first for some people, but once you get familiar with it, applying it will be a harmless and enjoyable ride. 
I also use flexbox in all my projects and it's been awesome!

Let's learn some rudiments:

### 1. display: flex
Don't panic! 😨 This is a CSS attribute that you assign to the parent container in order to use flexbox. For instance, if you have
```
<main id="parent">
        <div class="child"></div>
        <div class="child"></div>
        <div class="child"></div>
        <div class="child"></div>
    </main>
```

 The "main" tag is the parent element, also known as the flex container. 
Note that, this attribute aligns the children elements inline by default, that is lay them side by side (from left-right). The children can also be referred to as flex items.

![Flexbox](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b26zr3xzjf7i0lki7qsq.png)

### 2. flex-direction : row || flex-direction : column || flex-direction : column-reverse || flex-direction : row-reverse
I guess this is self-explanatory already. 

Flex-direction : row - This is the default arrangement of flex-items, when display : flex is assigned to the parent element.

![Flexbox - flex-direction : row](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b26zr3xzjf7i0lki7qsq.png)

Flex-direction : column  - The flex-items (children elements) are arranged from top to bottom within the parent element.

![Flexbox - flex-direction : column](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8dt2ul78tom6em5qv2ne.png)

Flex-direction : row-reverse - In this case, the children elements are still arranged from left-right, but the arrangement of the flex items begins from last flex-item to first flex-item.

![Flexbox - flex-direction : row-reverse](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0s33ip3cv6sj0cgzgqng.png)

Flex-direction : column-reverse - The flex-items are in column, but in a reverse order, similar to the row-reverse.

![Flexbox - flex-direction : column-reverse](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7lurqeap59a2or73stj.png)


### 3. justify-content : flex-start || justify-content : flex-end || justify-content : center || justify-content : space-between || justify-content : space-around 

If you are already familiar with the float attribute, justify-content is quite similar to the float attribute but with some added functionalities.

justify-content : flex-start - this attribute aligns the flex items to the left axis within the parent element.

![Flexbox - justify-content : flex-start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wq16lk0fvqxe7u6bo112.png)

justify-content : flex-end - this aligns the flex items to the right axis within the parent element.
![Flexbox - justify-content : flex-end](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/og17jzmi53n69j5o4y5v.png)

justify-content : center - this aligns the flex items at the center of the parent element.

![Flexbox - justify-content : center](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdzydsfwfyat83y4u9qm.png)
justify-content : space-between - aligns the flex items such that equal amount of space is available BETWEEN them.

![Flexbox - justify-content : space-between](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wn21xff6hx8l8io29r22.png)

justify-content : space-around - aligns the flex items such that equal amount of space is available AROUND them.

![Flexbox - justify-content : space-around](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzhzsap7yg5igs0yx19f.png)

### 4. align-items : flex-start || align-items : center || align-items : flex-end

They are similar to justify-content, the difference is that justify-content align elements on the horizontal axis, while align items align elements vertically.

align-items : flex-start
![Flexbox - align-items : flex-start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtf2bbrbdsfcboiodpmu.png)

align-items : center 
![Flexbox - align-items : center](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7pmradae6xva10t6b5jf.png)

align-items : flex-end
![Flexbox - align-items : flex-end](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwy90zb2ec2ppzkdogdm.png)

> TLDR : The properties listed are limited to those needed to complete the projects listed in this tutorial. You can learn about other attributes [here](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) or [here](https://www.w3schools.com/css/css3_flexbox.asp)

## Let's Build! 🚀

<img width="100%" style="width:100%" src="https://media.giphy.com/media/kyLYXonQYYfwYDIeZl/giphy.gif"/>

From what you've learnt so far, try out this web layouts
#### 1. A simple check out page

![A checkout page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3lduxcyslq9cc7v6ck0.png)

Check [Solution](https://codepen.io/arshadayvid/pen/JjWxZoM)

#### 2. A contact page
![A contact page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/okyeba5edum6nr58x3ev.png)

Check [Solution](https://codepen.io/arshadayvid/pen/VwpgdeG)
#### 3. A Navigation bar
![Navigation Bar](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjjxspij5i2jjsww7bax.png)

Check [Solution](https://codepen.io/arshadayvid/pen/qBrgKZY)

> Did you enjoy this tutorial or have any questions? Feel free to drop your comments below and connect with me on [Twitter](https://twitter.com/Arshadayvid)

<img width="100%" style="width:100%" src="https://media.giphy.com/media/kfvEdnjjcAoea0naii/giphy.gif" />