liveBook · Manning

Align-items

As explained in Web Design Playground, Second Edition

The CSS align-items property sets the default alignment of flex items along the cross axis of a flex container.

`align-items` is a Flexbox container property for lining up all its flex items along the cross axis. Think of Flexbox as arranging items on two tracks: `justify-content` handles the main axis, while `align-items` handles the other one. It is the handy tool for making a row of differently sized items sit at the top, bottom, middle, or stretch to fit. section 12.2 section 12.7

Set the property on the element with `display: flex`, not on each individual item. Its default value is `stretch`, so flex items expand along the cross axis to fill the container unless another alignment is chosen. No declaration is needed when stretching is exactly what you want—CSS has already clocked in for that shift. section 12.2 section 12.6

Choose one of these values for the container:

  • `stretch` expands each item along the cross axis to fill the container; it is the default. section 12.2
  • `flex-start` places items at the beginning of the cross axis, and `flex-end` places them at its end. section 12.2
  • `center` puts items in the middle of the cross axis. section 12.2
  • `baseline` lines items up using the baseline of the first line of text in the flex container. section 12.2
Figure 12.5 The align-items values in action

Compare the five values visually. The container outline makes the important part clear: alignment happens inside the container’s cross-axis space. section 12.2

Dead-center one item

To center an `h1` both across and down the browser window, make its parent a flex container, give that parent the viewport height, then use `justify-content: center` and `align-items: center`. The first property centers on the main axis; `align-items` supplies the vertical half of the trick when the main axis is horizontal. section 12.3

div {
    display: flex;
    justify-content: center;         #1
    align-items: center;             #2
    height: 100vh;                   #3
}

This compact rule set turns a `div` into a full-window flex container and centers its heading on both axes. section 12.3

In page layouts, `align-items: center` keeps header logos, titles, and navigation content centered vertically. The default `stretch` can also make neighboring flex items the same height, which is useful for a full-height sidebar without extra CSS tricks. For a single item that needs different alignment from its siblings, use `align-self` instead. section 15.6 section 12.6 section 12.7

How Web Design Playground, Second Edition covers it

How deep it goes
Defines five values and demonstrates centering and default stretching, without detailing layout internals.
Examples
Uses a centered browser-window heading, gallery headers and navigation, and a full-height sidebar.
What you can do after
Apply it via Flexbox to center items, edge-align them, align text baselines, or stretch items across a container.
How it connects
Connects Flexbox alignment to justify-content, flex-direction, align-self, flex-wrap, and align-content.

Also explained in

Compare how 4 books explain align-items →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.