Skip to content
Kuza Ndoto Academy

Flexbox

Beginner → IntermediateChapter 3 · Page 11 of 224 min readUpdated 15 Nov 2026

Flexbox (the Flexible Box Layout) arranges items in a single row or column and shares out the space between them. It makes once-painful tasks easy: centring something vertically, building a navigation bar, or making three cards the same height.

Flex containers and flex items

Set display: flex on a parent and it becomes a flex container. Its direct children become flex items and line up in a row.

<nav class="menu">
  <a href="#">Home</a>
  <a href="#">Shop</a>
  <a href="#">Contact</a>
</nav>
.menu {
  display: flex;
  gap: 1rem;
}

The three links now sit side by side with 1rem of space between them. Only direct children are affected; grandchildren keep their normal behaviour.

Two axes

Flexbox thinks in terms of two axes. The main axis runs in the direction the items are laid out, set by flex-direction: row (default, left to right in English), row-reverse, column (top to bottom) or column-reverse. The cross axis runs at right angles to it.

flex container (display: flex) 1 2 3 main axis (flex-direction: row) justify-content main start main end cross axis align-items cross start cross end
Figure 1. With flex-direction: row the main axis is horizontal and the cross axis vertical. Switch to column and they swap, so justify-content then works vertically.

Aligning items

justify-content: along the main axis

It decides what to do with leftover space along the main axis.

flex-start packed at the start center packed in the middle space-between gaps only between space-around half-size gaps at edges space-evenly every gap equal
Figure 2. The container is 260 units wide and each item 50, leaving 110 units of free space. Each value shares that space differently along the main axis.

align-items: along the cross axis

align-items positions items across the line. The default, stretch, makes every item as tall as the tallest one in a row, which is why flex cards line up so neatly. Other values are flex-start, center, flex-end and baseline (aligns the first line of text). A single item can override it with align-self.

The famous "centre anything" recipe is just two lines on the parent:

.hero {
  display: flex;
  justify-content: center;   /* horizontal centre (main axis) */
  align-items: center;       /* vertical centre (cross axis) */
  min-height: 60vh;
}

Wrapping and gap

By default items stay on one line and shrink to fit. flex-wrap: wrap lets them flow onto new lines instead, which is great for a row of product cards on a phone. gap (or row-gap and column-gap) puts space between items without adding margin to the outer edges. When items wrap onto several lines, align-content controls how those lines are spaced in the container.

Flexible sizes: grow, shrink and basis

Three item properties decide how items share space:

  • flex-grow: how much of the leftover space an item takes (0 by default, so items don't grow).
  • flex-shrink: how much an item shrinks when space runs out (1 by default).
  • flex-basis: the item's starting size before growing or shrinking (auto by default, meaning its width or content size).

Use the flex shorthand. flex: 1 means grow 1, shrink 1, basis 0%, so all items with flex: 1 end up the same width. flex: 2 on one item gives it twice the share of the free space. A search box that fills all remaining room next to a button is just input { flex: 1; }. The order property changes visual order (default 0), but screen readers and keyboards still follow the HTML order, so use it sparingly.

Try it

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Flexbox</title>
  <style>
    body { font-family: system-ui, sans-serif; margin: 0; }
    .navbar {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0.75rem 1rem;
      background: #1e3a8a;
      color: white;
    }
    .navbar ul {
      display: flex;
      gap: 1rem;
      list-style: none;
      margin: 0;
      padding: 0;
    }
    .navbar a { color: white; text-decoration: none; }
    .cards {
      display: flex;
      flex-wrap: wrap;
      gap: 1rem;
      padding: 1rem;
    }
    .card {
      flex: 1 1 180px;
      padding: 1rem;
      background: #e0f2fe;
      border-radius: 8px;
    }
  </style>
</head>
<body>
  <nav class="navbar">
    <strong>Soko Fresh</strong>
    <ul>
      <li><a href="#">Shop</a></li>
      <li><a href="#">Delivery</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
  <section class="cards">
    <div class="card">Fresh vegetables delivered daily.</div>
    <div class="card">Pay with M-Pesa on delivery.</div>
    <div class="card">Free delivery on orders over KES 1,500 within Nairobi.</div>
  </section>
</body>
</html>

A dark blue navigation bar shows "Soko Fresh" on the left and three white links on the right, all vertically centred. Below, three pale blue cards share the row equally and stretch to the same height, even though the third has more text. Make the preview narrow and the cards wrap onto new lines, because each card won't shrink below its 180px basis.

Key points

  • display: flex on a parent lays out its direct children along one axis.
  • justify-content works on the main axis; align-items on the cross axis.
  • flex-wrap: wrap and gap create responsive rows without media queries.
  • flex: 1 makes items share space equally; use Grid when you need rows and columns.
Up next · LayoutCSS GridTwo-dimensional layouts with rows and columns.
→

Tip: use the ← and → keys to move between pages.