Skip to content
Tutorials Center
My learning

Topics

Lesson 70 of 139 ยท Flexbox

Flex Container Properties

Master the flex container: flex-direction, flex-wrap, flex-flow, justify-content, align-items, align-content and gap, with a live playground to try them all.

In Flexbox Basics you made a flex container and met the main and cross axes. Now it's time to take control. Most of what Flexbox can do is set on the container: which way the items flow, whether they wrap onto new lines, and how they are spaced and aligned along both axes.

This lesson walks through every container property, one at a time, and ends with a playground where you can mix them freely and see the result instantly. Keep the two axes in mind throughout: justify-* always means the main axis, align-* always means the cross axis.

flex-direction: which way items flow

flex-direction sets the main axis and the direction items are placed along it.

CSS
.container {
  flex-direction: row;            /* default: inline direction, start to end */
  flex-direction: row-reverse;    /* inline direction, end to start */
  flex-direction: column;         /* block direction, top to bottom */
  flex-direction: column-reverse; /* block direction, bottom to top */
}

row follows the text direction, so in a right-to-left page a row starts on the right. The -reverse values flip only the visual order. As you learned in the previous lesson, the reading and tab order still follow the HTML, so use them carefully.

A column container is useful far more often than people expect: a vertical stack of form fields, a sidebar menu, or a card whose footer you want to push to the bottom (you will build that in Flexbox Patterns).

flex-wrap: one line or many

By default a flex container keeps every item on one line, shrinking items as needed and letting them overflow if they cannot shrink any further. flex-wrap lets items move onto new lines instead.

CSS
.container {
  flex-wrap: nowrap;       /* default: everything on one line */
  flex-wrap: wrap;         /* wrap onto new lines below */
  flex-wrap: wrap-reverse; /* wrap onto new lines above */
}
nowrap vs wrap

With nowrap, the tags cannot shrink below their text (because of white-space: nowrap), so they spill out of the 260px box. With wrap, they flow onto a second line. Each line in a wrapping container is laid out independently, which is why the items on the last line do not line up in columns with the ones above. If you need that, use Grid.

flex-flow: the shorthand

flex-flow sets flex-direction and flex-wrap in one declaration, in either order:

CSS
.container {
  flex-flow: row wrap;
  flex-flow: column nowrap;
}

justify-content: spacing on the main axis

When items do not fill the main axis, justify-content decides where the leftover space goes.

Value Effect
flex-start (default) items packed at the main-start
flex-end items packed at the main-end
center items packed in the middle
space-between first and last items touch the edges; equal space between the rest
space-around each item gets equal space on both sides, so edge gaps are half the inner gaps
space-evenly every gap, including the edges, is exactly equal

You will also see start and end. These come from the newer Box Alignment specification and follow the writing mode rather than the flex direction. For everyday rows they look the same as flex-start and flex-end; they differ only with the -reverse directions.

justify-content only has something to do when there is free space. If any item has flex-grow set (for example flex: 1), that item soaks up the free space and justify-content has no visible effect.

align-items: alignment on the cross axis

align-items positions items within their line along the cross axis.

Value Effect
stretch (default) items stretch to fill the line's cross size, unless they have a fixed height (or width, in a column)
flex-start items line up at the cross-start (the top, in a row)
flex-end items line up at the cross-end
center items are centered on the cross axis
baseline items line up so the first lines of their text sit on the same baseline

stretch is why flex items in a row are all the same height by default. baseline is handy when items have different font sizes, such as a big price next to a small "/month" label: their text lines up as if it were one sentence.

A single item can override align-items with align-self, covered in Flex Item Properties.

The flexbox playground

Time to play. Change the select boxes and the container updates live. The CSS that produces the current layout is printed below the boxes, so you can copy it straight into your own stylesheet.

Flexbox playground

Some experiments worth trying:

  • Set justify-content to each space-* value in turn and compare the gaps at the edges.
  • Choose align-items: baseline and watch the large "B" line up its text with the others.
  • Switch to column and notice that justify-content now moves items vertically, while align-items moves them horizontally.
  • Pick wrap, then change align-content. It only does something once there is more than one line (narrow the preview or switch to column with wrap to force extra lines).

align-content: spacing the lines

align-content is the cross-axis twin of justify-content, but it moves whole lines, not individual items. It only affects multi-line containers, meaning flex-wrap: wrap with enough items to create more than one line, and a container that has spare cross-axis space.

It accepts the same distribution values as justify-content (flex-start, center, space-between and so on) plus stretch, which makes lines grow to share the extra space. The initial value is normal, which behaves like stretch in a flex container.

align-content moves whole lines

On the left, both lines are packed at the top. On the right, space-between pushes the first line to the top and the last to the bottom. Remember the difference: align-items positions items within each line; align-content positions the lines within the container.

gap, row-gap and column-gap

You met gap in the last lesson. In a wrapping row container it accepts two values: the first is the space between lines (row-gap), the second the space between items on a line (column-gap).

CSS
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 8px;  /* 12px between lines, 8px between items */
}

The names row-gap and column-gap refer to the physical rows and columns of a horizontal layout. In a flex-direction: column container, column-gap still separates columns (which are now your wrapped lines) and row-gap separates items stacked in a line.

A quick reference

Property Axis Typical values
flex-direction sets the main axis row, column
flex-wrap lines nowrap, wrap
flex-flow shorthand row wrap
justify-content main center, space-between
align-items cross, per item stretch, center, baseline
align-content cross, per line flex-start, space-between
gap both 1rem, 12px 8px

There is also a shorthand, place-content, that sets align-content and justify-content together. It's more common in Grid; see Grid Alignment.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In a container with flex-direction: column, what does justify-content: center do?

    justify-content always works on the main axis. In a column, the main axis is vertical, so the items are centered vertically (if the container has spare height).
  2. 2. Which value of justify-content makes the space before the first item, between items and after the last item all equal?

    space-evenly makes every gap identical. space-around gives half-size gaps at the edges, and space-between gives none.
  3. 3. Why might align-content: center have no visible effect?

    align-content positions lines. With flex-wrap: nowrap (or too few items to wrap) there is only one line, so there is nothing to distribute.
  4. 4. What is flex-flow: column wrap short for?

    flex-flow is the shorthand for flex-direction and flex-wrap.
  5. 5. Your row has a heading in 32px text and a small label in 12px text. Which align-items value makes their text sit on the same line?

    baseline aligns items by the baseline of their first line of text, so differently sized text lines up as if it were one sentence.

Summary

  • flex-direction sets the main axis (row or column, optionally reversed); flex-wrap allows items to move onto new lines; flex-flow combines both.
  • justify-content distributes free space along the main axis: flex-start, center, flex-end, space-between, space-around, space-evenly.
  • align-items positions items on the cross axis within their line; the default stretch makes items equal height in a row.
  • align-content positions whole lines and only matters in a wrapping container with more than one line.
  • gap sets the space between items and between lines, with separate row-gap and column-gap when needed.

Next: Flex Item Properties, where individual items learn to grow, shrink and break ranks.