Skip to main content

CSS Flex Container Properties

These properties go on the flex container element itself:

  • display — needs the value flex or inline-flex
  • flex-direction — picks the direction flex items are laid out in
  • flex-wrap — decides if flex items may wrap
  • flex-flow — shorthand combining flex-direction and flex-wrap
  • justify-content — positions flex items along the main axis (horizontally) when they leave some of its space unused
  • align-items — positions flex items along the cross axis (vertically) when they leave some of its space unused
  • align-content — positions the flex lines when items wrap and the cross axis has extra space

CSS flex-direction Property

Use flex-direction to choose which way the items inside a flex container run. Its four possible values are:

  • row (default)
  • column
  • row-reverse
  • column-reverse

Row direction

With row, which applies by default, flex items sit side by side from left to right.

Prompt
Create a flex container with three numbered items (1, 2, 3) using flex-direction: row. The container should have a DodgerBlue background, and each item should have a white background, 10px margin, 20px padding, and 30px font size.
Output: complete HTML page

Column direction

With column, flex items stack vertically, starting at the top and ending at the bottom.

Prompt
Create a flex container with three numbered items (1, 2, 3) using flex-direction: column. The container should have a DodgerBlue background, and each item should have a white background, 10px margin, 20px padding, and 30px font size.
Output: complete HTML page

Row reverse direction

Still horizontal, row-reverse runs the flex items from right to left instead.

Prompt
Create a flex container with three numbered items (1, 2, 3) using flex-direction: row-reverse. The container should have a DodgerBlue background, and each item should have a white background, 10px margin, 20px padding, and 30px font size.
Output: complete HTML page

Column reverse direction

Still vertical, column-reverse runs the flex items from bottom to top instead.

Prompt
Create a flex container with three numbered items (1, 2, 3) using flex-direction: column-reverse. The container should have a DodgerBlue background, and each item should have a white background, 10px margin, 20px padding, and 30px font size.
Output: complete HTML page

CSS flex-wrap Property

When one flex line has too little room for all the items, flex-wrap decides whether they move onto further lines. You can choose from:

  • nowrap (default)
  • wrap
  • wrap-reverse

Nowrap

By default, nowrap keeps every flex item on the one line, with no wrapping.

Prompt
Create a flex container with nine numbered items (1 through 9) using flex-wrap: nowrap. The container should have a DodgerBlue background and be 200px wide. Each item should have a white background, 10px margin, 20px padding, and 30px font size.
Output: complete HTML page

Wrap

With wrap, flex items move onto new lines whenever they need to.

Prompt
Create a flex container with nine numbered items (1 through 9) using flex-wrap: wrap. The container should have a DodgerBlue background and be 200px wide. Each item should have a white background, 10px margin, 20px padding, and 30px font size.
Output: complete HTML page

Wrap reverse

Flex items also move onto new lines under wrap-reverse when they need to, only in reverse order.

Prompt
Create a flex container with nine numbered items (1 through 9) using flex-wrap: wrap-reverse. The container should have a DodgerBlue background and be 200px wide. Each item should have a white background, 10px margin, 20px padding, and 30px font size.
Output: complete HTML page

CSS flex-flow Property

Rather than writing flex-direction and flex-wrap separately, you can set both on one line with the shorthand flex-flow property.

Example

Prompt
Create a flex container with three numbered items (1, 2, 3) using the flex-flow shorthand set to "row wrap". The container should have a DodgerBlue background, and each item should have a white background, 10px margin, 20px padding, and 30px font size.
Output: complete HTML page

Flex Container Subpages

Two more pages carry on with the flex container properties:

  • justify-content — aligning flex items horizontally
  • align-items and align-content — aligning vertically, and true centering