🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
🌐 Web (HTML & CSS)

CSS: layout and boxes

Every element is a box. You can set width, height, margin (space outside), and padding (space inside). display: flex; on a parent arranges children in a row or column. Use class and id to target specific elements.

3 min 10 XP Lesson 18 of 26
CSS: layout and boxes
🌐

Appy Says…

Getting elements to sit side by side, centre a div, make a navigation bar — these are the classic CSS puzzles. Flexbox solves all of them. It's the single most useful CSS feature for modern layout.

📖

What is CSS Flexbox?

Flexbox is a CSS layout mode that arranges children of a container along one axis (row or column). The parent is the flex container; children become flex items that can grow, shrink, and align automatically.

  • display: flex — turn on flexbox for a container
  • flex-direction: row | column — axis direction (row = horizontal, default)
  • justify-content — alignment along the main axis: flex-start | center | space-between | space-around
  • align-items — alignment on the cross axis: stretch | center | flex-start | flex-end
  • gap: 16px — space between flex items (modern, cleaner than margins)
  • flex: 1 on a child — take up all available remaining space
  • flex-wrap: wrap — allow items to wrap to the next line
🎮

Think of it like arranging items on a shelf

Flexbox is like a magical shelf where you tell it how to arrange items — left-aligned, centred, spaced evenly — and it handles all the maths. You say 'space these three items evenly' and they just are, regardless of screen width.

⚙️

How It Works

  • 1. Add display: flex to the parent container
  • 2. Children automatically lay out in a row by default
  • 3. justify-content: space-between pushes first item to left, last to right, rest evenly spaced
  • 4. align-items: center vertically centres all children
  • 5. flex: 1 on a child makes it expand to fill remaining space
  • 6. Perfect centering: display: flex; justify-content: center; align-items: center;
🌍

Real-World Examples

  • Navigation bar: display: flex; justify-content: space-between; align-items: center;
  • Card grid: display: flex; flex-wrap: wrap; gap: 16px;
  • Button with icon and label side by side: display: flex; align-items: center; gap: 8px;
  • Full-height sidebar layout: parent display: flex, sidebar fixed width, main flex: 1
💡

Key Facts

  • Flexbox is supported in 99%+ of browsers — safe to use everywhere
  • Flexbox is one-dimensional (one axis at a time); CSS Grid is two-dimensional
  • The visual order of flex items can differ from DOM order via order property
  • Flexbox finally solved the 'how to vertically centre a div' problem that plagued CSS for 20 years
⚠️

Watch Out!

justify-content works on the main axis (direction of flex-direction). align-items works on the cross axis. If you change flex-direction: column, they swap roles. This confuses many beginners.

📌

Remember

justify-content = main axis alignment. align-items = cross axis alignment. display: flex; justify-content: center; align-items: center; centres anything.

What You Learned

  • Flexbox: display: flex on parent, then justify/align children along axes
  • gap for spacing, flex: 1 to fill space, flex-wrap for responsive rows
  • Unlocks: navbars, card layouts, centring elements, sidebar layouts

Key Facts

  • Flexbox is supported in 99%+ of browsers — safe to use everywhere
  • Flexbox is one-dimensional (one axis at a time); CSS Grid is two-dimensional
  • The visual order of flex items can differ from DOM order via order property
  • Flexbox finally solved the 'how to vertically centre a div' problem that plagued CSS for 20 years

Real-World Examples

• Navigation bar: <code>display: flex; justify-content: space-between; align-items: center;</code> • Card grid: <code>display: flex; flex-wrap: wrap; gap: 16px;</code> • Button with icon and label side by side: <code>display: flex; align-items: center; gap: 8px;</code> • Full-height sidebar layout: parent <code>display: flex</code>, sidebar fixed width, main <code>flex: 1</code>

Remember

justify-content = main axis alignment. align-items = cross axis alignment. display: flex; justify-content: center; align-items: center; centres anything.

Quick Quiz

1 / 2

What does padding do?