CSS: CSS Flexbox Container

🗓 Last updated: 2026-11-30👤 Written by: Alex✓ Reviewed by: Gray🏷 Category: CSS Tutorial

Flexbox is a powerful one-dimensional layout method that makes it easy to align, distribute, and order elements within a container.

Properties in this section: flex-direction flex-wrap justify-content align-items align-content gap margin

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 Yourself

3. justify-content

Aligns items along the main axis:

▶ Example

▶ Try it Yourself

Common values:


4. align-items

Aligns items along the cross axis:

▶ Example

▶ Try it Yourself

5. flex-wrap

Controls whether items wrap to new lines:

▶ Example

▶ Try it Yourself

6. gap

Sets spacing between flex items:

▶ Example

▶ Try it Yourself

Properties in this section: flex-direction flex-wrap justify-content align-items align-content gap margin

❓ 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

📝 Exercises

Basic

  1. Create a flex container with 5 items. Try all four flex-direction values to observe direction changes.
  2. Demonstrate all 6 justify-content values with screenshots for comparison.

Intermediate

  1. Set flex-wrap: wrap and observe multi-line alignment with different align-content values.
  2. Compare gap property with margin for spacing — appreciate gap's convenience.

Challenge

  1. Create a responsive navbar: desktop has logo left, menu right; mobile has logo left, hamburger right, menu hidden.
  2. Create a card list with justify-content: space-between for even distribution, last row left-aligned.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