This site is mobile accessible. Press the "Tap Here" button to use a smaller font-size.

Smartphone icons created by Freepik - Flaticon

15.4.1 CSS Flex Container

Parent Element (Container)

Like we specified in the previous chapter, this is a flex container (the blue area) with three flex items:

1
2
3

The flex container becomes flexible by setting the display property to flex:

Example 1: Define a flex container
.flex-container {
  display: flex;
}

The flex-direction Property

The flex-direction property defines in which direction the container wants to stack the flex items.

Example 2: Using flex-direction: column

The column value stacks the flex items vertically (from top to bottom):

.flex-container {
  display: flex;
  flex-direction: column;
}
Example 3: Using flex-direction: column-reverse

The column-reverse value stacks the flex items vertically (but from bottom to top):

.flex-container {
  display: flex;
  flex-direction: column-reverse;
}
Example 4: Using flex-direction: row

The row value stacks the flex items horizontally (from left to right):

.flex-container {
  display: flex;
  flex-direction: row;
}
Example 5: Using flex-direction: row-reverse

The row-reverse value stacks the flex items horizontally (but from right to left):

.flex-container {
  display: flex;
  flex-direction: row-reverse;
}

The flex-wrap Property

The flex-wrap property specifies whether the flex items should wrap or not. The examples below have 12 flex items, to better demonstrate the flex-wrap property.

1
2
3
4
5
6
7
8
9
10
11
12
Example 6: Using flex-wrap: wrap

The wrap value specifies that the flex items will wrap if necessary:

.flex-container {
  display: flex;
  flex-wrap: wrap;
}
Example 7: Using flex-wrap: nowrap

The nowrap value specifies that the flex items will not wrap (this is default):

.flex-container {
  display: flex;
  flex-wrap: nowrap;
}
Example 8: Using flex-wrap: wrap-reverse

The wrap-reverse value specifies that the flexible items will wrap if necessary, in reverse order:

.flex-container {
  display: flex;
  flex-wrap: wrap-reverse;
}

The flex-flow Property

The flex-flow property is a shorthand property for setting both the flex-direction and flex-wrap properties.

Example 9: Using flex-flow
.flex-container {
  display: flex;
  flex-flow: row wrap;
}

The justify-content Property

The justify-content property is used to align the flex items:

1
2
3
Example 10: Using justify-content: center

The center value aligns the flex items at the center of the container:

.flex-container {
  display: flex;
  justify-content: center;
}
Example 11: Using justify-content: flex-start

The flex-start value aligns the flex items at the beginning of the container (this is default):

.flex-container {
  display: flex;
  justify-content: flex-start;
}
Example 12: Using justify-content: flex-end

The flex-end value aligns the flex items at the end of the container:

.flex-container {
  display: flex;
  justify-content: flex-end;
}
Example 13: Using justify-content: space-around

The space-around value displays the flex items with space before, between, and after the lines:

.flex-container {
  display: flex;
  justify-content: space-around;
}
Example 14: Using justify-content: space-between

The space-between value displays the flex items with space between the lines:

.flex-container {
  display: flex;
  justify-content: space-between;
}

The align-items Property

The align-items property is used to align the flex items.

1
2
3

In these examples we use a 200 pixels high container, to better demonstrate the align-items property.

Example 15: Using align-items: center

The center value aligns the flex items in the middle of the container:

.flex-container {
  display: flex;
  height: 200px;
  align-items: center;
}
Example 16: Using align-items: flex-start

The flex-start value aligns the flex items at the top of the container:

.flex-container {
  display: flex;
  height: 200px;
  align-items: flex-start;
}
Example 17: Using align-items: flex-end

The flex-end value aligns the flex items at the bottom of the container:

.flex-container {
  display: flex;
  height: 200px;
  align-items: flex-end;
}

Perfect Centering

In the following example we will solve a very common style problem: perfect centering.

SOLUTION: Set both the justify-content and align-items properties to center, and the flex item will be perfectly centered:

Example 18: Perfect centering
.flex-container {
  display: flex;
  height: 300px;
  justify-content: center;
  align-items: center;
}