edubun
English
1 / 13

6 / 9 · Basic

Flexbox

Before flexbox, centring a box vertically took tricks. With display: flex, it takes two declarations: justify-content and align-items.

Jump to the 10 practice questions

You will learn

  • Lay boxes along one line with display: flex
  • Align on the main and cross axes and wrap with gap
  • Share space with flex-grow, -shrink and -basis

Before this: Typography

1Explain

One row or one column

Flexbox, the CSS Flexible Box Layout module, arranges a group of boxes along one line. Put display: flex on a container and its direct children, the flex items, line up side by side instead of stacking. That one declaration is enough for a menu bar, a row of buttons, or a card whose footer should stick to the bottom. Flexbox works in one dimension at a time: either a row or a column.

In a row, the main axis runs left to right (in English) and the cross axis top to bottom. justify-content works along the main axis, align-items along the cross axis.

2Explore

Direction and justify-content

Everything in flexbox is described in terms of two axes. The main axis is the direction the items flow, set by flex-direction: row (the default) or column. The cross axis runs at right angles to it. justify-content distributes the items along the main axis: flex-start, center, flex-end, space-between (first and last item at the edges, equal gaps between) or space-around.

justify-content works along the direction the items flow, so in a column it moves them up and down.

Tap a choice: the code and the preview change together.

