# Flexbox and its Properties

Flexbox(flexible box layout) is a CSS layout model used to arrange HTML elements in rows or columns and control their alignment, spacing and distribution. It is apply on Parent element & childrens are obey the parent.

## Flexbox Main Axis and Cross Axis Diagram

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/6f73e7c7-5425-4285-8d04-944e0eebe5e3.png align="center")

The diagram explains the Main Axis, Cross Axis, Main Start, Main End, Cross Start and Cross End in CSS Flexbox.

It is one of the most important concept to understand before learning `justify-content` and `align-items`.

1. **Understanding the Diagram**

- **Main Axis(Green Line):** The Primary direction in ehich flex items are [arranged.it](http://arranged.it)'s direction is from left to right.
- **Cross Axis(Purple Line):** The direction perpendicular to the main [axis.it](http://axis.it)'s direction is from top to bottom.

2. **Explanation of all six terms**

| Term | Meaning |
| --- | --- |
| Main Axis | Direction in which flex items are arranged |
| Cross Axis | Direction perpendicular to the main axis |
| Main Start | Starting point of the main axis |
| Main End | Ending point of the main axis |
| Cross Start | Starting point of the cross axis |
| Cross End | Ending point of the cross axis |

**Let discuss all the properties of of flexbox**

1. `display:flex`

It is arrange all the childrens on the main axis from left to right. it activates flexbox on an element and makes it an flex container.

Before write `display:flex`

**HTML File**

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
 <div id="parent">
 <p class="child child-1">1</p>
 <p class="child child-2">2</p>
 <p class="child child-3">3</p>
 <p class="child child-4">4</p>
 <p class="child child-5">5</p>  
 
 </div>
</body>
</html>
```

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/02855262-1fdf-4bd5-86ca-d45c7ad87db9.png align="center")

After Writing `display:flex`

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex; 
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/b2696393-d020-469f-95e0-3221bb6cbd44.png align="center")

2. `flex-direction:row`

it does not change output bu default this property is applied. it is align the item on main axis from left to right. it is specifies the direction in which flex items are arraanged.

3. f`lex-direction:column`

It is a CSS flecbox property that arranges flex items vertically from top to bottom along the main axis. it change the Main Axis top to bottom and cross axis Left to right.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
     flex-direction: column; 
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/9ef359f5-b2e9-4692-82ed-a096355d8c32.png align="center")

4. `flex-direction:row-reverse`

It is a CSS flexbox Property that arranges flex items horizontally in the reverse order from right to left.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
     flex-direction:row-reverse; 
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/f6493576-0034-4315-8359-4896414034b6.png align="center")

5. `flex-direction:column-reverse`

It is a CSS flexbox property that arranges flex items vertically in the reverse order bottom to top.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
     flex-direction:column-reverse; 
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/4c574e37-9bc0-4245-9950-2cdfbae879f1.png align="center")

6. `justify-content: ;`

It is a CSS flexbox property used to align and distribute flex items along the maina xis of a flex container.Flexbox alignment properties work along the Main Axis, and the direction of the main axis depends on the `flex-direction` property.

7. `justify-content:center;`

It is a CSS flexbox property that align flex items at the center of the flex container along the main axis.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
    justify-content: center;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/46650a10-057b-4965-9317-07634daf3a3c.png align="center")

8. `justify-content:end;`

It is a flexbox property that positions flex items at the end of the main axis of the flex container.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
    justify-content: end;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/32557c31-8994-49ca-8b0b-faf10d0ca382.png align="center")

9. `justify-content:start;`

It is a CSS flexbox property that positions the flex items at the start of the main axis of the flex container.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
    justify-content: start;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/b4012a8f-4e31-45b6-91e5-cfb7dc958e39.png align="center")

10. `justify-content:space-around;`

It is a CSS flexbox property that distributes flex items along the main axis by providing equal space around each item. The space at the beginning and end of the container is half the space between two adjacent items.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
    justify-content: space-around;
}
```

Browser Preview

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/18e93659-ea04-4e39-bfff-038350b56078.png align="center")

11. `justify-content:space-between;`

It is a CSS flexbox property that distributes flex items evenly along the main axis placing the first item at the beginning and the last item at the end with equal space between the items.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
    justify-content: space-between;
}
```

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/7fb2cd1a-54ea-4ba6-bcbb-9e5d5d5162f2.png align="center")

12. `justify-content:space-evenly;`

It is a CSS flexbox property that distributes flex items evenly along the main axis providing equal space between all items and at both end of containers.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
    justify-content: space-evenly;
}
```

Browser Preview

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/065464d9-c489-48cf-99ac-52be416d633e.png align="center")

13. `align-items: ;`

It is the CSS flexbox property that arrange all the flex items along the cross axis.

14. `align-items:center;`

It is a CSS flexbox property that aligns all the flex items at the center of the flex container along the cross axis.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
    justify-content: space-evenly;
    align-items: center;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/56210cc3-6c06-4f53-a8b8-f2e19ac205f4.png align="center")

