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:
| Value | Free space goes | Gaps in the demo |
|---|---|---|
flex-start (and start) | all after the items | 0 / 0 / 0 / 240 |
center | half before, half after | 120 / 0 / 0 / 120 |
flex-end (and end) | all before the items | 240 / 0 / 0 / 0 |
space-between | only between items | 0 / 120 / 120 / 0 |
space-around | equal on both sides of each item, so edges get half | 40 / 80 / 80 / 40 |
space-evenly | every gap equal, edges included | 60 / 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:
| Value | What 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 |
center | Items are centered, at their own height |
flex-end (and end) | Items sit at the bottom |
baseline | Items 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-contentis the flow direction,align-itemsis across it. In a column, vertical centering isjustify-content: center. - No free space. An item with
flex: 1takes all the space, sojustify-contenthas none to distribute. The same happens when the container isinline-flexand exactly as wide as its items. - No height for
align-items. A container as tall as its content leaves nothing to align. Set aheightormin-height. - Looking for
justify-selfin flexbox. It is ignored there. Usemargin-inline-start: auto(ormargin-left: auto) on the item. align-items: stretchthat does not stretch. An item with its ownheightkeeps it. Remove the height (usemin-heightif it needs a minimum).- Putting the properties on the items. Both go on the flex container. Only
align-selfgoes 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.