Why flexbox is perfect for layouts

Before flexbox, building professional page layouts required floats, absolute positioning, or hacky margin tricks. Flexbox changed everything by providing a simple, predictable way to align and distribute space.

With display: flex, you can:

  • Build the holy grail layout (header, sidebar, main, aside, footer) with just 3 properties
  • Center content both horizontally and vertically — the solution to one of CSS's trickiest problems
  • Make equal-height columns automatically
  • Stick a footer to the bottom even when content is short
  • Create responsive layouts that reflow with flex-wrap

Let's build all five patterns.

The holy grail layout with flexbox

The holy grail is a classic three-column page with a header on top and footer on bottom. Using floats or grids, it's complex. With flexbox, it takes one container and one property.

holy-grail.html Header + 3-column middle + footer, all with flexbox

How it works:

  • body { display: flex; flex-direction: column; } — Stacks children vertically
  • min-height: 100vh — Body takes full viewport height
  • flex: 1 on .middle — Expands to fill available space between header and footer
  • .middle { display: flex; } — Creates a flex row for sidebar, main, and aside
  • main { flex: 1; } — Main column expands to fill available width
  • Sidebars have fixed widths (200px each)
Key insight:
The body is a flex column, the middle div is a flex row. Two flex containers, two directions. This is the holy grail pattern.

Try the holy grail

Edit the layout below. Try changing the sidebar widths, main color, or adding more content to see how flexbox adapts.

✏️ Try It Yourself — Holy Grail Layout
Preview

Perfect centering with flexbox

Before flexbox, centering an element both horizontally and vertically required tricks like absolute positioning + transforms. Now it's three lines of CSS.

centering.css The perfect centering trick

How it works:

  • display: flex — Activates flexbox
  • justify-content: center — Centers children along the main axis (horizontal)
  • align-items: center — Centers children along the cross axis (vertical)
  • Set height (or min-height) on the container so there's space to center into
Pro tip:
This works for centering a single element or multiple elements. If you have multiple children, they all center as a group.

Try perfect centering

Edit the code below. Try changing the card size, colors, or adding more cards to see how flex centers them.

✏️ Try It Yourself — Perfect Centering
Preview

Equal-height columns

One of flexbox's superpowers: by default, all flex items stretch to match the height of the tallest sibling. No extra CSS needed.

equal-height.css Flex items auto-stretch to equal height

Why this matters: In the past, you'd need JavaScript or a grid-based layout to make columns equal height. With flexbox, it's automatic. The default value of align-items is stretch, which stretches all flex items to fill the container's cross-axis height.

Responsive layouts with flex-wrap

Add flex-wrap: wrap to make flex items wrap to the next line when there's no space. Perfect for responsive card layouts.

responsive-cards.css Cards wrap on smaller screens

How flex-wrap: wrap works:

  • flex-wrap: nowrap (default) — All items stay on one line, shrinking if needed
  • flex-wrap: wrap — Items wrap to the next line when they don't fit
  • flex: 1 1 250px — Each item grows, shrinks, with a minimum width of 250px

Quick reference: Flex layout patterns

Pattern 1: Center a box

centering-pattern.css Center horizontally and vertically

Pattern 2: Holy grail layout

holy-grail-pattern.css Header, 3-column, footer

Pattern 3: Sticky footer

sticky-footer-pattern.css Footer sticks to bottom
Challenge: Build a Blog Page

Task: Create a complete blog page layout with:

  • A header with navigation (use flex to center or space items)
  • A 2-column layout: main content on the left (flex: 1), sidebar 300px on the right
  • Main content with a few blog post cards
  • A footer that sticks to the bottom even if content is short

Bonus: Make the post cards wrap on mobile using flex-wrap: wrap and media queries.

Hint: Start with a body flex column (header, .middle, footer). The .middle div is also flex with the main content and sidebar. Use flex: 1 on main and width: 300px on sidebar. For sticky footer, set flex: 1 on the main content area.

Key flexbox concepts for layouts

  • flex-direction: column — Stack items vertically (for header/footer/main layouts)
  • flex-direction: row (default) — Arrange items horizontally (sidebars + main)
  • flex: 1 — Item grows to fill available space
  • justify-content: center — Center along the main axis (horizontal in row)
  • align-items: center — Center along the cross axis (vertical in row)
  • flex-wrap: wrap — Items wrap to next line when they don't fit
  • gap — Space between items (cleaner than margin tricks)
  • min-height: 100vh — Container takes full viewport height

Test your knowledge

Question 1 of 5
How do you center an element both horizontally and vertically with flexbox?
Question 2 of 5
Do flex items automatically get equal heights by default?
Question 3 of 5
How do you make a footer stick to the bottom even when content is short?
Question 4 of 5
What makes the holy grail layout work with flexbox?
Question 5 of 5
How do cards wrap to the next line when the screen is small?
0
/5
Great work! You understand flexbox layouts.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You just built layouts with flexbox.
Imagine what you'll build next.

Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses →