Menu

CSS justify-content and align-items: Every Value

justify-content places flex items along the main axis and align-items places them along the cross axis. See every value side by side, measured, with a clickable alignment grid, align-self, and the reasons justify-content sometimes does nothing.

This page includes runnable editors - edit, run, and see output instantly.

justify-content and align-items in one sentence each

In a flex container, justify-content spreads the items along the main axis (across the row) and align-items lines them up on the cross axis (up and down the row). Both go on the container:

The box is centered both ways: the same distance from the start and end edges, and from the top and bottom. Take height: 140px away and align-items: center has nothing to do, because the container shrinks to the item's own height.

Every justify-content value, measured

Each row below is the same three items with a different justify-content. The numbers in the label above each row are the gaps the browser left: before the first item, between items, and after the last.

The container is 420px and the items take 180px, so there are 240px of free space. Where it goes:

ValueFree space goesGaps in the demo
flex-start (and start)all after the items0 / 0 / 0 / 240
centerhalf before, half after120 / 0 / 0 / 120
flex-end (and end)all before the items240 / 0 / 0 / 0
space-betweenonly between items0 / 120 / 120 / 0
space-aroundequal on both sides of each item, so edges get half40 / 80 / 80 / 40
space-evenlyevery gap equal, edges included60 / 60 / 60 / 60

flex-start follows the flex direction and start follows the writing direction. They only differ with row-reverse or column-reverse, where flex-start is at the reversed end.

Every align-items value

align-items works across the row. The items below have different heights so you can see what each value does with them:

ValueWhat happens on the cross axis
stretch (the default)Items without a set height grow to fill the container
flex-start (and start)Items sit at the top, at their own height
centerItems are centered, at their own height
flex-end (and end)Items sit at the bottom
baselineItems move so the first lines of text share one baseline

In the baseline box the three "Ag" labels sit on one line even though the first is larger and the second has padding above it. That is the value for a row mixing a big heading with small text, or a label with an input.

Click to align

Click any cell of the 3 by 3 grid. The box moves there, and the CSS that puts it there is printed below:

The rows of the picker change align-items and the columns change justify-content. The middle cell is the classic "center a div".

In a column, the two swap

justify-content always follows the main axis. With flex-direction: column the main axis is vertical, so justify-content moves items up and down and align-items moves them across:

If you mix them up, swap the two properties rather than rewriting the layout.

align-self and auto margins: one item only

align-self on an item overrides align-items for that item. There is no justify-self in flexbox; to move one item along the main axis, give it an auto margin, which takes all the free space on that side:

The green item drops to the bottom on its own, and the amber item is pushed to the far end of the row while the others stay at the start.

When there are several lines: align-content

align-items aligns items inside their line. When a container wraps onto several lines, align-content spaces the lines themselves, and takes the same values as justify-content. It does nothing in a nowrap container, whose one line always fills the container's height. The flex-wrap page shows it.

Common mistakes

  • Mixing up the axes. justify-content is the flow direction, align-items is across it. In a column, vertical centering is justify-content: center.
  • No free space. An item with flex: 1 takes all the space, so justify-content has none to distribute. The same happens when the container is inline-flex and exactly as wide as its items.
  • No height for align-items. A container as tall as its content leaves nothing to align. Set a height or min-height.
  • Looking for justify-self in flexbox. It is ignored there. Use margin-inline-start: auto (or margin-left: auto) on the item.
  • align-items: stretch that does not stretch. An item with its own height keeps it. Remove the height (use min-height if it needs a minimum).
  • Putting the properties on the items. Both go on the flex container. Only align-self goes on an item.

Frequently Asked Questions

What is the difference between justify-content and align-items?

justify-content distributes items along the main axis, the direction they flow in (horizontal for flex-direction: row). align-items positions them along the cross axis (vertical for a row). In a column both swap: justify-content becomes vertical.

What is the difference between space-between, space-around and space-evenly?

space-between puts no space before the first item or after the last and splits the rest between items. space-around gives every item equal space on both sides, so the edges get half a gap. space-evenly makes every gap equal, the edges included.

Why is justify-content not working?

Usually there is no free space to distribute: an item has flex-grow or flex: 1, or the container is only as wide as its items (for example display: inline-flex or a floated container). Also check that display: flex is on the parent, not the items.

Why is align-items center not centering vertically?

The container is probably exactly as tall as its content, so there is nothing to center within. Give the flex container a height or min-height, and the items will move to its middle.

What does align-self do?

align-self overrides the container's align-items for one item. For example align-self: flex-end on one child moves only that child to the end of the cross axis while the others stay where align-items put them.

Is there a justify-self in flexbox?

No. Flexbox ignores justify-self, because the main axis is shared by all items. To push one item along the main axis, use an auto margin, such as margin-inline-start: auto. justify-self works in grid.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED