Home/Lessons/Flexbox Basics
CSS StylingIntermediate16 min read

Flexbox Basics

Align elements side by side or stack them vertically using CSS Flexbox.

0 / 31 lessons completed0%
YouTube video lessonYouTube

Lesson Content

Flexbox is a CSS layout system designed for arranging items in a single direction — either a row or a column. It solves the classic problem of aligning elements side by side or centring things vertically.

Enabling Flexbox: Add `display: flex` to a container element. Its direct children become flex items.

  • flex-directionrow (default, horizontal) or column (vertical)
  • justify-content — aligns items along the main axis (horizontal in row mode)
  • align-items — aligns items along the cross axis (vertical in row mode)
  • gap — adds space between items
  • flex-wrap — allows items to wrap onto multiple lines
  • flex: 1 — makes an item grow to fill available space
  • flex-shrink: 0 — prevents an item from shrinking

Flexbox is perfect for navigation bars, card rows, centring content, and any one-dimensional layout. For two-dimensional layouts (rows AND columns), use CSS Grid.

code example
/* Navigation bar with flexbox */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 32px;
  background: white;
}

/* Card row */
.card-row {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
}

.card-row .card {
  flex: 1;
  min-width: 280px;
}

/* Perfect centring */
.hero {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  text-align: center;
}

Finished this lesson?

Mark it complete to track your progress through the course.

Learning Objectives

  • 1Enable flexbox with display: flex
  • 2Control direction with flex-direction
  • 3Align items on the main and cross axis
  • 4Build a responsive navigation bar with flexbox
Your Progress0%

0 of 31 lessons done

All Lessons