Day 12 - Digging Deeper into CSS - Flexbox
Tags: html, css, codenewbie
Published: 2019-11-08 06:57:36

Another day, another concept. Today we're flexing on Flexbox. 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/oznasclt6tsqifilsyvz.jpg)


With Flexbox, "flex containers" and "flex items" are introduced. Think of them like "parent element" and "child element" but they are boxes. The "flex container" the box that is the parent element and its main goal is to organize the boxes that are the "flex items" (child element) and set their position. 

Understanding this is key as you will want to ensure that you are adding properties to the elements you want.

So now you have the container and the items and you want to move the items and you will do so mainly with the container. You will want to use the available properties for Flexbox but you should know that a specific property is preset to move in a certain direction, that is the "main axis" and the cross "axis".

Here is a visual that helped me in the beginning and will help in understanding which property to use to move the items in a specific position.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/nnzwomhl9di6t0z1q5b5.png)

I am organizing the below into properties for the flex container and the flex items. [CSS Trick](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) has an excellent breakdown of the value of the different properties. 

##Flex Container Properties

These only affect the flex items boxes and not the content inside them.

1- Horizontal Alignment
<code>justify-content</code> is used to position the flex items horizontally along the main axis. There are multiple values such as <code>center</code>, <code>flex-start</code>, <code>flex-end</code>, <code>space-around</code> and <code>space-between</code>. 

The default value for this property is <code>flex-start</code>.

2- Vertical Alignment
<code>align-items</code> is used to position the flex items vertically along the cross axis. The values are similar to <code>justify-content</code>.

They are: <code>center</code>, <code>flex-start</code>, <code>flex-end</code>, <code>stretch</code> and <code>baseline</code>. 

The default value for this property is <code>stretch</code>.

3- Flex-Wrap
Imagine that you have a container that has a set width of 600px. The flex items are set to a width of 200px and there are 5 flex items. What will happen is that the flex items will line up horizontally and thus creating a horizontal scroll bar. Of course, we don't want that and that's where <code>flex-wrap:wrap</code> comes in. It defines what happens to the flex items that are outside the container.

4- Flex-Direction
This defines whether the flex items will be displayed horizontally or vertically. The main two  values are <code>flex-direction: row;</code> and <code>flex-direction: column</code>.

The default direction is <code>flex-direction: row;</code>.

**Note**
When the <code>flex-direction;</code> is row, the main axis is horizontal and the cross axis is vertical.

When the <code>flex-direction;</code> is column, the main axis is vertical and the cross axis horizontal.

Another cool thing you can do with <code>flex-direction;</code> are with the values <code>row-reverse</code> and <code>column-reverse</code>. This is really cool way to change the order of the flex items.


##Flex Items Properties

Flex Container only affect the direct child, the flex items, and they don't affect the content the the direct flex items. These properties can be used to manipulate the content inside the flex items.

1- Order
The purpose of the <code>order</code> property is to create lay the items out in ordinal groups. They are assigned a number and the items are then laid out in order starting with the ones with the lowest value. If two items have the same order value, they will be place according to the HTML placement.


[MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Ordering_Flex_Items) explained this very well.

Let's look at this example from MDN.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/x7azvup9fhqdmp6bj2u2.png)


Now, you wouldn't see the above often. The <code>order</code> property could be given to only two flex items.

Let's take the example above and keep the <code>order</code> property to source item 3 and 5 and the rest will be empty. What would happen?

Source item 3 and 5 will be moved to the end of the row. Remember how we said that the ordinal group always starts from the lowest. Well, each flex item has a default value of 0. Therefore, when we added the <code>order: 1;</code> to source item 3 and 5, we gave them a higher value than 0 and they are pushed to the back.

Another value I've seen is the negative value which will put the item to the from of the other items since the value is lower than 0.

2- Align-Self
<code>align-self</code> is used to position the content within the flex items vertically along the cross axis.

It has the same value as <code>align-items</code>.

3-Flex

The <code>flex</code> CSS property sets how a flex item will grow or shrink to fit the space available in its flex container. It is a shorthand for <code>flex-grow</code>, <code>flex-shrink</code>, and <code>flex-basis</code>. The default property settings is <code>flex: 0 1 auto;</code>.

<code>flex-grow</code>: This specifies how much of the extra available space that a flex item should take. The default is 0, meaning if there is extra space the flex item will not stretch. 

<code>flex-shrink</code>: When there is not enough room, this specifies how much space should a flex item give up in proportion to the other flex items. The default is 1 meaning that when there is not enough room, the flex items should be spread evenly. 

<code>flex-basis</code>: This specifies the initial size of the flex item before CSS makes adjustments with <code>flex-shrink</code> or <code>flex-grow</code>.  

The units used by the f<code>flex-basis</code> property are the same as other size properties (px, em, %, etc.). The value auto sizes items based on the content.


Wow, another one down. It is 2am and I didn't think I would be able to do this today but I pushed through.

The streak continues...

<code></code>