日本語

CSS justify-content vs align-items: Differences in Flexbox

​​CSS​Flexbox,Playwright

This article was translated from the Japanese original with AI. 日本語の原文を読む

justify-content and align-items, set on a display: flex parent, both control where the children are placed, but they work along different axes. justify-content controls the position along the direction in which the children line up, and align-items controls the position along the perpendicular direction. When the children are in a row, justify-content is what you use to right-align buttons or space menu items evenly, and align-items is what you use to vertically center an icon and its text.

css
.box {
  display: flex;
  justify-content: center; /* position along the row (horizontal) */
  align-items: center;     /* position perpendicular to the row (vertical) */
}
Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened a page in Playwright with three 80px-wide children inside a 400px-wide parent (no padding or border), and measured the position and size of the children with getBoundingClientRect(). Unless noted otherwise, the child heights were 40px, 60px, and 80px. I did not test Firefox or Safari, vertical writing modes, right-to-left languages, or multi-line layouts with flex-wrap: wrap.

The main axis is the direction of the row, and the cross axis is the perpendicular direction

In flexbox, the direction in which children are laid out is called the main axis, and the perpendicular direction is called the cross axis (terminology in the CSS Flexbox specification). MDN's page on justify-content says that justify-content controls how space is distributed between and around the children along the main axis. MDN's page on align-items says that in flexbox, it controls alignment along the cross axis.

The initial value of flex-direction is row, so on a horizontal-writing page the main axis is horizontal and the cross axis is vertical. If you specify neither property, both computed values are normal in Chromium, and the children pack from the top left (child 1 at x=0, child 2 at x=80, child 3 at x=160, and y is 0 for all of them). According to MDN, normal for justify-content behaves like start in a flex container.

justify-content decides where the leftover space along the main axis goes

With three 80px children in a 400px parent, there are 160px of leftover space horizontally. justify-content decides whether that space goes before, between, or after the children. I changed the value and measured each child's left edge (x). The results were:

justify-content Child 1 Child 2 Child 3 Where the leftover space goes
flex-start 0 80 160 All on the right
center 80 160 240 80px on each side
flex-end 160 240 320 All on the left
space-between 0 160 320 80px between each pair of children
space-around 26.66 159.98 293.33 About 26.7px at each end, about 53.3px between children
space-evenly 40 160 280 40px at each end and between children
A six-row diagram. Inside a light blue parent outlined with a gray dotted line, light blue boxes 1, 2, and 3 of different heights sit side by side in a row. With flex-start they are pushed to the left; with center they are in the middle; with flex-end they are pushed to the right. With space-between the outer boxes touch the edges of the parent; with space-around and space-evenly there is space at the ends too. In every row the boxes are aligned at the top edge.
Changing the justify-content value changes only the horizontal position (parent width 400px)

In every case, y stayed at 0. Changing justify-content does not move the children vertically.

space-between, space-around, and space-evenly differ in how they treat the ends. The CSS Box Alignment specification says that space-between places the first and last children flush with the edges of the parent, space-around puts half of the between-gap at each end, and space-evenly puts the same gap at the ends as between children. The measured values match this: with space-around, the gap at each end is 160÷6 and the gap between children is 160÷3.

align-items sets the position along the cross axis

align-items controls the position along the cross axis. When the children are in a row, that means the vertical position.

If you don't set the parent's height, the difference is hard to see. The parent's height then matches the tallest child, child 3, which is 80px. With align-items: center, child 1's y is 20 and child 2's y is 10, so they are centered within 80px. Child 3 is the same height as the parent, so it has no room to move.

I set height: 200px on the parent and changed the value. The child y values were:

align-items Child 1 (height 40) Child 2 (height 60) Child 3 (height 80)
flex-start 0 0 0
center 80 70 60
flex-end 160 140 120

In every case, x stayed at 0, 80, and 160, so the horizontal positions did not change.

A four-row diagram. Inside a tall gray dotted parent, light blue boxes 1, 2, and 3 sit side by side, pushed to the left. With flex-start they align at the top, with center they are vertically centered, and with flex-end they align at the bottom. The last row shows the case where align-items is not written: boxes 1 and 2, which have no height set, stretch to the full height of the parent, while box 3, which has an 80px height, stays at the top.
Changing the align-items value changes only the vertical position (parent height 140px)

If you don't write it, children without a height stretch

