Sub topic 5
Aligning items on the cross axis
This chapter assumes you know the main axis and cross axis, display: flex, flex-direction, and justify-content. You also need the box model and basic length units. Here you will learn how align-items positions items on the cross axis, and why its results sometimes look surprising.
What align-items controls
align-items is set on the flex container. It decides where each item sits on the cross axis, which is the axis perpendicular to the main axis. With flex-direction: row, the cross axis is vertical. With flex-direction: column, it is horizontal.
justify-content distributes items along the main axis. align-items aligns them across it. The two properties never swap names when the direction changes. Only the physical directions they act on change.
The values
| Value | Effect on the cross axis |
|---|---|
| `stretch` | Items grow to fill the line's cross size. This is the default behavior. |
| `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 by the baseline of their first line of text. |
The initial value is normal, which behaves like stretch for flex items. That is why the children of a bare display: flex container all end up the same height.
stretch and its conditions
Stretching only happens when the item's cross-size property is auto. In a row container, an item with height: 80px keeps that height. Stretching also respects max-height, so an item stops growing at its maximum.
.row {
display: flex;
align-items: stretch;
}
.row .fixed {
height: 80px; /* not stretched */
}Auto margins on the cross axis also cancel stretching for that item. The margin absorbs the free space instead.
flex-start, flex-end and center
These three place items without changing their size. Each item keeps its natural cross size and is positioned inside the available cross space.
.nav {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}In this navigation bar, justify-content pushes the first and last items to the edges. align-items: center centers every item vertically, so a tall logo and short links share a midline.
baseline
Centering text of different sizes can look wrong, because the letters do not sit on the same line. baseline aligns the first-line text baselines instead.
.price {
display: flex;
align-items: baseline;
gap: 0.25rem;
}
.price .amount {
font-size: 2rem;
}
.price .currency {
font-size: 1rem;
}With center, the small currency symbol would float at the middle of the large number. With baseline, both sit on the same text line.
The free space must exist
align-items distributes free space on the cross axis. In a single-line row container with no explicit height, the container is only as tall as its tallest item. There is no free space, so center and flex-end appear to do nothing for that item.
Give the container a cross size, such as height or min-height, to create room.
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}Here min-height: 100vh makes the container as tall as the viewport. The single child is centered both ways: justify-content handles horizontal, and align-items handles vertical.
Column containers
With flex-direction: column, align-items works horizontally.
.stack {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 0.5rem;
}Each child is as wide as its content and sits at the left edge, in left-to-right writing modes. With the default stretch, children would be as wide as the container. This explains a common surprise: buttons inside a column container become full width until you set align-items: flex-start, or use align-self on individual items.
Wrapping containers
When flex-wrap: wrap creates several lines, align-items aligns items inside each line, not across the whole container. Each line is only as tall as its tallest item (assuming the container has spare space and the default align-content for wrapped lines). Distributing the lines themselves is the job of align-content, which a later chapter covers.
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
align-items: flex-start;
}
.card {
flex: 1 1 200px;
}Without align-items: flex-start, cards in the same line stretch to equal height. With it, each card keeps its own content height.
Overflow and safe alignment
Centering has an edge case. If an item is larger than the container on the cross axis, center pushes it equally past both edges. In a row container, the top part can overflow out of reach, because content cannot be scrolled to above the start edge.
Modern browsers support the safe keyword, which falls back to flex-start when the item would overflow.
.modal-host {
display: flex;
justify-content: center;
align-items: safe center;
height: 100vh;
overflow: auto;
}Check browser support for safe before depending on it. An alternative that works everywhere is to put margin: auto on the item. Auto margins never cause unreachable overflow, because they only absorb positive free space.
Common errors
- Setting
align-itemson an item instead of the container. It has no effect there. Usealign-selfon an item. - Expecting
align-itemsto move items along the main axis. That isjustify-content. - Forgetting that a fixed
heighton an item disables stretching. - Using
centeron a container with no extra height. - Using
centerfor mixed text sizes whenbaselinegives cleaner results.
Exercise
A row container has display: flex; align-items: center; and holds one item. The item sits at the top of the container, not the middle, and the container is exactly as tall as the item. What is the cause, and what is one fix?
Show answer
The container's height comes from its content, so there is no free cross space to center within. Give the container a height, for example min-height: 100vh or height: 300px, and the item will center vertically.
Exercise
In a flex-direction: column container, all buttons stretch to the full container width. Which single declaration on the container makes each button only as wide as its label, and where do the buttons sit?
Show answer
align-items: flex-start. In a column container, the cross axis is horizontal, so the buttons shrink to their content width and sit at the left edge (in left-to-right languages).