Flexbox: one axis at a time

Flexbox is a one-dimensional layout tool. You pick a main axis (row or column) and the children line up along it. justify-content controls spacing along the main axis. align-items controls spacing across it. That is the entire mental model.

style.css
css
.nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 2rem;
}

.nav-links {
  display: flex;
  gap: 1.5rem;
  list-style: none;
}

A nav bar with a logo on the left and links on the right, perfectly aligned.

Two flex containers, two layout decisions. The parent uses space-between to push the logo and the link group to opposite ends. The link group uses gap to space its own children evenly. This is the bread-and-butter pattern for almost every navigation bar on the modern web.

Quiz: Quiz

Loading practice…