If you don't write align-items, its computed value is normal. MDN's explanation of align-items says that for flex items, normal behaves as stretch. The stretch explanation in the specification says that only children whose cross-size (height when the children are in a row) is auto will stretch, and children with auto cross-axis margins are excluded.

In a 200px-tall parent, I did not set height on child 1 or child 2, and set height: 80px only on child 3. Child 1 and child 2 stretched to 200px, and child 3 stayed at 80px.

When I added align-items: center to the same children, child 1 and child 2 stopped stretching, and their height became 16px, the height of the text inside them (y was 92). So if you want cards to be the same height, leave align-items unset. If you want each item to keep its content height and sit in the middle, use center.

baseline aligns the text baselines

align-items: baseline aligns the baselines of text inside the children (the line the text sits on). I placed children with font sizes of 16px, 32px, and 48px in a row with flex-start. Their y values were all 0, so their top edges were aligned. With baseline, their y values were 27, 13, and 0. As the baseline explanation in the specification describes, the child with the largest distance from its baseline to its top edge (here, the 48px child 3) sits at the top edge, and the other children are moved down until their baselines line up. This is useful when you place headings and annotations of different sizes side by side.

flex-direction: column swaps the axes

justify-content and align-items do not act on "horizontal" and "vertical". They act on the main and cross axes. The flex-direction explanation in the specification says that for row, the main axis is the inline direction (the direction text flows), and for column, the main axis is the block direction (the direction blocks stack). On a horizontal-writing page, column makes the main axis vertical.

I set the parent to 200px tall, with justify-content: center and align-items: flex-end. When I changed only flex-direction, the children were positioned as follows:

flex-direction Child 1 Child 2 Child 3
row (heights 40, 60, 80px) x=80, y=160 x=160, y=140 x=240, y=120
column (each 40px tall) x=320, y=40 x=320, y=80 x=320, y=120

With row, the children are centered horizontally and sit at the bottom edge. With column, they stack vertically, are centered vertically, and sit at the right edge. The same declarations now apply to different axes: center moves from horizontal to vertical, and flex-end moves from vertical to horizontal.

A two-row diagram. Both rows use justify-content: center and align-items: flex-end. In the top row, boxes 1, 2, and 3 sit side by side in a row, centered horizontally and aligned to the bottom edge. In the bottom row, with flex-direction: column, the same three boxes are stacked vertically, centered vertically, and aligned to the right edge.
With the same declarations, changing only flex-direction swaps the axes that center and flex-end act on

If you want to center vertically in a column layout and it doesn't move, check the parent's height. When I added column and justify-content: center without setting the parent's height, the parent's height became the sum of the children, 180px, and the children's y stayed at 0, 40, and 100. There is no leftover space on the main axis, so justify-content has nothing to move.

In a column layout, the direction in which children stretch when align-items is not written also changes to horizontal. Children without a width stretched to the parent's 400px width. A child with width: 80px stayed at 80px.

When justify-content does nothing

justify-content only distributes leftover space along the main axis. If there is no leftover space, changing the value does not move anything.

When I set flex-grow: 1 on the children, each child grew to 133.33px and filled the parent's width. Even with justify-content: center, child 1's x stayed at 0, because the growing children used up all the leftover space.

justify-content also stops having an effect when a child has margin-left: auto. In a parent with justify-content: center, when I set margin-left: auto on child 3, children 1 and 2 were left-aligned at x=0 and x=80, and only child 3 moved to the right edge at x=320. According to the specification's explanation of auto margins, leftover space on the main axis is first given to auto margins before justify-content is applied. To place only the last menu item at the right edge, margin-left: auto is a better fit than justify-content.

Moving a single child

You can move one child along the cross axis by setting align-self on that child. In a 200px-tall parent with align-items: flex-start, I set align-self: flex-end only on child 3. Child 1 and child 2 stayed at y=0, and child 3 moved to the bottom edge at y=120.

There is no per-child property for the main axis. Setting justify-self: flex-end on child 3, or justify-items: center on the parent, left the children at x=0, 80, and 160. The justify-self explanation in the Box Alignment specification says that justify-self has no effect on flex items because there are multiple items along the main axis. To move just one child along the main axis, use auto margins as described in the previous section.

Direction to move (when children are in a row) Set on the parent for all children Set on each child
Main direction (horizontal) justify-content Auto margins (such as margin-left: auto)
Cross direction (vertical) align-items align-self

​Posted by 86