Hi FlexBox, nice to meet you 🙂
Tags: css, flexbox, webdev
Published: 2019-06-13 01:15:29

###What is FlexBox? 🤔
_This tool provides an efficient way to lay out, align and distribute space among items in a container, even when we don't know each element size._

![alt text](https://media.giphy.com/media/RhPvGbWK78A0/giphy.gif "Interesting")

###Browser compatibility ✔
Nowadays, flexbox is supported by all modern browsers.

![](https://thepracticaldev.s3.amazonaws.com/i/auwgikbmrtxvlw5hbwt2.PNG)


---

###Concepts 💡

Consider the following image as a reference for the next concepts:

![](https://thepracticaldev.s3.amazonaws.com/i/4v61t0w8nmlf6n94kul6.png)

__display__
This property defines a flex container, so all container's children will have a flex context.

```css
.container {
  display: flex;
}
```

---

__flex-direction__
Using this property we set the main-axis. Flexbox is single-direction, so we can show our items in horizontal rows or vertical columns.


![](https://thepracticaldev.s3.amazonaws.com/i/pp3bu9k73kbf36ycaxi8.png)

```css
.container {
  flex-direction: row | row-reverse | column | column-reverse;
}
```

---

__order__
With Flexbox we can change the order of each item inside the container, using order attribute. By default, we'll see the same order displayed on our HTML.


![](https://thepracticaldev.s3.amazonaws.com/i/muituev7d33xwhrz15nd.png)


```css
.container {
  display: flex;
}
.item {
  order: <integer>;
}
```

---

__flex-grow__
This attribute defines what amount of the available space inside our container the item should take. It's useful when we want to let an item grow if necessary.


![](https://thepracticaldev.s3.amazonaws.com/i/jpfp52729sogchtt2av1.png)

```css
.item {
  flex-grow: <number>; /* Default 0 */
}
```

---

__flex-wrap__
Flexbox will always try to show all the items in one line. To prevent this, if necessary, we can use flex-wrap attribute.

![](https://thepracticaldev.s3.amazonaws.com/i/bnqcv1ob44geu6ck7x7j.png)

`nowrap` - default value. Items in one line.
`wrap` - multiple lines from top to bottom.
`wrap-reverse` - multiple lines from bottom to top.

```css
.container {
   flex-wrap: nowrap | wrap | wrap-reverse;
}
```

---

__flex-shrink__
This attribute defines the ability for an item to shrink.

![](https://thepracticaldev.s3.amazonaws.com/i/v0il3npldiro9w9g7gnk.png)

```css
.item {
   flex-shrink: <number>; /* Default 1 */
}
```

---

__flex__
Using this property we can avoid using `flex-grow`, `flex-shrink` and `flex-basis`. The second parameter is `flex-shrink`, the third parameter is `flex-basis`. Default `0 1 auto`.

```css
.item{
  flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ];
}
```

---

__justify-content__
With this attribute, we define the container's children alignment along the main axis.

![](https://thepracticaldev.s3.amazonaws.com/i/hzfzsyd8yd3viz338cee.png)

`flex-start` - default value. Items aligned to the left.
`flex-end` - items aligned to the right.
`center` - items centered
`space-between` - items evenly distributed in the line. First item on the left, last item on the right.
`space-around` - items evenly distributed with equal space around them.
`space-evenly` - equal space between two items.

```css
.container {
   justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
}
```

---

__align-items__
Define how our items will behave on the opposite axis to the main axis. (It's always perpendicular)

![](https://thepracticaldev.s3.amazonaws.com/i/4elluabvo9bzpifc7c48.png)

`flex-end` - cross-end margin edge of the items is placed on the cross-end line
`flex-start` - cross-start margin edge of the items is placed on the cross-start line
`stretch` - default - fill the container considering min-width/max-width
`center` - items centered
`baseline` - items aligned based on their baselines align

```css
.container {
   align-items: stretch | flex-start | flex-end | center | baseline;
}
```

---

__align-self__
Using this attribute we can override the default alignment for a specific item.

![](https://thepracticaldev.s3.amazonaws.com/i/dd2jalha9rd9om8kg1bl.png)


```css
.item {
   align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
```

---

__align-content__
We can use this attribute to align container's lines within when there is extra space in the cross-axis.

__Pro tip:__ use this attribute only when there is more than one line of items inside your container.

![](https://thepracticaldev.s3.amazonaws.com/i/u79ld71v9dswupj8grbn.png)

`stretch` - default - the content will take up the remaining space
`flex-start` - lines aligned to the start of the container
`flex-end` - lines aligned to the end of the container
`center` - lines aligned to the center
`space-between` - lines evenly distributed, first line is at the start and last line is at the end of the container
`space-around` - lines evenly distributed with equal space around them

```css
.container {
   align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}
```

---

###Playground 👨‍💻
Are you curious? Give it a try, play with it.


{% codepen https://codepen.io/ecastellano/pen/BgNZPW %}

###Thanks 🙌
Hope this article helps you. Feel free to contact me if you have any question.