flex-direction
justify-content
<style>
  .row { display: flex; flex-direction: row; justify-content: flex-start; align-items: center; gap: 8px; height: 150px; background: #fff4d6; }
  .row div { background: #e8eefa; padding: 6px 12px; }
</style>
<div class="row"><div>Bread</div><div>Cakes</div><div>Visit</div></div>
Preview

3Problem

Spread items in a row

In a row, which property spreads items horizontally?

Hint

In a row the main axis runs left to right.

Solution

Answer: justify-content

justify-content works along the main axis, which is horizontal in a row.

4Problem

justify-content in a column

With flex-direction: column, what does justify-content control?

Hint

The main axis turns with flex-direction.

Solution

Answer: Vertical distribution

justify-content follows the main axis. With flex-direction: column, that axis runs vertically, so it distributes space from top to bottom.

5Explain

Gaps and wrapping

gap puts space between items without adding any at the outer edges, which is what you usually want and what margins make awkward. By default, flex items stay on one line and shrink to fit, even if that squashes them. flex-wrap: wrap lets them move onto a new line when there is not enough room. So a row of four cards on a laptop can become two rows of two on a tablet and a single column on a phone, with no media query.

Your code
.bar {
  display: flex;
  justify-content: space-between;  /* logo left, menu right */
  align-items: center;             /* vertically centred */
  gap: 16px;                       /* space between items */
}
.cards { display: flex; flex-wrap: wrap; gap: 12px; }
.card  { flex: 1 1 200px; }        /* grow, shrink, start at 200px */
Preview

6Explore

How items grow

The flex shorthand on an item takes three values. flex-grow says how much of the leftover space the item takes compared with its siblings; flex-shrink says how willingly it shrinks when space runs out; flex-basis is its starting size before growing or shrinking.

With flex-basis 0, the leftover space is shared in proportion to flex-grow: 2 against 1 and 1 gives Big half.

Tap a choice: the code and the preview change together.

flex-grow of Big
<style>
  .row { display: flex; background: #fff4d6; }
  .row div { flex: 1 1 0; background: #e8eefa; border: 1px solid #fff; padding: 8px; }
  .row .big { flex-grow: 1; }
</style>
<div class="row"><div class="big">Big</div><div>B</div><div>C</div></div>
Preview

7Problem

Four equal items

Four items have flex: 1 in a 400px container with no gaps. How wide is each?

px

Hint

400 shared four ways.

Solution

Answer: 100 px

flex: 1 means a basis of 0 and equal growth: 400 / 4 = 100.

8Problem

flex-grow 2 against 1 and 1

Three items with flex-basis 0 share 400px. One has flex-grow 2, the others 1. How wide is the big one?

px

Hint

There are 4 shares in total.

Solution

Answer: 200 px

Shares 2 + 1 + 1 = 4; 400 times 2/4 = 200.

9Problem

Visual order and keyboard order

Flexbox can change the order items appear in, with flex-direction: row-reverse or the order property, without touching the HTML. Use this with care. Keyboard focus and screen readers still follow the order in the HTML, so if the screen shows Contact before Home but Tab visits Home first, keyboard users get lost. When the order matters to meaning, change it in the HTML.

The order property puts Contact first on screen, but it is last in the HTML. What still visits it last?

Hint

The keyboard follows the HTML.

Solution

Answer: Keyboard focus and screen readers

They follow the HTML order, so visual and real order disagree.

10Practice

A header bar

Make .bar a flex container with the name on the left and the menu on the right (space-between), vertically centred. Then make .menu a flex row with a 16px gap.

Checks
  • .bar is flex
  • space-between
  • centred vertically
  • .menu is flex
  • 16px gap
Your code
<style>
  .bar { background: #fff4d6; padding: 8px 16px; }
  .bar ul { list-style: none; margin: 0; padding: 0; }
</style>

<header class="bar">
  <strong>Corner Bakery</strong>
  <ul class="menu">
    <li><a href="#">Bread</a></li>
    <li><a href="#">Cakes</a></li>
    <li><a href="#">Visit</a></li>
  </ul>
</header>
Preview
Hint

Two flex containers: .bar for the whole header, .menu for the links.

A solution
<style>
  .bar { background: #fff4d6; padding: 8px 16px; display: flex; justify-content: space-between; align-items: center; }
  .menu { display: flex; gap: 16px; }
  .bar ul { list-style: none; margin: 0; padding: 0; }
</style>

<header class="bar">
  <strong>Corner Bakery</strong>
  <ul class="menu">
    <li><a href="#">Bread</a></li>
    <li><a href="#">Cakes</a></li>
    <li><a href="#">Visit</a></li>
  </ul>
</header>

11Practice

Cards that wrap

Make .cards a wrapping flex container with a 12px gap, and give each .card flex: 1 1 120px. The cards move onto a new line whenever there is no room for 120 pixels each.

Checks
  • flex container
  • flex-wrap: wrap
  • 12px gap
  • cards grow
  • basis 120px
Your code
<style>
  .card { background: #e8eefa; padding: 12px; }
</style>

<div class="cards">
  <div class="card">Sourdough</div>
  <div class="card">Rye</div>
  <div class="card">Baguette</div>
  <div class="card">Brioche</div>
</div>
Preview
Hint

flex-wrap: wrap lets items move onto a new line.

A solution
<style>
  .card { background: #e8eefa; padding: 12px; }
  .cards { display: flex; flex-wrap: wrap; gap: 12px; }
  .card { flex: 1 1 120px; }
</style>

<div class="cards">
  <div class="card">Sourdough</div>
  <div class="card">Rye</div>
  <div class="card">Baguette</div>
  <div class="card">Brioche</div>
</div>

12Explain

Common mistakes

Mistake
<style>
  .menu li { display: flex; gap: 16px; }
</style>
<ul class="menu"><li>Bread</li><li>Cakes</li></ul>
Fixed
<style>
  .menu { display: flex; gap: 16px; list-style: none; padding: 0; }
</style>
<ul class="menu"><li>Bread</li><li>Cakes</li></ul>

display: flex goes on the container. On each li it only arranges the li's own content.

13

Summary

  • display: flex lines up the direct children along the main axis.
  • justify-content on the main axis, align-items on the cross axis, gap between.
  • flex: grow shrink basis; flex-wrap: wrap for rows that reflow; keep visual order equal to HTML order.

Next unitGrid basics
Full text · The whole lesson as one text

One row or one column

Flexbox, the CSS Flexible Box Layout module, arranges a group of boxes along one line. Put display: flex on a container and its direct children, the flex items, line up side by side instead of stacking. That one declaration is enough for a menu bar, a row of buttons, or a card whose footer should stick to the bottom. Flexbox works in one dimension at a time: either a row or a column. When you need rows and columns together, grid, in the next unit, is usually the better tool.

In a row, the main axis runs left to right (in English) and the cross axis top to bottom. justify-content works along the main axis, align-items along the cross axis.

Two axes

Everything in flexbox is described in terms of two axes. The main axis is the direction the items flow, set by flex-direction: row (the default) or column. The cross axis runs at right angles to it. justify-content distributes the items along the main axis: flex-start, center, flex-end, space-between (first and last item at the edges, equal gaps between) or space-around. align-items positions them on the cross axis: stretch (the default, all items as tall as the tallest), flex-start, center or flex-end. So a box is centred in both directions with justify-content: center and align-items: center.

.bar {
  display: flex;
  justify-content: space-between;  /* logo left, menu right */
  align-items: center;             /* vertically centred */
  gap: 16px;                       /* space between items */
}
.cards { display: flex; flex-wrap: wrap; gap: 12px; }
.card  { flex: 1 1 200px; }        /* grow, shrink, start at 200px */
A header bar and a row of cards that wraps onto new lines.

Gaps and wrapping

gap puts space between items without adding any at the outer edges, which is what you usually want and what margins make awkward. By default, flex items stay on one line and shrink to fit, even if that squashes them. flex-wrap: wrap lets them move onto a new line when there is not enough room. So a row of four cards on a laptop can become two rows of two on a tablet and a single column on a phone, with no media query.

How items grow and shrink

The flex shorthand on an item takes three values. flex-grow says how much of the leftover space the item takes compared with its siblings; flex-shrink says how willingly it shrinks when space runs out; flex-basis is its starting size before growing or shrinking. flex: 1 1 200px means start at 200 pixels, then share extra space equally with other items that also have a grow of 1. flex: 1 on its own is short for 1 1 0%, which makes all items equal widths. Give one item flex-grow: 2 and the others 1, and it takes twice their share of the leftover space.

Visual order and real order

Flexbox can change the order items appear in, with flex-direction: row-reverse or the order property, without touching the HTML. Use this with care. Keyboard focus and screen readers still follow the order in the HTML, so if the screen shows Contact before Home but Tab visits Home first, keyboard users get lost. When the order matters to meaning, change it in the HTML.

Test alignment by changing only the direction of a flex container. Start with a row and keep justify-content set to center. Before switching to column, predict which direction the items will move. The main axis turns with the direction, so centring now happens vertically. Leave align-items unchanged and compare the result. Changing one declaration at a time makes it easier to see which axis each property controls.

Key points
  • display: flex lines up the direct children along the main axis.
  • justify-content on the main axis, align-items on the cross axis, gap between.
  • flex: grow shrink basis; flex-wrap: wrap for rows that reflow; keep visual order equal to HTML order.

Practice questions from Flexbox on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (5)
  • W3C CSS Working Group specifications: CSS Snapshot 2025, Selectors Level 4, CSS Cascading and Inheritance Level 5, CSS Color Level 4, CSS Values and Units Level 4, CSS Box Model Level 3, CSS Box Sizing Level 3, CSS Fonts Level 4, CSS Flexible Box Layout Level 1, CSS Grid Layout Level 2, Media Queries Level 4 and Level 5 (w3.org/TR/, checked 2026-10-17)
  • MDN Web Docs, Learn web development: CSS styling basics and CSS layout, and the CSS reference (developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics, .../Core/CSS_layout, developer.mozilla.org/en-US/docs/Web/CSS, checked 2026-10-17)
  • W3C, CSS history and CSS1 Recommendation of 17 December 1996 (w3.org/Style/CSS20/history.html, w3.org/press-releases/1996/css1-rec, checked 2026-10-17)
  • W3C WCAG 2.2, success criteria 1.4.3 Contrast (Minimum)
  • 1.4.8 Visual Presentation, 1.4.10 Reflow, 1.4.12 Text Spacing (w3.org/TR/WCAG22/, checked 2026-10-17).

Checked: 2026-10-17