Sub topic 5
Aligning items on the cross axis
This chapter assumes you can write a flex container with display: flex, know that the main axis follows flex-direction, and are comfortable with CSS selectors and the box model. Here you will learn how align-items places items on the cross axis, which is the axis perpendicular to the main axis.
How align-items works
In a row container the main axis is horizontal, so the cross axis is vertical. align-items is set on the container. It aligns every item within its flex line along the cross axis. If you switch to flex-direction: column, the cross axis becomes horizontal and the same property now aligns items left to right.
The common values
| Value | Effect on the cross axis |
|---|---|
| `stretch` | Items fill the line's cross size. This is the default. |
| `flex-start` | Items sit at the cross-start edge. |
| `flex-end` | Items sit at the cross-end edge. |
| `center` | Items are centered. |
| `baseline` | Items line up on the baseline of their first line of text. |
stretch only works when the item's cross size is auto. If you set an explicit height on an item in a row container, that item will not stretch.
A worked example
This navigation bar centers its items vertically, so a tall logo and short links share a middle line.
.nav {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}justify-content pushes the first and last items to the edges. align-items: center handles the vertical placement. Centering one child both ways uses the same two properties:
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}The min-height: 100vh matters. The cross axis has room to align items only if the container is taller than its content. Without a height, a row container is only as tall as its tallest item, so center appears to do nothing.
Using baseline
Use align-items: baseline when items have different font sizes and their text should read as one line. A large heading beside a small label will share a text baseline instead of sharing a top or center edge.
Common errors
- Aligning on the wrong axis. Use
justify-contentfor the main axis andalign-itemsfor the cross axis. After you changeflex-direction, swap which one you expect to act horizontally. - A container with no spare cross space. If the container's height equals its content height,
flex-start,centerandflex-endall look identical. - Expecting stretch with a fixed size. A set
height(row) orwidth(column) on an item turns off stretching for that item. - Expecting
align-itemsto move wrapped lines. It aligns items within each line. Spacing between lines is the job ofalign-content, which a later chapter covers.
Exercise
A row container has height: 300px and align-items: center. One child has height: 80px. Where does the child sit, and what changes if you switch the container to flex-direction: column with the same align-items: center?
Show answer
In the row container, the child sits in the vertical middle, with 110px of free space above and below. In the column container, the cross axis is horizontal, so the child is centered horizontally. Its 80px height is now a main-axis size, and align-items no longer affects its vertical position.
Exercise
Your align-items: center rule has no visible effect on a row container whose children have different heights. What is the most likely cause, and how do you fix it?
Show answer
The container is only as tall as its tallest child, so no free cross-axis space exists. Give the container extra height, for example min-height: 100vh, and the shorter items will center.