CSS Flexbox (Flexible Box Layout)

CSS Flexbox is short for the CSS Flexible Box Layout module.

Flexbox is a layout model for arranging items (horizontally or vertically) within a container, in a flexible and responsive way.

Flexbox makes it easy to design a flexible and responsive layout, without using float or positioning.

CSS Flex Container Properties

The flex container element can have the following properties:


CSS flex-direction Property

The flex-direction property specifies the display-direction of flex items in the flex container.

This property can have one of the following values:

Example

The row value is the default value, and it displays the flex items horizontally (from left to right):

Example

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

Example

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

Example

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

CSS flex-wrap Property

The flex-wrap property specifies whether the flex items should wrap or not, if there is not enough room for them on one flex line.

This property can have one of the following values:

  • nowrap (default)
  • wrap
  • wrap-reverse

Example

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

Example

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

Example

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

CSS flex-flow Property

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

Example

CSS justify-content Property

The justify-content property aligns the flex items along the main-axis (horizontally).

This property can have one of the following values:

  • center
  • flex-start (default)
  • flex-end
  • space-around
  • space-between
  • space-evenly

Example

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

Example

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

Result:

Example

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

 

Example

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

Result:

Example

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

Result:

Example

The space-evenly value displays the flex items with equal space around them:

Result:

CSS align-items Property

The align-items property aligns the flex items vertically (on the cross-axis).

This property can have one of the following values:

  • normal (default)
  • stretch
  • center
  • flex-start
  • flex-end
  • baseline

Example

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

Result:

Example

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

Result:

Example

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

 

Example

The stretch value stretches the flex items to fill the container (this is equal to “normal” which is default):

Result:

CSS align-content Property

The align-content property aligns the flex lines. It only has effect when flex items wrap onto multiple lines.

This property can have one of the following values:

  • stretch (default)
  • center
  • flex-start
  • flex-end
  • space-between
  • space-around
  • space-evenly

Example

With center, the flex lines are packed toward the center of the container:

Result:

Example

With space-between, the space between the flex lines are equal:

Result:

True Centering With Flexbox

The following example shows how to solve a common style problem: true centering.

To achieve true centering, set both the justify-content and the align-items properties to center for the flex container, and the flex item will be perfectly centered both horizontally and vertically:

Example

Result:

CSS Flex Items

The direct child elements of a flex container automatically becomes flex items.

Flex items can have the following properties:

  • order – Specifies the display order of the flex items inside the flex container
  • flex-grow – Specifies how much a flex item will grow relative to the rest of the flex items
  • flex-shrink – Specifies how much a flex item will shrink relative to the rest of the flex items
  • flex-basis – Specifies the initial length of a flex item
  • flex – Shorthand property for flex-growflex-shrink, and flex-basis
  • align-self – Specifies the alignment for the flex item inside the flex container

CSS order Property

The order property specifies the display order of the flex items inside the flex container.

The first flex item in the source code does not have to appear as the first item in the layout.

The order value must be a number, and the default value is 0.

Example

The order value specifies the display order of the flex items:

Result:

CSS flex-grow Property

The flex-grow property specifies how much a flex item will grow relative to the rest of the flex items.

The value must be a number, and the default value is 0.

Example

Make the third flex item grow four times faster than the other flex items:

Result:

CSS flex-shrink Property

The flex-shrink property specifies how much a flex item will shrink relative to the rest of the flex items.

The value must be a number, and the default value is 1.

Example

Let the third flex item shrink twice as much as the other flex items:

Result:

CSS flex-basis Property

The flex-basis property specifies the initial length of a flex item.

Example

Set the initial length of the third flex item to 250 pixels:

 

CSS flex Property

The flex property is a shorthand property for the flex-growflex-shrink, and flex-basis properties.

Example

Make the third flex item growable (1), not shrinkable (0), and with an initial length of 150 pixels:

CSS align-self Property

The align-self property specifies the alignment for the selected item inside the flexible container.

This property overrides the default alignment set by the container’s align-items property.

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

Example

Align the third flex item in the middle of the container:

Result:

Example

Align the second flex item at the top of the container, and the third flex item at the bottom of the container:

Result:

The CSS Flex Items Properties

The following table lists all the CSS Flex Items properties: