π Introduction
Flexbox (Flexible Box Layout) is a powerful CSS layout module designed to create responsive and easy-to-control UI alignment. The properties that apply to the flex container determine how flex items are arranged, aligned, wrapped, and distributed. Mastering these container-level properties is essential for building modern layouts. β‘π
π What is a Flex Container?
Any element becomes a flex container when you apply:
Create Flex Container
.container { display: flex; }Once enabled, all direct children become flex items, and container properties affect their positioning.
π₯ Flex Container Properties
1. display
Enables Flexbox on an element.
display
display: flex; /* block-level flex */
display: inline-flex; /* inline-level flex */2. flex-direction
Controls the direction of flex items.
- row β default (left β right)
- row-reverse β right β left
- column β top β bottom
- column-reverse β bottom β top
flex-direction
.container {
flex-direction: row;
}3. flex-wrap
Controls whether flex items wrap to the next line.
- nowrap β default
- wrap β allow wrapping
- wrap-reverse β wrap upwards
flex-wrap
flex-wrap: wrap;4. flex-flow (shorthand)
A shorthand for flex-direction + flex-wrap.
flex-flow
flex-flow: row wrap;5. justify-content
Controls alignment of items along the main axis.
- flex-start (default)
- flex-end
- center
- space-between
- space-around
- space-evenly
justify-content
justify-content: center;6. align-items
Aligns items along the cross axis (perpendicular to flex-direction).
- stretch (default)
- flex-start
- flex-end
- center
- baseline
align-items
align-items: center;7. align-content
Controls spacing between multiple wrapped rows/columns of flex items.
- flex-start
- flex-end
- center
- space-between
- space-around
- space-evenly
- stretch (default)
align-content
align-content: space-between;8. gap
Controls spacing between flex items (like grid-gap).
gap
gap: 20px;9. row-gap & column-gap
A more specific version of gap.
row-gap & column-gap
row-gap: 10px;
column-gap: 20px;π Summary Table
| Property | Description |
|---|---|
| display | Enables flexbox |
| flex-direction | Main axis direction |
| flex-wrap | Allow wrapping of items |
| justify-content | Main-axis alignment |
| align-items | Cross-axis alignment |
| align-content | Spacing between wrapped lines |
| gap | Space between items |
π§ Tips & Best Practices
- Use flex-flow for cleaner shorthand
- Main axis depends on flex-direction
- align-content works only when items wrap
- Use gap instead of margins between flex items
- Use justify-content + align-items for centering
π Conclusion
Flexbox container properties form the foundation of powerful, modern layout systems. From direction and alignment to wrapping and spacing, they give you full control over how flex items behave. Master them β and building responsive UI becomes effortless. π