🇬🇧 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·
🎨 CSS Styling

Flexbox Layouts

Flexbox makes it easy to arrange items in a row or column. Set display: flex on a container, then control direction, alignment, and spacing. justify-content aligns items along the main axis; align-items aligns along the cross axis. gap adds space between items without needing margins.

3 min 10 XP Lesson 6 of 10
Flexbox Layouts
🌐

Appy Says…

For 20 years, centring a div in CSS was infamously hard. Flexbox solved it in one line. Today, Flexbox is the foundation of almost every modern web layout — from navbars to card grids to sidebars.

📖

What is Flexbox?

CSS Flexbox arranges elements in a container along one axis (row or column). The parent declares display: flex and its children automatically become flex items.

  • display: flex — activate flexbox on the container
  • flex-direction: row | column | row-reverse | column-reverse
  • justify-content: flex-start | center | flex-end | space-between | space-around | space-evenly (main axis)
  • align-items: stretch | center | flex-start | flex-end | baseline (cross axis)
  • gap: 16px — space between items
  • flex: 1 on child — grow to fill available space
  • flex-wrap: wrap — wrap items to next line
🎮

Think of it like a Roblox GUI ScreenGui layout

Roblox's ScreenGui lets you pin UI elements with offsets and anchors. Flexbox is like having a smart layout container that automatically positions and spaces its children — you just say 'centre them with gaps' and it's done.

⚙️

How It Works

  • 1. Parent: display: flex; justify-content: space-between; align-items: center;
  • 2. Children become flex items — laid out along the main axis (default: row)
  • 3. justify-content controls spacing along the main axis
  • 4. align-items aligns items on the perpendicular axis
  • 5. flex: 1 on a child = 'take all remaining space'
  • 6. Perfect centre: display: flex; justify-content: center; align-items: center;
🌍

Real-World Examples

  • Navbar: logo left, links right — justify-content: space-between
  • Icon + label button: display: flex; align-items: center; gap: 8px;
  • Sidebar layout: parent flex-row, sidebar fixed width, main flex: 1
  • Responsive card row: display: flex; flex-wrap: wrap; gap: 16px;
💡

Key Facts

  • flex: 1 is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%
  • Flexbox is 1D; CSS Grid is 2D — use Flexbox for components, Grid for page layouts
  • order property changes visual order without changing DOM order
  • Flex items ignore float and vertical-align
⚠️

Watch Out!

justify-content works on the main axis; align-items on the cross axis. If you set flex-direction: column, they swap roles. This catches everyone at least once.

📌

Remember

Perfect center: display: flex; justify-content: center; align-items: center;. Use gap for spacing between items instead of margins.

What You Learned

  • Flexbox: display:flex on parent; justify/align children along main and cross axes
  • gap for spacing, flex:1 to fill space, flex-wrap for responsive rows
  • Unlocks: navbars, card rows, centring, sidebars, any 1D layout challenge

Key Facts

  • flex: 1 is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%
  • Flexbox is 1D; CSS Grid is 2D — use Flexbox for components, Grid for page layouts
  • order property changes visual order without changing DOM order
  • Flex items ignore float and vertical-align

Real-World Examples

• Navbar: logo left, links right — <code>justify-content: space-between</code> • Icon + label button: <code>display: flex; align-items: center; gap: 8px;</code> • Sidebar layout: parent flex-row, sidebar fixed width, main <code>flex: 1</code> • Responsive card row: <code>display: flex; flex-wrap: wrap; gap: 16px;</code>

Remember

Perfect center: display: flex; justify-content: center; align-items: center;. Use gap for spacing between items instead of margins.

Quick Quiz

1 / 2

What does display: flex do?