What is Flexbox?

Flexbox (Flexible Box Layout) is a modern CSS layout system that makes it easy to create flexible, responsive page layouts. Before Flexbox, developers used floats and positioning hacks. Flexbox is cleaner, more powerful, and much easier to reason about.

Flexbox solves problems like:

  • Centering content — Vertically and horizontally centering elements is now trivial.
  • Equal-height columns — All items stretch to match the tallest one automatically.
  • Flexible spacing — Distribute space evenly between items or at the edges.
  • Responsive order — Reorder items without changing the HTML.
  • Wrapping — Items can wrap to multiple lines automatically on smaller screens.
Key concept:
Flexbox is a one-dimensional layout system. It works on a single axis (row or column). For two-dimensional layouts, use CSS Grid (which we'll cover in the next arc).

Creating a flex container: display: flex

To start using Flexbox, set display: flex on a parent element. This transforms the parent into a flex container, and all direct children become flex items.

flexbox-basic.css Creating a flex container

Once you set display: flex, the magic happens automatically:

  • Items arrange in a row by default (left to right).
  • Items are flexible — they grow or shrink to fill available space.
  • All items have the same height (they stretch vertically).
  • The container takes the full width of its parent.

Changing direction: flex-direction

By default, flex items arrange in a row (left to right). Use flex-direction to change the main axis.

flex-direction.css All four values
  • row (default) — Items arrange horizontally, left to right.
  • row-reverse — Items arrange horizontally, right to left (visual order only, not DOM order).
  • column — Items stack vertically, top to bottom.
  • column-reverse — Items stack vertically, bottom to top.

Try it yourself: Flexbox playground

Adjust the properties below to see how Flexbox responds. Change flex-direction, justify-content, and align-items to see the effect on the colored boxes.

✏️ Flexbox Playground — Edit & Run
Preview

Aligning along the main axis: justify-content

justify-content controls how flex items are distributed along the main axis (left-right for rows, top-bottom for columns).

justify-content.css All six values
  • flex-start (default) — Items aligned to the start of the container.
  • flex-end — Items aligned to the end of the container.
  • center — Items centered on the axis.
  • space-between — Items spread out; first item at start, last at end.
  • space-around — Space distributed around each item (half-space at edges).
  • space-evenly — Space distributed evenly between all items, including edges.

Aligning on the cross axis: align-items

align-items controls how flex items are aligned perpendicular to the main axis (top-bottom for rows, left-right for columns).

align-items.css All five values
  • stretch (default) — Items stretch to fill the container height/width.
  • flex-start — Items aligned to the start of the cross axis.
  • flex-end — Items aligned to the end of the cross axis.
  • center — Items centered on the cross axis (perfect for vertical centering!).
  • baseline — Items aligned by their text baseline (useful for text).

Wrapping items: flex-wrap

By default, flex items try to fit on a single line and shrink to do so. Use flex-wrap to allow items to wrap to multiple lines.

flex-wrap.css Three values
  • nowrap (default) — Items stay on one line, shrinking if necessary.
  • wrap — Items wrap to new lines when they don't fit.
  • wrap-reverse — Items wrap, but the first line appears at the bottom.

Spacing and multi-line alignment

The gap property

gap adds space between flex items. It's much cleaner than adding margins to individual items.

gap.css Setting spacing

The align-content property

align-content works like justify-content, but for multi-line flex containers. It only has an effect when items wrap.

align-content.css Multi-line alignment

Try it yourself: Build a navigation bar

Use Flexbox to create a responsive navigation bar with a logo on the left and nav links on the right. Adjust justify-content and gap to see how it responds.

✏️ Navigation Bar — Edit & Run
Preview
Challenge: Build a responsive card layout

Task: Create a responsive card layout using Flexbox. Requirements:

  • A flex container with flex-wrap: wrap
  • 4 cards inside the container
  • Add gap: 16px for spacing between cards
  • Use justify-content: center to center the cards
  • Each card should have a fixed width (e.g., 200px) and a background color
  • Cards should wrap to the next line on smaller screens

Hint: Start with a container div with display: flex; flex-wrap: wrap; justify-content: center; gap: 16px;. Create 4 card divs inside with a fixed width and background color. Try resizing the browser window to see how the cards wrap!

Key takeaways

  • Flexbox is one-dimensional: It arranges items in a single row or column, not a grid.
  • display: flex turns a container into a flex context and its children into flex items.
  • flex-direction sets the main axis: row, column, or reversed.
  • justify-content aligns items along the main axis; use it to center, space, and distribute items.
  • align-items aligns items along the cross axis; center is perfect for vertical centering.
  • flex-wrap allows items to wrap to multiple lines; gap adds spacing between items.
  • align-content aligns multiple lines (only works when flex-wrap is on).

Test your knowledge

Question 1 of 5
What does display: flex do?
Question 2 of 5
Which property controls horizontal alignment in a row?
Question 3 of 5
What does align-items: center do?
Question 4 of 5
How do you allow flex items to wrap to the next line?
Question 5 of 5
What does gap: 20px do in a flex container?
0
/5
Great! You're mastering Flexbox.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You just mastered Flexbox containers.
Imagine what layouts 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 →