Flexbox helps you to align elements. It’s much easier and better than Float etc
Flexbox is generally used for small-scale layouts, whereas Grid is used for larger stuff.
![CSS Flexbox for Beginners [2022] CSS Flexbox for Beginners [2022]](https://m5.paperblog.com/i/726/7264521/css-flexbox-for-beginners-2022-L-TKYXib.png)
Flexbox has 2 axes. The main axes and the cross axes.
The main axis is defined by the flex-direction property.
The cross axis will automatically run perpendicular to the main axis.
![CSS Flexbox for Beginners [2022] CSS Flexbox for Beginners [2022]](https://m5.paperblog.com/i/726/7264521/css-flexbox-for-beginners-2022-L-cNytZn.png)
Flexbox Main Axis
The main axis is defined by flex-direction.
Flex-direction has 4 possible values:
- Row
- Row-reverse
- Column
- Column-reverse
![CSS Flexbox for Beginners [2022] CSS Flexbox for Beginners [2022]](https://m5.paperblog.com/i/726/7264521/css-flexbox-for-beginners-2022-L-gLBXVn.png)
In the screenshot below,
display:flex makes container-1 a flex item
flex:1 – for each of the boxes, makes them evenly distributed in terms of their width. As all the flex items are given a value of 1, they are all the same size.
![CSS Flexbox for Beginners [2022] CSS Flexbox for Beginners [2022]](https://m5.paperblog.com/i/726/7264521/css-flexbox-for-beginners-2022-L-TO48ze.png)
If you were to change the flex value for .box-1 to 4, then box-1 would take up four-sixths (4/6) of the page:
![CSS Flexbox for Beginners [2022] CSS Flexbox for Beginners [2022]](https://m5.paperblog.com/i/726/7264521/css-flexbox-for-beginners-2022-L-X1amqt.png)
Change the order of Flex Items
With flexbox you can change the order of the flex-items, (the boxes) without changing the HTML.
To do this use the “order” property.
The code below will put box-1 in the second position, left to right.
![CSS Flexbox for Beginners [2022] CSS Flexbox for Beginners [2022]](https://m5.paperblog.com/i/726/7264521/css-flexbox-for-beginners-2022-L-D7aIPX.png)
Flex-direction: Column
By giving the container the flex-direction property, with a value of “column”, the boxes / flex items will stack on top of each other
![CSS Flexbox for Beginners [2022] CSS Flexbox for Beginners [2022]](https://m5.paperblog.com/i/726/7264521/css-flexbox-for-beginners-2022-L-9btMVy.png)
Justify-Content
You can use the “justify-content” property to align the boxes within the flex container.
justify-content: flex-end; will push all the flex items to the right
![CSS Flexbox for Beginners [2022] CSS Flexbox for Beginners [2022]](https://m5.paperblog.com/i/726/7264521/css-flexbox-for-beginners-2022-L-IlAUjY.png)
Justify-content:center; will place the boxes in the center of the container
![CSS Flexbox for Beginners [2022] CSS Flexbox for Beginners [2022]](https://m5.paperblog.com/i/726/7264521/css-flexbox-for-beginners-2022-L-663jRX.png)
Justify-content:space-between;
Aligns the content spaced evenly across the entire width of the container, with margins in between:
![CSS Flexbox for Beginners [2022] CSS Flexbox for Beginners [2022]](https://m5.paperblog.com/i/726/7264521/css-flexbox-for-beginners-2022-L-wpi1vj.png)
Space-around will add some “space around” the items, so that they have margins in between the items and also to the sides of the left-most and right-most items:
![CSS Flexbox for Beginners [2022] CSS Flexbox for Beginners [2022]](https://m5.paperblog.com/i/726/7264521/css-flexbox-for-beginners-2022-L-JdnBdr.png)
Flex Grow
If after the dimensions of the flex-items have been set, they leave room or space, you can use the “flex-grow” property.
If you give each flex item a flex grow value of 1, then the items will take up an equal amount of the remaining space.
It could be 1, or 100, it won’t make a difference if all the items have the same value.
Below, flex item “one”, is the only flex item with a “flex grow” property, and it therefore takes up all the space that was left over from left to right by the 3 boxes.
![CSS Flexbox for Beginners [2022] CSS Flexbox for Beginners [2022]](https://m5.paperblog.com/i/726/7264521/css-flexbox-for-beginners-2022-L-effoSZ.png)