Flexbox Properties
Flexbox Properties
flex-direction — row or column
.container {
display: flex;
flex-direction: row; /* DEFAULT - items left to right → */
}
.container {
flex-direction: column; /* items top to bottom ↓ (a vertical stack) */
}
row (default) flows items horizontally; column flows them vertically. Here's the key insight: when you switch to column, the axes SWAP — now the main axis is vertical, so justify-content controls vertical positioning and align-items controls horizontal. This is why understanding "main vs cross axis" matters: the properties follow the direction. There's also row-reverse and column-reverse to flip the order. flex-direction is what lets flexbox build both horizontal navs AND vertical sidebars with the same tool.flex-wrap and gap — spacing and responsiveness
.container {
display: flex;
flex-wrap: wrap; /* items wrap to the next line if they don't fit */
gap: 20px; /* even space BETWEEN all items */
}
flex-wrap: wrap lets them flow onto multiple lines instead — essential for a row of cards that should wrap on smaller screens rather than cramming. gap is the modern, clean way to add equal spacing between items — no more fiddly margins on each item. Just gap: 20px and every item is evenly spaced. Together, flex-wrap: wrap + gap create responsive card grids with almost no effort.flex-grow, flex-shrink, flex-basis — how items size
.item {
flex-grow: 1; /* how much to GROW to fill extra space (0 = don't) */
flex-shrink: 1; /* how much to SHRINK if space is tight (0 = don't) */
flex-basis: 200px; /* the starting/ideal size before grow/shrink */
}
flex-grow: 1 on all items makes them share space equally; give one item flex-grow: 2 and it takes twice the share. flex-shrink controls shrinking when space is tight. flex-basis sets the ideal starting size. These enable flexible layouts where a sidebar stays fixed while the main content grows to fill the rest — genuinely powerful, but you'll usually use the shorthand below.
The flex Shorthand — the one you'll actually use
flex: 1; /* grow:1, shrink:1, basis:0 - "take equal share" */
flex: 0 0 200px; /* grow:0 shrink:0 basis:200px - "fixed 200px" */
/* Common layout: sidebar fixed, content fills the rest */
.sidebar { flex: 0 0 250px; } /* always 250px */
.content { flex: 1; } /* takes all remaining space */
flex shorthand. The two you'll use constantly: flex: 1 means "grow to take an equal share of available space" (perfect for making items fill a row equally), and flex: 0 0 200px means "stay exactly 200px, don't grow or shrink" (for fixed-width sidebars). The sidebar-plus-content pattern above — flex: 0 0 250px for a fixed sidebar and flex: 1 for flexible content — is one of the most useful layouts in all of web design. Memorise flex: 1; you'll type it endlessly.align-self — override for one item
.container { display: flex; align-items: flex-start; }
.special {
align-self: center; /* THIS one item centers, ignoring the container */
}
While align-items on the container aligns ALL items on the cross axis, align-self lets a SINGLE item override that and align itself differently. So if all items sit at the top but you want one centered, give just that item align-self: center. It's the exception handler for flexbox alignment — most items follow the container's rule, but one can break away. A small but handy property for those "everything aligned except this one" moments.
Exam Corner
Q: What does flex-wrap: wrap do? Lets items flow onto multiple lines instead of squeezing onto one.
Q: What is gap used for? Adding even spacing between flex items.
Q: What does flex: 1 mean? The item grows to take an equal share of available space.
Q: How do you make a fixed 250px sidebar next to flexible content? Sidebar: flex: 0 0 250px; content: flex: 1.
flex-direction — row या column
.container {
display: flex;
flex-direction: row; /* DEFAULT - items left to right → */
}
.container {
flex-direction: column; /* items top to bottom ↓ (vertical stack) */
}
row (default) items horizontally बहाता है; column उन्हें vertically. मुख्य insight: जब आप column पर switch करते हैं, axes SWAP होती हैं — अब main axis vertical है, तो justify-content vertical positioning control करता है और align-items horizontal. इसीलिए "main vs cross axis" समझना मायने रखता है: properties दिशा follow करती हैं. order flip करने को row-reverse और column-reverse भी हैं. flex-direction ही flexbox को एक tool से horizontal navs AUR vertical sidebars दोनों बनाने देता है.flex-wrap और gap — spacing और responsiveness
.container {
display: flex;
flex-wrap: wrap; /* fit na ho to items agli line par wrap */
gap: 20px; /* saare items ke BEECH samaan space */
}
flex-wrap: wrap उन्हें कई lines पर बहने देता है — cards की row के लिए ज़रूरी जो छोटी screens पर cramming के बजाय wrap हों. gap items के बीच समान spacing जोड़ने का modern, साफ तरीका है — हर item पर झंझट वाले margins नहीं. बस gap: 20px और हर item समान रूप से spaced. साथ में, flex-wrap: wrap + gap लगभग बिना मेहनत responsive card grids बनाते हैं.flex-grow, flex-shrink, flex-basis — items कैसे size होते
.item {
flex-grow: 1; /* extra space bharne ko kitna BADHE (0 = nahi) */
flex-shrink: 1; /* space tang ho to kitna SIKUDE (0 = nahi) */
flex-basis: 200px; /* grow/shrink se pehle starting/ideal size */
}
flex-grow: 1 उन्हें space समान रूप से share कराता है; एक item को flex-grow: 2 दीजिए और वह दोगुना हिस्सा लेता है. flex-shrink space तंग होने पर सिकुड़ना control करता है. flex-basis ideal starting size set करता है. ये flexible layouts enable करते हैं जहां sidebar fixed रहे जबकि main content बाकी भरने को बढ़े — सच में ताकतवर, पर आप आमतौर पर नीचे वाला shorthand use करेंगे.
flex Shorthand — जो असल में use करेंगे
flex: 1; /* grow:1, shrink:1, basis:0 - "samaan hissa lo" */
flex: 0 0 200px; /* grow:0 shrink:0 basis:200px - "fixed 200px" */
/* Common layout: sidebar fixed, content baaki bhare */
.sidebar { flex: 0 0 250px; } /* hamesha 250px */
.content { flex: 1; } /* saari bachi space le */
flex shorthand use करते हैं. दो जो लगातार use करेंगे: flex: 1 मतलब "उपलब्ध space का समान हिस्सा लेने को बढ़ो" (items को row में समान रूप से भरने के लिए perfect), और flex: 0 0 200px मतलब "ठीक 200px रहो, न बढ़ो न सिकुड़ो" (fixed-width sidebars के लिए). ऊपर वाला sidebar-plus-content pattern — fixed sidebar के लिए flex: 0 0 250px और flexible content के लिए flex: 1 — पूरे web design के सबसे उपयोगी layouts में से एक है. flex: 1 याद कीजिए; इसे अंतहीन type करेंगे.align-self — एक item के लिए override
.container { display: flex; align-items: flex-start; }
.special {
align-self: center; /* YEH ek item center, container ignore karke */
}
जबकि container पर align-items SAARE items को cross axis पर align करता है, align-self एक AKELE item को उसे override करके अलग align करने देता है. तो अगर सारे items ऊपर बैठें पर आप एक centered चाहें, बस उस item को align-self: center दीजिए. यह flexbox alignment का exception handler है — ज़्यादातर items container का rule follow करते हैं, पर एक अलग हो सकता है. "इस एक को छोड़कर सब aligned" वाले पलों के लिए छोटी पर काम की property.
Exam Corner
Q: flex-wrap: wrap क्या करता है? Items को एक line पर squeeze के बजाय कई lines पर बहने देता है.
Q: gap किसलिए use होता है? Flex items के बीच समान spacing जोड़ने को.
Q: flex: 1 का क्या मतलब? Item उपलब्ध space का समान हिस्सा लेने को बढ़ता है.
Q: Flexible content के बगल fixed 250px sidebar कैसे बनाते हैं? Sidebar: flex: 0 0 250px; content: flex: 1.
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.console.log देखने के लिए F12 दबाइए.