Flexbox Container Properties

The flexbox has a flex-container which is also known as parent container. This container holds all the flex-items inside it. In this tutorial we will go through all the flex properties related to flex container.

Remember that these properties will be applied directly to the flex container and not on the flex-items. Let's get through them one by one.


CSS Display Flex

To start using Flexbox, the first thing to add is the property display:flex; in the parent container. Just after you add this property, your parent container will become a flex-container and all of its child will become flex-items. These flex-items will by-default align themselves in a row.

Divs are block level-elements so they take up all horizontal space available and the next div automatically get inserted on a new line. But, by using display:flex;, Divs leave their default behaviour and get aligned one after another in a horizontal line. Let's see the example below:


<!DOCTYPE html> <html lang="en"> <head> <title> CSS Flex Box </title> <meta charset="UTF-8"> <style> .flex-container { display: flex; background-color: DodgerBlue; } .flex-item { background-color: #f1f1f1; margin: 10px; padding: 20px; font-size: 30px; } </style> </head> <body> <div class="flex-container"> <div class="flex-item">A</div> <div class="flex-item">B</div> <div class="flex-item">C</div> </div> <p>Look how the display:flex; is getting used in parent container.</p> </body> </html>

Output

A
B
C


Look how the display:flex; is getting used in parent container.


CSS Flex Direction

After setting up the container as Flexbox, the browser by-default uses the row direction for the flex-items, i.e., items get placed from left to right. But, what if we want to change the direction?

CSS Flex direction property makes it possible to display the flex-items as we want. They can be aligned in a horizontal row, in a vertical column, in a row reverse direction or in a column reverse direction. Let's see them one by one:

  • flex-direction:row; - It is the default property. It place flex-items from left to right in a row.
  • flex-direction:column; - It place the flex-items from top to bottom in a column.
  • flex-direction:row-reverse; - It places items from right to left and it also reverses the order of flex-items. The first item will be added last and vice versa.
  • flex-direction:column-reverse; - As the name suggests, it will place items from bottom to top and also but in a reverse order.

Let's see all the properties in an example:


<!DOCTYPE html> <html> <head> <style> .flex-container1 { display: flex; flex-direction:row; background-color: DodgerBlue; } .flex-container2 { display: flex; flex-direction:column; background-color: red; } .flex-container3 { display: flex; flex-direction:row-reverse; background-color: tomato; } .flex-container4 { display: flex; flex-direction:column-reverse; background-color: green; } .flex-item { background-color: #f1f1f1; margin: 5px; padding: 10px; font-size: 15px; width:100px; } </style> </head> <body> <h2>Flex Direction Row</h2> <div class="flex-container1"> <div class="flex-item">A</div> <div class="flex-item">B</div> <div class="flex-item">C</div> </div> <h2>Flex Direction column</h2> <div class="flex-container2"> <div class="flex-item">A</div> <div class="flex-item">B</div> <div class="flex-item">C</div> </div> <h2>Flex Direction Row-reverse</h2> <div class="flex-container3"> <div class="flex-item">A</div> <div class="flex-item">B</div> <div class="flex-item">C</div> </div> <h2>Flex Direction Column-reverse</h2> <div class="flex-container4"> <div class="flex-item">A</div> <div class="flex-item">B</div> <div class="flex-item">C</div> </div> <p>Look how the display:flex; is getting used in parent container.</p> </body> </html>

Output

Flex Direction Row

A
B
C

Flex Direction column

A
B
C

Flex Direction Row-reverse

A
B
C

Flex Direction Column-reverse

A
B
C


Look how the display:flex; is getting used in parent container.


CSS Flex Wrap

Flex Wrap is a very useful property in case of overflow of content. By default, CSS Flexbox tries to fit all the elements in a single row, it can result into overflow of content outside of the flex-container.

Flex Wrap property automatically places the flex-items in the next row, when they don't fit in a single row. There are three values which you can give to this property:

  • Flex-wrap: Wrap; - It activates the Wrap property. Now, all the overflown content will slip into next row and out of the container.
  • Flex-wrap: Wrap-reverse; - It also wraps the content but all the flex-items will be wrapped in a reverse order.
  • Flex-wrap: Nowrap; - It is the default value, flex-items won't get wrapped.

<!DOCTYPE html> <html> <head> <style> .flex-container1 { display: flex; flex-wrap: nowrap; background-color: Tomato; height: auto; } .flex-container2 { display: flex; flex-wrap: wrap; background-color: Tomato; height: auto; } .flex-container3 { display: flex; flex-wrap: wrap-reverse; background-color: Tomato; height: auto; } .flex-items { background-color: #f1f1f1; padding: 10px 25px; text-align: center; height: 20px; width: 100px; margin: 5px; } </style> </head> <body> <h3>Flex-wrap: nowrap; </h3> <div class="flex-container1"> <div class="flex-items">1</div> <div class="flex-items">2</div> <div class="flex-items">3</div> <div class="flex-items">4</div> <div class="flex-items">5</div> <div class="flex-items">6</div> <div class="flex-items">7</div> <div class="flex-items">8</div> </div> <h3>Flex-wrap: wrap;</h3> <div class="flex-container2"> <div class="flex-items">1</div> <div class="flex-items">2</div> <div class="flex-items">3</div> <div class="flex-items">4</div> <div class="flex-items">5</div> <div class="flex-items">6</div> <div class="flex-items">7</div> <div class="flex-items">8</div> </div> <h3>Flex-wrap: wrap-reverse;</h3> <div class="flex-container3"> <div class="flex-items">1</div> <div class="flex-items">2</div> <div class="flex-items">3</div> <div class="flex-items">4</div> <div class="flex-items">5</div> <div class="flex-items">6</div> <div class="flex-items">7</div> <div class="flex-items">8</div> </div> </body> </html>

Output

Flex-wrap: nowrap;

1
2
3
4
5
6
7
8

Flex-wrap: wrap;

1
2
3
4
5
6
7
8

Flex-wrap: wrap-reverse;

1
2
3
4
5
6
7
8

CSS Flex Flow (Shorthand property)

CSS Flex Flow property is a shorthand property in which we can combine both Flex-direction and Flex-Wrap properties. We can give the values for both the properties just by separating them with a space.

Syntax
<Flex-direction> <Flex-wrap>;

Example- Flex-flow: column wrap;

Let's take a loot at the example:


<!DOCTYPE html> <html> <head> <style> .flex-container { display: flex; flex-flow: row wrap; background-color: Tomato; } .flex-items { background-color: #f1f1f1; width: 100px; margin: 5px; text-align: center; height: 40px; font-size: 20px; } </style> </head> <body> <h2>Flex-flow</h2> <p>Notice how perfectly both flex-direction and flex-wrap are working.</p> <div class="flex-container"> <div class="flex-items">1</div> <div class="flex-items">2</div> <div class="flex-items">3</div> <div class="flex-items">4</div> <div class="flex-items">5</div> <div class="flex-items">6</div> </div> </body> </html>

Output

Flex-flow

Notice how perfectly both flex-direction and flex-wrap are working.

1
2
3
4
5
6

Justify Content of a Row in CSS Flexbox

Justify Content property is used when you want to align the flex-items to be displayed in a particular manner along the row(main axis). It horizontally aligns the item as specified. It justifies the extra horizontal space present in a row. For example, It can place items at the start, end or center and it can also manipulate the space around the flex-items.

There are different values that you can set to justify content:

  • justify-content:flex-start; - It places the flex-items at the starting of the flex(along the main axis), i.e., extreme left.
  • justify-content:flex-end; - It places the flex-items at the ending of the flex(along the main axis), i.e., extreme right.
  • justify-content:center; - It simply place the items in the center of the row. This property along with align-items:center; is very useful in centering the content within a flex-container.
  • justify-content:space-between; - It is a very useful property when there is some extra space left in a row. It equally distribute the extra space in-between the flex-items. The first and last item gets placed at left and right ends respectively and then the space gets distributed between them. Look at the example to clarify its concept.
  • justify-content:space-around; - This property will distribute equal amount of space to both left and right side of a flex-item.

Let's see all the properties in an example:


<!DOCTYPE html> <html> <head> <style> .flex-container1 { display: flex; flex-direction: row; justify-content: flex-start; background-color: DodgerBlue; } .flex-container2 { display: flex; flex-direction: row; justify-content: flex-end; background-color: red; } .flex-container3 { display: flex; flex-direction: row; justify-content: center; background-color: tomato; } .flex-container4 { display: flex; flex-direction: row; justify-content: space-between; background-color: green; } .flex-container5 { display: flex; flex-direction: row; justify-content: space-around; background-color: skyblue; } .flex-item { background-color: #f1f1f1; margin: 5px; padding: 10px; font-size: 15px; width: 100px; } </style> </head> <body> <h2>Justify-Content: Flex-start</h2> <div class="flex-container1"> <div class="flex-item">A</div> <div class="flex-item">B</div> <div class="flex-item">C</div> </div> <h2>Justify-Content: Flex-end</h2> <div class="flex-container2"> <div class="flex-item">A</div> <div class="flex-item">B</div> <div class="flex-item">C</div> </div> <h2>Justify-Content: Center</h2> <div class="flex-container3"> <div class="flex-item">A</div> <div class="flex-item">B</div> <div class="flex-item">C</div> </div> <h2>Justify-Content: Space-between</h2> <div class="flex-container4"> <div class="flex-item">A</div> <div class="flex-item">B</div> <div class="flex-item">C</div> </div> <h2>Justify-Content: Space-around</h2> <div class="flex-container5"> <div class="flex-item">A</div> <div class="flex-item">B</div> <div class="flex-item">C</div> </div> </body> </html>

Output

Justify-Content: Flex-start

A
B
C

Justify-Content: Flex-end

A
B
C

Justify-Content: Center

A
B
C

Justify-Content: Space-between

A
B
C

Justify-Content: Space-around

A
B
C

Note: There is one more property justify-content:space-evenly; try it and then see its effect.

Align Items in CSS Flexbox

Align Items positions the flex-items along the vertical space(cross axis). If there is some vertical space left around the flex-items, then that space can be justified by using align-items property. It works same as justify-content but in cross axis.

There are different values that you can set to align-items:

  • align-items:flex-start; - It places the flex-items at the starting of the flex(cross axis, vertically).
  • align-items:flex-end; - It places the flex-items at the ending of the flex, i.e., at the bottom of the container.
  • align-items:center; - It aligns all the items in the center of the cross axis.
  • align-items:baseline; - This property aligns all the flex items on their baselines. The center point of all the flex-items would be on a same straight line. It will get cleared in the example.
  • align-items:stretch; - This will stretch all the flex-items' height to fill all the available vertical space. Remember that for stretch to work, the height of the flex-items should be set to auto.

Let's see the example below:


<!DOCTYPE html> <html> <head> <style> .flex-container1 { display: flex; height: 100px; flex-direction: row; align-items: flex-start; background-color: DodgerBlue; } .flex-container2 { display: flex; flex-direction: row; background-color: red; height: 100px; align-items: flex-end; } .flex-container3 { display: flex; flex-direction: row; height: 100px; align-items: center; background-color: tomato; } .flex-container4 { display: flex; flex-direction: row; height: 100px; align-items: baseline; background-color: green; } .flex-container4 > div { background-color: #f1f1f1; width: 100px; margin: 10px; text-align: center; } .flex-container5 { display: flex; flex-direction: row; height: 100px; align-items: stretch; background-color: skyblue; } .flex-item { background-color: #f1f1f1; margin: 5px; padding: 15px; font-size: 20px; height: 25px; } </style> </head> <body> <h2>Align-items: Flex-start</h2> <div class="flex-container1"> <div class="flex-item">A</div> <div class="flex-item">B</div> <div class="flex-item">C</div> </div> <h2>Align-items: Flex-end</h2> <div class="flex-container2"> <div class="flex-item">A</div> <div class="flex-item">B</div> <div class="flex-item">C</div> </div> <h2>Align-items: Center</h2> <div class="flex-container3"> <div class="flex-item">A</div> <div class="flex-item">B</div> <div class="flex-item">C</div> </div> <h2>Align-items: Baseline</h2> <div class="flex-container4"> <div> <h1>A</h1></div> <div> <h4>B</h4></div> <div> <h2>C</h2></div> </div> <h2>Align-items: Stretch</h2> <div class="flex-container5"> <div style="height:auto;" class="flex-item">A</div> <div style="height:auto;" class="flex-item">B</div> <div style="height:auto;" class="flex-item">C</div> </div> </body> </html>

Output

Align-items: Flex-start

A
B
C

Align-items: Flex-end

A
B
C

Align-items: Center

A
B
C

Align-items: Baseline

A

B

C

Align-items: Stretch

A
B
C

Align-Content of a column in CSS Flexbox

Align-content is similar to justify-content but it works on the cross axis(vertical axis). It will align the items on the vertical axis, if, there is some extra space left. It is a little bit different than previous properties, there are some conditions that should be met, for it to work:

  • There property flex-flow should be set to wrap or wrap-reverse.
  • There should be multiple rows. It doesn't show any effect on a single row.

So, it is clear from the above conditions that this property will only work when the flex-flow:wrap/wrap-reverse gets engaged, otherwise not.

There are different values that can be set to align-content:

  • align-content:flex-start; - It places the flex items at the starting of the cross axis(vertical).
  • align-content:flex-end; - It aligns the flex-items at the end of the cross axis, i.e., bottom.
  • align-content:center; - All the items gets aligned in the center.
  • align-content:space-around; - This property will distribute equal amount of space to both top and bottom side of a flex-items.
  • align-content:space-between; - It equally distribute the extra vertical space in-between the flex-items.
  • align-content:stretch; - This property will stretch the height of all the flex-items to fill-up the extra space present inside the container. Remember to use height:auto; on flex-items for this property to show effect./li>

Let's take a look at the example:


<!DOCTYPE html> <html> <head> <style> .flex-container1 { display: flex; flex-flow: wrap; align-content: flex-start; background-color: Tomato; height: 100px; } .flex-container2 { display: flex; flex-flow: wrap; align-content: flex-end; background-color: Tomato; height: 100px; } .flex-container3 { display: flex; flex-flow: wrap; align-content: center; background-color: Tomato; height: 100px; } .flex-container4 { display: flex; flex-flow: wrap; align-content: space-around; background-color: Tomato; height: 100px; } .flex-container5 { display: flex; flex-flow: wrap; align-content: space-between; background-color: Tomato; height: 100px; } .flex-container6 { display: flex; flex-flow: wrap; align-content: stretch; background-color: Tomato; height: 100px; } .flex-items { background-color: #f1f1f1; padding: 10px; text-align: center; line-height: 10px; width: 40px; margin: 2.5px; } </style> </head> <body> <h3>Align-content: Flex-start;</h3> <div class="flex-container1"> <div class="flex-items">1</div> <div class="flex-items">2</div> <div class="flex-items">3</div> <div class="flex-items">4</div> <div class="flex-items">5</div> <div class="flex-items">6</div> <div class="flex-items">7</div> <div class="flex-items">8</div> </div> <h3>Align-content: Flex-end;</h3> <div class="flex-container2"> <div class="flex-items">1</div> <div class="flex-items">2</div> <div class="flex-items">3</div> <div class="flex-items">4</div> <div class="flex-items">5</div> <div class="flex-items">6</div> <div class="flex-items">7</div> <div class="flex-items">8</div> </div> <h3>Align-content: Center;</h3> <div class="flex-container3"> <div class="flex-items">1</div> <div class="flex-items">2</div> <div class="flex-items">3</div> <div class="flex-items">4</div> <div class="flex-items">5</div> <div class="flex-items">6</div> <div class="flex-items">7</div> <div class="flex-items">8</div> </div> <h3>Align-content: Space-around;</h3> <div class="flex-container4"> <div class="flex-items">1</div> <div class="flex-items">2</div> <div class="flex-items">3</div> <div class="flex-items">4</div> <div class="flex-items">5</div> <div class="flex-items">6</div> <div class="flex-items">7</div> <div class="flex-items">8</div> </div> <h3>Align-content: Space-between;</h3> <div class="flex-container5"> <div class="flex-items">1</div> <div class="flex-items">2</div> <div class="flex-items">3</div> <div class="flex-items">4</div> <div class="flex-items">5</div> <div class="flex-items">6</div> <div class="flex-items">7</div> <div class="flex-items">8</div> </div> <h3>Align-content: Stretch;</h3> <div class="flex-container6"> <div class="flex-items">1</div> <div class="flex-items">2</div> <div class="flex-items">3</div> <div class="flex-items">4</div> <div class="flex-items">5</div> <div class="flex-items">6</div> <div class="flex-items">7</div> <div class="flex-items">8</div> </div> </body> </html>

Output

Align-content: Flex-start;

1
2
3
4
5
6
7
8

Align-content: Flex-end;

1
2
3
4
5
6
7
8

Align-content: Center;

1
2
3
4
5
6
7
8

Align-content: Space-around;

1
2
3
4
5
6
7
8

Align-content: Space-between;

1
2
3
4
5
6
7
8

Align-content: Stretch;

1
2
3
4
5
6
7
8

Note: If Align-content is used on a single row with wrap property active, then the properties flex-start, flex-end, center and stretch will work but space-between and space-around won't work. It is same as using align-items, so for single rows, never use align-content, use align-items.

This is the end for the flex container properties. In the next tutorial of flexbox, we will look at some more properties related to flex-items. Click on Next.



Color Picker

List of color names supported by all browser »



Character Code

List of character codes to display special character »



Language Code

List of all language supported by all browser »



Country Code

List of Country code supported by all browser »