CSS · Layout

Flexbox

Flexbox is the go-to tool for laying out items in a row or a column — navigation bars, toolbars, card rows and centring things.

Beginner 5 min Lesson 11 of 26 Runnable examples
0/26 in CSS
On this page · 5 min · Beginner

Learning objectives#

  • Create a flex container
  • Explain the main axis and cross axis
  • Align items with justify-content and align-items
  • Control sizing with flex-grow, flex-shrink and flex-basis

Explanation#

Set display: flex on a parent and its direct children become flex items laid out along the main axis (a row by default).

justify-content distributes items along the main axis. align-items aligns them on the cross axis (vertically, for a row).

flex: 1 is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0 — the item grows to fill free space equally with its siblings.

Visual explanation#

Flexbox main axis and cross axis for flex-direction: rowdisplay: flexitem 1item 2item 3main axis → justify-contentcrossaxis ↓align-items
With flex-direction: row, justify-content works along the main axis and align-items along the cross axis.

Syntax#

CSS Illustrative
.container {
  display: flex;
  flex-direction: row;          /* or column */
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}
.item { flex: 1 1 200px; }

Code example: A responsive navigation bar#

HTMLA responsive navigation bar Runnable
<style>
  nav { display:flex; align-items:center; gap:16px; padding:12px 16px; background:#1e1b4b; font-family:sans-serif; }
  .logo { color:#fff; font-weight:700; margin-right:auto; }
  nav a { color:#c7d2fe; text-decoration:none; }
  .btn { background:#2dd4bf; color:#042f2e; padding:6px 12px; border-radius:8px; }
</style>
<nav>
  <span class="logo">Techie Babaji</span>
  <a href="#">Tutorials</a>
  <a href="#">Projects</a>
  <a class="btn" href="#">Start</a>
</nav>
Expected output
The logo sits on the left; the links and the button are grouped on the right, vertically centred.
Opens this example in the full playground

Step-by-step explanation#

  1. display: flex turns <nav> into a flex container; the logo and links become flex items in a row.
  2. align-items: center centres all items vertically.
  3. gap: 16px adds consistent spacing between items without margins.
  4. margin-right: auto on the logo absorbs all free space, pushing the remaining items to the right.

Real-world use case#

Headers, button groups, form rows, media objects (image + text) and centring a single element are all classic Flexbox jobs.

Common mistakes#

Setting flex properties on the wrong element

justify-content and align-items go on the container; flex, align-self and order go on the items.

Confusing axes after changing direction

With flex-direction: column, justify-content works vertically and align-items works horizontally.

Using Flexbox for full page grids

For two-dimensional layouts with rows and columns, CSS Grid is usually simpler.

Best practices#

  • Use gap instead of margins between items
  • Use flex-wrap: wrap with a flex-basis for simple responsive rows
  • Use margin-left: auto to push items apart
  • Reach for Grid when you need alignment in two dimensions

Remember this#

Flexbox = one direction (row or column).

justify-content = main axis; align-items = cross axis.

gap adds space between items.

Practice exercise#

Your task: Centre a 200×200 box both horizontally and vertically inside a container that is 100vh tall.

Show solution
CSS Illustrative
.container { display: flex; justify-content: center; align-items: center; min-height: 100vh; }
.box { width: 200px; height: 200px; }

Mini quiz#

Flexbox quizMultiple choice Question 1 of 2

In a row flex container, which property centres items vertically?

Score so far: 0/0

Want more? Practise all CSS MCQs · CSS interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.