liveBook · Manning

Align-items

As explained in CSS in Depth, Second Edition

The CSS align-items property specifies the default cross-axis alignment of items within a flex or grid container.

The CSS align-items property sets the default alignment of flex or grid items along the cross axis. In a flex row, that axis is vertical; in a flex column, it is horizontal. Its initial value, stretch, makes items fill the container’s cross-axis size, producing equal-height columns in a row layout. section 4.4.1 section 5.6

Choose a value according to the visible alignment you need:

  • Use flex-start or flex-end to place items at the cross-axis start or end; use start or end for logical alignment based on the container’s writing mode. section 4.4.1
  • Use center to center items, or baseline to line up the first text baselines across items with different font sizes. Add first or last to select the text line. section 4.4.1
  • Remember that align-items sets the default for every flex item; align-self can override that default for one item. section 4.4.3 section 4.5

Aligning a stylized price

A price can use flexbox to center its parts while aligning them vertically: .cost { display: flex; justify-content: center; align-items: center; line-height: 0.7; }. The larger dollar amount, currency symbol, and cents then share a centered cross-axis alignment. The cents can opt out with .cost-cents { align-self: flex-start; }, placing it at the cross-axis start. section 4.4.3

Aligning navigation controls

For a navigation row whose links no longer fill the container’s height, .nav-container__inner { display: flex; justify-content: space-between; align-items: end; } places the flex items at the bottom, removing the unwanted space beneath them. section 12.6.2

Listing 4.11 Setting styles for the cost tile
.centered {
  text-align: center;
}

.cost {
  display: flex;
  justify-content: center;     #1
  align-items: center;         #1
  line-height: 0.7;
}

.cost-currency {
  font-size: 2rem;       #2
}
.cost-dollars {
  font-size: 4rem;        #2
}
.cost-cents {
  font-size: 1.5rem;      #2
  align-self: flex-start;     #3
}

.cta-button {
  display: block;
  background-color: #cc6b5a;
  color: white;
  padding: 0.5em 1em;
  text-decoration: none;
}

Inspect the complete cost-tile stylesheet, including centered flex alignment, varied font sizes, line-height adjustment, and the align-self override. section 4.4.3

How CSS in Depth, Second Edition covers it

How deep it goes
Defines the cross-axis rule, common values, writing-mode behavior, baselines, and item-level overrides through concrete layouts.
Examples
Demonstrates equal-height flex columns, a stylized $20.00 price, bottom-aligned navigation links, and grid alignment properties.
What it assumes
Assumes basic flex-container and flex-item concepts while distinguishing cross-axis alignment from main-axis justification.
What you can do after
Apply cross-axis alignment to flex and grid layouts, then override one flex item with align-self when needed.
How it connects
Connects cross-axis alignment with flexbox, grid layout, justify-content, align-content, align-self, writing modes, and text baselines.

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 CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.