πŸ“¦ CSS Flexbox Tutorial: Flexbox Container Properties

πŸ“Œ 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

PropertyDescription
displayEnables flexbox
flex-directionMain axis direction
flex-wrapAllow wrapping of items
justify-contentMain-axis alignment
align-itemsCross-axis alignment
align-contentSpacing between wrapped lines
gapSpace 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
>>β€œFlexbox container properties make layouts responsive, clean, and incredibly easy.” πŸ’‘

πŸŽ‰ 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. πŸš€