15. `align-items:start;`

It is a CSS flexbox property that aligns all the flex items at the start of the flex container along the cross axis.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
    justify-content: space-evenly;
    align-items: start;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/385d1afa-5c4e-4db2-b290-8b912bddfb25.png align="center")

16. `align-items:end;`

It is a CSS flexbox property that aligns all the flex items at the end of the flex container along the cross axis.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
    justify-content: space-evenly;
    align-items: end;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/ec25a330-3ed3-469c-a70c-05f339d951c0.png align="center")

17. `align-items:stretch;`

It is a CSS flexbox property that stretches all flex items to fill the available space along the cross axis of the flex container provided their cross-axis size is not explicitly constrained.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    /* height: 40px;
    width: 40px; */
}
#parent{
     display: flex;
    justify-content: space-evenly;
    align-items: stretch;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/a878037f-cfda-46c3-be1a-330e5a8927ff.png align="center")

18. `gap:50px;`

It is a CSS flexbox property used to create 50 pixels of space between flex items inside a flex container.

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
    /* justify-content: space-evenly;
    align-items: stretch; */
    gap:50px;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/85fc1fb8-cc4e-491c-af46-28cbb707570a.png align="center")

We have understand all the properties based on parent element till now. let discuss the properties which is work on child elements.

1. `flex:1;`

It is a css flexbox property applied to child element that allows it to grow and occupy the available free space inside the flex container

**CSS File**

```css
#parent{
    border: 2px solid brown;
    width: 70vh;
    height: 350px;
}
.child{
    border: 2px solid goldenrod;
    height: 40px;
    width: 40px;
}
#parent{
     display: flex;
    justify-content: space-evenly;
    align-items: stretch;
    gap:2px;
}
.child-1{
    flex: 1;
}
```

Browser Preview

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/2e59cfc8-d6a0-4ff2-8623-1f8a19438a8e.png align="center")

2. `flex: 2` and `flex: 1`

- `flex: 2` allows an element to take twice the available space compared to an element with `flex: 1`.
- `flex: 1` allows an element to take one share of the available space.

**CSS File**

```css
.child-1{
    flex: 2;
}
.child-2{
    flex: 1;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/d878244d-7974-4700-9f72-ac4c68748f7d.png align="center")

3. `align-self:end;`

It is a CSS flexbox property applied to an individual child element positions it to at the end of the cross axis overriding the alignment specified by the parent's `align-items` property.

CSS File

```css
.child-1{
    flex: 2;
}
.child-2{
    flex: 1;
    align-self: end;
}
```

Browser Preview

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/ea50bf46-d9c4-4a23-9035-921a23e481ea.png align="center")

3. `Order: ;`

The `order` property is a CSS flexbox property applied to child elements to change their visual display order inside a flex container without changing the actual order of elements in the HTML document. It work based on Priority.

By default all the element have the 0 Priority.

**CSS File**

```css
.child-1{
    flex: 2;
}
.child-2{
    flex: 1;
    /* align-self: end; */
}
.child-3{
    order:2;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/03d5beac-b3b2-47f6-9843-84d80fa40fdc.png align="center")

Let understand it with the help of diagram how it is change the order

Diagram

Bye default priority of all element is Zero

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/c8bd60ce-b649-4054-b5e0-f9bf26d13a50.png align="center")

After set the `Order` property 2 on child 3

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/63eedbb1-284c-44d8-ac12-becd6fe73151.png align="center")

It scan firstly Order 0 Bucket then Order 1 and Order 2 bucket. Now the displaying order of flex items is:

`1,2,4,5,3`

## Advantages of Flexbox

1. **Easy Alignment:** Easily align elements horizontally and vertically using `justify-content` and `align-items`.
2. **Flexible Layout:** Items can automatically adjust their sizes according to the available space.
3. **Space Distribution:** Distribute space between elements using properties like `space-between`, `space-around`, and `space-evenly`.
4. **Responsive Design:** Helps create layouts that adjust to different screen sizes.
5. **Easy Reordering:** Change the visual order of elements using the `order` property without changing the HTML structure.
6. **Less CSS Code:** Reduces the need for complicated positioning and floating techniques.
7. **Gap Support:** Easily add consistent spacing between items using the `gap` property.

## Disadvantages of Flexbox

1. **One-Dimensional Layout:** Flexbox is mainly designed to work in one direction at a time—row or column.
2. **Not Ideal for Complex Layouts:** It can be difficult to create complex two-dimensional layouts involving rows and columns.
3. **Unexpected Sizing:** Flex items may shrink or grow automatically, which can sometimes produce unexpected results.
4. **Alignment Can Be Confusing:** Understanding the main axis and cross axis can be difficult for beginners.
5. **Visual Order vs HTML Order:** Changing the visual order using `order` does not change the actual HTML or keyboard navigation order.
