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.
How it works:
body { display: flex; flex-direction: column; }— Stacks children verticallymin-height: 100vh— Body takes full viewport heightflex: 1on.middle— Expands to fill available space between header and footer.middle { display: flex; }— Creates a flex row for sidebar, main, and asidemain { flex: 1; }— Main column expands to fill available width- Sidebars have fixed widths (200px each)
Try the holy grail
Edit the layout below. Try changing the sidebar widths, main color, or adding more content to see how flexbox adapts.
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.
How it works:
display: flex— Activates flexboxjustify-content: center— Centers children along the main axis (horizontal)align-items: center— Centers children along the cross axis (vertical)- Set
height(ormin-height) on the container so there's space to center into
Try perfect centering
Edit the code below. Try changing the card size, colors, or adding more cards to see how flex centers them.
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.
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.
How flex-wrap: wrap works:
flex-wrap: nowrap(default) — All items stay on one line, shrinking if neededflex-wrap: wrap— Items wrap to the next line when they don't fitflex: 1 1 250px— Each item grows, shrinks, with a minimum width of 250px
Quick reference: Flex layout patterns
Pattern 1: Center a box
Pattern 2: Holy grail layout
Pattern 3: Sticky footer
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
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 →