📏
Flexbox

Flexbox — Basics

One-Dimensional Layout
💡 Flexbox ek elastic rubber band jaisa hai jisme items string mein pirode ho — items automatically space share karte hain, align hote hain, aur available space ke hisaab se stretch/shrink hote hain, bina manual pixel-calculations ke.

Flexbox (Flexible Box Layout) ek "one-dimensional" layout system hai — ek row YA ek column mein items ko arrange/align/distribute karne ke liye designed. display: flex parent ko "flex container" banata hai, uske direct children automatically "flex items" ban jaate hain.

Flexbox se pehle, centering (especially vertical) CSS mein notoriously mushkil tha (hacky tricks chahiye hote the) — flexbox ne "align-items: center; justify-content: center;" se ek line mein solve kar diya.

.container {
  display: flex;   /* bas itna kaafi hai flexbox shuru karne ke liye */
}

.container {
  display: flex;
  justify-content: center;   /* horizontal centering */
  align-items: center;         /* vertical centering */
  height: 300px;
}
/* Ab andar ka content perfectly center hoga — kabhi mushkil tha! */
📏
Flexbox ek elastic rubber band jaisa hai jisme items string mein pirode ho — items automatically space share karte hain, align hote hain, aur available space ke hisaab se stretch/shrink hote hain, bina manual pixel-calculations ke.
1 / 6
⚡ झट से Recap
  • display: flex = one-dimensional layout (row ya column)
  • justify-content/align-items = alignment/centering, bahut easy
  • Direct children automatically "flex items" ban jaate hain
इस page में (2 subtopics)

Flexbox mein do axes hain: main axis (jis direction mein flex-direction point karta hai — row mein horizontal), cross axis (perpendicular — row mein vertical). Ye concept samajhna zaroori hai kyunki justify-content aur align-items alag axes ko target karte hain.

.row-container {
  display: flex;
  flex-direction: row;
  /* Main axis = horizontal (left to right)
     Cross axis = vertical (top to bottom) */
}

.column-container {
  display: flex;
  flex-direction: column;
  /* Main axis = vertical (ab ulta!)
     Cross axis = horizontal */
}
💡Tip: flex-direction: column set karne par, justify-content aur align-items ka "meaning" swap ho jaata hai (justify-content ab vertical control karta hai) — ye ek common confusion source hai.

Flexbox almost universally supported hai modern browsers mein (IE11 mein partial/buggy support tha, jo ab largely irrelevant hai) — production use ke liye completely safe hai aajkal.

/* Modern CSS — flexbox worry-free use kar sakte ho: */
.container {
  display: flex;
}
/* Koi vendor prefixes ya fallbacks zaroori nahi 2024+ mein */
💡Tip: Purane tutorials mein -webkit-box, -ms-flexbox jaise vendor prefixes dikh sakte hain — ye ab largely unnecessary hain modern browsers ke liye, historical context ke liye jaanna kaafi hai.