← Web basics: HTML, CSS and JavaScript

Flexbox basics

Line boxes up in a row or column with display: flex.

Start this lesson

Flexbox is the easiest way to put boxes side by side. You put display: flex on the parent (the container), and its direct children become flex items that line up in a row. Then the container controls them: gap sets the space between items, justify-content spreads them along the row (flex-start, center, space-between), and align-items lines them up across it (for a row: top, center or bottom). flex-direction: column stacks them instead, and flex-wrap: wrap lets them flow onto new lines when space runs out.

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

<div class="toolbar">
  <strong>Pet Shelter</strong>
  <a href="#adopt">Adopt</a>
  <a href="#help">Volunteer</a>
</div>

The three children sit in one row. space-between pushes the first to the left edge and the last to the right edge, align-items: center lines them up vertically, and gap guarantees at least 12px between them.

A common mistake is putting display: flex on the items. It goes on the parent whose children you want to arrange.

justify-content works along the main direction (across a row), align-items across it. In a column, they swap.