CSS: CSS Flexbox Container
Flexbox is a powerful one-dimensional layout method that makes it easy to align, distribute, and order elements within a container.
1. Flex Container Properties
(1) Quick Reference
| Property | Values | Purpose |
|---|---|---|
display: flex |
— | Creates flex container |
flex-direction |
row, row-reverse, column, column-reverse |
Main axis direction |
flex-wrap |
nowrap, wrap, wrap-reverse |
Whether items wrap |
justify-content |
flex-start, flex-end, center, space-between, space-around, space-evenly |
Main axis alignment |
align-items |
flex-start, flex-end, center, stretch, baseline |
Cross axis alignment |
align-content |
flex-start, flex-end, center, space-between, space-around, stretch |
Multi-line cross axis |
gap |
10px, 10px 20px |
Space between items |
2. flex-direction
Controls the main axis direction:
▶ Example
▶ Try it Yourself3. justify-content
Aligns items along the main axis:
▶ Example
▶ Try it YourselfCommon values:
flex-start: Items at start (default)flex-end: Items at endcenter: Items centeredspace-between: Equal space between itemsspace-around: Equal space around itemsspace-evenly: Equal space between and around
4. align-items
Aligns items along the cross axis:
▶ Example
▶ Try it Yourself5. flex-wrap
Controls whether items wrap to new lines:
▶ Example
▶ Try it Yourself6. gap
Sets spacing between flex items:
▶ Example
▶ Try it Yourself❓ FAQ
Q How do I center a child element horizontally and vertically with flex?
A Three lines on the container:
display: flex; justify-content: center; align-items: center;. justify-content handles the main axis (horizontal by default), align-items the cross axis (vertical by default) — both set to center puts the item dead center. It's the classic flex pattern and far more reliable than the old absolute-positioning plus transform trick.Q What's the difference between flex and grid, and which should I use?
A flex is one-dimensional — it lays items along a single row or column, ideal for navbars and button groups; grid is two-dimensional and controls rows and columns at once, ideal for page skeletons and card walls. Quick rule: content flowing in one direction → flex; strict row/column alignment → grid. They nest well: grid for the page frame, flex inside components.
Q Does justify-content still work horizontally when flex-direction is column?
A No. flex-direction redefines the main axis, and justify-content always works along it: with
row the main axis is horizontal, with column it turns vertical. So in column mode justify-content: center centers vertically, and horizontal centering moves to align-items. Remember: justify-content → main axis, align-items → cross axis.Q My items are squeezed into one row — how do I make them wrap?
A Add
flex-wrap: wrap to the container. The default nowrap shrinks items instead of wrapping; wrap sends anything that doesn't fit to the next line. For responsive card lists, pair flex-wrap: wrap with a fixed width on each item so rows reflow as the screen narrows.Q What's the difference between align-items and align-content?
A
align-items aligns items within each line; align-content distributes space between multiple lines. With a single line, align-content does nothing — the most common point of confusion. It only kicks in after flex-wrap: wrap creates multiple lines.Q How do I choose between space-between, space-around, and space-evenly?
A
space-between pins the first and last items to the edges and splits the rest evenly — perfect for a navbar with logo left, menu right. space-around gives each item equal side margins, so gaps between items end up twice as wide as the outer ones. space-evenly makes every gap identical. Pick evenly for uniform distribution, between for edge-to-edge alignment.Q When should I use flex-direction: row versus column?
A
row (the default) lays items horizontally — toolbars, tag lists, button rows. column stacks them vertically — forms, menus, vertical lists. Switching direction swaps which axis justify-content and align-items control, so decide where the main axis points before writing properties.Q Why is my gap property not working in a flex container?
A First confirm the container is
display: flex — gap only applies to flex/grid containers. Then check browser support: IE and legacy Edge don't support gap in flex layouts, so fall back to margins there. Also note gap: 10px sets row and column gaps to the same 10px, while two values mean row-gap column-gap — easy to mix up.Q How do I build a navbar with the logo on the left and the menu on the right?
A One rule does it:
display: flex; justify-content: space-between; align-items: center; pushes the two groups to the edges and centers them vertically. Wrap the menu items in their own flex container and space them with gap: 24px. No floats, no absolute positioning needed.📖 Summary
- Flexbox container properties control "overall" behavior — how items arrange, wrap, and align
- Remember:
justify-contentcontrols main axis,align-itemscontrols cross axis - If
flex-directionchanges, these two swap their effective directions
📝 Exercises
Basic
- Create a flex container with 5 items. Try all four
flex-directionvalues to observe direction changes. - Demonstrate all 6
justify-contentvalues with screenshots for comparison.
Intermediate
- Set
flex-wrap: wrapand observe multi-line alignment with differentalign-contentvalues. - Compare
gapproperty withmarginfor spacing — appreciate gap's convenience.
Challenge
- Create a responsive navbar: desktop has logo left, menu right; mobile has logo left, hamburger right, menu hidden.
- Create a card list with
justify-content: space-betweenfor even distribution, last row left-aligned.
🧩 Related Examples
Pill Hover NavbarA dark navbar: Flexbox menu with a pill highlight on hover and a ::after underline sliding out from the cent…
Split-Screen Login PageA full-page flex split: gradient brand panel with a slogan on one side, clean form panel on the other, and t…
Three-Column PricingThe SaaS-website staple: three cards in a row with the recommended one highlighted — accent border, 10px lif…