CSS Flex-Box Most Used Properties
Tags: css, design
Published: 2021-07-19 07:58:05

Hello, In this article I will try to explain the most used properties of CSS Flexbox by myself.

### What is CSS Flexbox? 
-> Flexbox is a one-dimensional layout model which means that you can control only one direction at a single time either row or column. This allows you to align the flex items easily in different styles and layouts.

### How to Use Flexbox?
-> To use flex property on child elements you need to make the display of parent element flex.

This is Our Sample Html Code with One Parent Div and 4 Children Div
```
        <div class="flex-container">
            <div class="flex-item child-1">I am first Child of Flex</div>
            <div class="flex-item child-2">I am Second Child of Flex</div>
            <div class="flex-item child-3">I am Third Child of Flex</div>
            <div class="flex-item child-4">I am Fourth Child of Flex</div>
        </div>
``` 

This is Our Sample Css Code ( Without Flex ) which gives a better look to learn 😁
```
.flex-container {
  padding: 1em;
  background-color: palegreen;
  border-radius: 12px;
  border: 1px solid black;
}

.flex-item {
  background-color: plum;
  margin: 0.5em;
  padding: 0.5em;
  border-radius: 12px;
  border: 1px solid black;
}
``` 

This is how Our Code Looks !!
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626678095634/7-S_knALG.png)

### Let's Make this Flexbox now ⚡

```
.flex-container {
  padding: 1em;
  background-color: palegreen;
  border-radius: 12px;
  border: 1px solid black;
  /* This line will make the parent div to flexbox */
  display: flex;
}
``` 

Our box will display like this now !! This is because by default the flex-direction is row.
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626678065415/JgKzwXvVJ.png)


### Flex-Direction Property 
<hr>
This will arrange all elements vertically. 
```
flex-direction: column;
```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626678407810/Bn9-LlOev.png)
<hr>
<hr>
This will arrange all elements vertically but in reverse order.
```
flex-direction: column-reverse;
```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626678425809/hqtK2QYeb.png)
<hr>
<hr>
This will arrange all elements horizontally but in reverse order. 
```
flex-direction: row-reverse;
```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626678452033/PYrol6ikN.png)
<hr>

Let's Keep Flex Direction as Row which is by default and see some other important properties 😄

### Flex Wrap 
```
.flex-container {
  padding: 1em;
  background-color: palegreen;
  border-radius: 12px;
  border: 1px solid black;
  /* This line will make the parent div to flexbox */
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}
```
This property will help elements to wrap themselves when the viewport size decreases. There are two major types of wrap that is a wrap & wrap reverse workflow is the same just a small difference as in row & row reverse.

This will give you a better idea of how Wrap works 
![ezgif.com-gif-maker(3).gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1626679191490/Pb3Ez-0XQ.gif)

### Justify Content 
This is a way to arrange the children along the main axis. 
<hr>
```
  justify-content: center;
```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626679394725/CHxUUN9hg.png)
<hr>
```
  justify-content: flex-start;
```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626679427699/wZghNURDX.png)
<hr>
```
  justify-content: flex-end;
```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626679448038/_53amyRvl.png)
<hr>
```
  justify-content: space-around;
```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626679484587/d5Af1C8q0.png)
<hr>
```
  justify-content: space-evenly;
```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626679505883/idWBwRRPl.png)
<hr>
```
justify-content: space-between;
```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626679528476/0ZrillnD3.png)
<hr>

By seeing all this layout you can understand which property to use when !! 

### Align Item 
Let's give Our parent div some height so we can check how this property align the elements vertically.
<hr>
```
align-items: center;
```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626680052965/cSZmDBd8w.png)
<hr>
```
align-items: flex-start;
```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626680074391/JheoDr7Aa.png)
<hr>
```
align-items: flex-end;
```
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626680096511/fM9HxNdIMk.png)
<hr>

### Align Self 
This is a property you Will apply on a Child element that will align that particular child as you set.

### Golden Tip 🥇 
With just 3 lines you can center the whole content !! Don't get how to do it? Ok let me tell you 😁
```
.flex-container {
  display: flex;
  justify-content: center;
  align-items: center;
}
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626680527320/MVdVuh5_EY.png)


### Conclusion 
I think now you might get an idea of how the flexbox is useful. I hope that this article will help you in using flexbox confidently without hit and trial !! There are also many other properties of flexbox but as the name of the article says Most Used properties I feel that these are the properties that are used mostly by every developer! 

Wanna get connected with me 😄
[Twitter](https://twitter.com/rohan_2502) 
[LinkedIn](https://www.linkedin.com/in/rohan-k-2502/) 

Recently I also have started a Telegram channel If you like this content please consider checking my Channel 
[Telegram](https://t.me/rohankulkarnichannel)