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.
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.
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.
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.
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).
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).
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.
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.
The align-content property
align-content works like justify-content, but for multi-line flex containers. It only has an effect when items wrap.
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.
Task: Create a responsive card layout using Flexbox. Requirements:
- A flex container with
flex-wrap: wrap - 4 cards inside the container
- Add
gap: 16pxfor spacing between cards - Use
justify-content: centerto 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: flexturns a container into a flex context and its children into flex items.flex-directionsets the main axis: row, column, or reversed.justify-contentaligns items along the main axis; use it to center, space, and distribute items.align-itemsaligns items along the cross axis;centeris perfect for vertical centering.flex-wrapallows items to wrap to multiple lines;gapadds spacing between items.align-contentaligns multiple lines (only works whenflex-wrapis on).
Test your knowledge
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 →