01. Flex Container Properties (direction, justify, align, wrap, gap)
🟡 Intermediate
1. Definition
Declaring display: flex converts an element into a 1-dimensional Flex Container. Container properties control layout alignment along the Main Axis (via justify-content) and Cross Axis (via align-items).
2. Why It Is Used
Flexbox simplifies 1D layout alignment (toolbars, card rows, centered hero banners) without requiring float clearfixes or complex positioning math.
3. How It Works
When flex-direction: row is set, the Main Axis runs horizontally and Cross Axis vertically. Setting flex-direction: column swaps these axes.
4. Syntax
.flex-row {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
gap: 16px;
}
5. Flex Container Alignment Reference Table
| Property | Controls Alignment Along | Available Key Values |
|---|---|---|
justify-content | Main Axis | flex-start, center, flex-end, space-between, space-around, space-evenly |
align-items | Cross Axis (Single Row) | stretch, center, flex-start, flex-end, baseline |
flex-wrap | Multi-line Wrapping | nowrap, wrap, wrap-reverse |
gap | Spacing Between Items | Px, rem, em, % gutter units |
6. Beginner Example
.nav-bar {
display: flex;
justify-content: space-between;
align-items: center;
}
7. Intermediate Example
.centered-hero {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 80vh;
gap: 20px;
}
8. Advanced Responsive Flex Wrap Grid Example
.card-list {
display: flex;
flex-wrap: wrap;
gap: 24px;
justify-content: center;
}
9. Line-by-Line Code Explanation
justify-content: space-between;: Pushes first child to left edge and last child to right edge, distributing remaining free space evenly between items.gap: 24px;: Sets uniform gutter spacing between flex children without needing margin hacks.
10. Expected Terminal / Browser Output
A perfectly centered navigation bar or card row with equal horizontal gutters.
11. Common Mistakes & Pitfalls
- Confusing Main Axis and Cross Axis: Using
align-itemsexpecting horizontal centering whenflex-direction: rowis active.
12. Important Rules & Limitations
- Flexbox is designed for 1D layouts (single row OR column). Use CSS Grid for 2D layouts requiring both rows and columns.
13. Multilingual Explanation: English
Flexbox manages 1D layout alignment. justify-content aligns items along the main axis, while align-items aligns items along the cross axis.
14. Multilingual Explanation: Hindi (Roman Script)
Flexbox 1D layouts ke liye hai. Main axis par align karne ke liye justify-content aur cross axis par align-items ka use karein.
15. Multilingual Explanation: Marathi (Roman Script)
Flexbox 1D layout sathi vaparla jaato. Main axis alignment justify-content dware ani cross axis align-items dware tharte.
16. Practice Questions
- How does changing
flex-directionfromrowtocolumnaffectjustify-content?
17. Practical Coding Challenge
Create a .toolbar flex container that places a logo on the left and a button group on the right using justify-content: space-between.