CSS: CSS Flexbox Container

Last updated: 2026-10-13

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 Does justify-content change direction when flex-direction is column?
A Yes. justify-content always works along the main axis. When flex-direction: row, main axis is horizontal; when column, main axis becomes vertical. Remember: justify-content controls main axis, align-items controls cross axis.
Q When should I use flex-wrap?
A When items don't fit in one line. Default (nowrap) compresses items; wrap lets them flow to next line. Essential for responsive layouts where items should stack on smaller screens.
Q What's the difference between align-items and align-content?
A align-items controls single-line item alignment (within each line). align-content controls multi-line space distribution (only works with multiple lines). Single-line: align-content is ignored.

📖 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.

100%

🙏 帮我们做得更好

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

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