Putting one box exactly in the middle, both top to bottom and left to right, used to be famously hard in CSS. With Flexbox, two properties do it: justify-content and align-items.
Both control alignment: where the items sit inside their parent, for example at the left, in the middle, or at the bottom. The parent is the element that has display: flex, often called the container. Flexbox places items along two axes. The main axis is the line that the items sit along: with row (the default), from left to right. The cross axis crosses the main axis at a right angle: from top to bottom. justify-content sets where the items sit along the main axis, and align-items along the cross axis.
Top, middle, bottom with align-items
This CSS rule puts every item in the middle of its parent's height:
.container {
display: flex;
align-items: center;
height: 150px;
}
display: flexlines up every child of.containerside by side in one row.height: 150pxmakes the parent taller than its items, so there is empty space above and below the items.align-items: centerputs each item in the middle of the parent's height, with the same amount of empty space above and below it.
Change the value of align-items and the items move:
flex-start: the items sit at the top.flex-end: the items sit at the bottom.stretch: the default. Items with noheightare stretched to the height of the parent.baseline: the text baseline (the line the letters sit on) of every item is lined up, even when the font sizes differ.
Exactly in the middle: justify-content and align-items
justify-content works on the main axis. Some of its values are flex-start (left), center (middle), flex-end (right) and space-between (the first and last items sit at the two edges). Combine justify-content with align-items, and a box inside the parent sits exactly in the middle of that parent:
.container {
height: 300px;
display: flex;
justify-content: center;
align-items: center;
}
justify-content: center centres the box left to right, and align-items: center centres it top to bottom. Both properties go on the parent, not on the box. text-align: center is not enough: it only centres the text inside the box, not the box itself.
Stack items with flex-direction: column
Before you read the explanation, guess. In the next rule, the direction of the items changes to column. Does align-items: center centre the items left to right, or top to bottom?
.container {
display: flex;
flex-direction: column;
align-items: center;
}
The answer: left to right. flex-direction: column stacks the items from top to bottom. Both axes turn with it: the main axis now runs from top to bottom, and the cross axis from left to right. align-items still works on the cross axis, so it now sets the position left to right.
New lines with flex-wrap, space with gap
This rule lets items move to a new line and puts space between the items:
.container {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
When you don't write flex-wrap, Flexbox keeps every item on one line, even if the items have to get narrower. flex-wrap: wrap lets items that don't fit move to a new line. gap: 16px puts 16px of space between items, both side by side and between one line and the line below it.
⚠️ Why won't the box go to the middle?
- A parent with no height. Without
height, the parent is only as tall as its content, soalign-items: centerseems to do nothing. - Writing the properties on the box.
justify-contentandalign-itemsgo on the parent, not on the items. - Forgetting the axes turn. With
flex-direction: column,justify-contentworks top to bottom andalign-itemsworks left to right. - Mixing up
align-contentandalign-items.align-contentsets the space between the lines of items, so it only has an effect when there isflex-wrap: wrapand the items fill more than one line.
💡 One item can be different from the rest. Put
align-selfon that item, for examplealign-self: flex-end. For that item only, the value ofalign-selfis used in place of the value ofalign-items.
TL;DR: justify-content sets where the items sit along the main axis (the direction the items line up in, left to right by default). align-items sets where they sit along the cross axis (at a right angle to the main axis, top to bottom by default). Both go on the parent, the element that has display: flex. display: flex; justify-content: center; align-items: center; puts an item exactly in the middle of the parent. flex-direction: column stacks the items from top to bottom. Both axes turn with it, so align-items then works left to right. flex-wrap: wrap lets items that don't fit move to a new line, and gap puts space between the items.
⚡ Exactly in the middle
Write:
.container {
height: 300px;
display: flex;
justify-content: center;
align-items: center;
}
Meaning: .container is the parent: the element that has display: flex and wraps one box. justify-content: center centres that box left to right (the main axis, the direction the items line up in). align-items: center centres the box top to bottom (the cross axis, at a right angle to the main axis). Both go on the parent, not on the box.
⚡ The container needs a height
Guess first: the parent (.container) has no height. What does align-items: center seem to do?
Nothing you can see. Without height, the parent is only as tall as its content, so there is no empty space above and below the items.
Meaning: align-items sets where the items sit between the top and the bottom of the parent, so the parent must be taller than its content. Besides center, it has other values: flex-start puts the items at the top, flex-end puts them at the bottom, and stretch (the default) stretches items with no height to the height of the parent.
⚡ The axes turn with it
Write:
.container {
display: flex;
flex-direction: column;
align-items: center;
}
Guess first: when the direction is flex-direction: column, which way does align-items: center centre the items?
Left to right. The items are now stacked from top to bottom, so the main axis runs top to bottom. The cross axis, where align-items works, runs left to right.
Meaning: When the direction is column, justify-content works top to bottom and align-items works left to right.