← Back to CSS Course | Chapter 10: Flexbox | Lesson 4 of 5

CSS Flex Items

अगर flex container shelf है, तो flex items individual books हैं — और grow/shrink/basis वे rules हैं कि हर book कितनी जगह claim कर सकती है।
Syntax
css
item {
  flex: grow shrink basis;
  order: integer;
  align-self: value;
}

order

order property HTML को छुए बिना किसी item की visual position बदल देती है — items default रूप से order: 0 पर होते हैं और ascending sort होते हैं, इसलिए एक कम number किसी item को पहले move करता है और एक ज़्यादा number इसे बाद में move करता है, यहाँ तक कि ties भी source order से break होते हैं।

यह सिर्फ paint order को असर करता है, accessibility के लिए tab/reading order को नहीं, इसलिए इसे purely visual rearrangement के लिए कम मात्रा में इस्तेमाल करें।

Note:
  • Accepted values:
  • order: 0 — default; items order से, फिर source order से layout होते हैं
  • order: <integer> — कम values पहले आती हैं, negatives allowed हैं
  • बराबर order वाले items अपना source order बनाए रखते हैं
  • order सिर्फ visual order बदलता है, tab या reading order नहीं
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords

उदाहरण: order

css
<style>
.container {
  display: flex;
  background: #eef;
  padding: 10px;
  gap: 8px;
}
.first {
  order: 2;
  background: coral;
  color: white;
  padding: 10px;
}
.second {
  order: 1;
  background: dodgerblue;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div class="first">Coded first, shown second</div>
  <div class="second">Coded second, shown first</div>
</div>

flex-grow

flex-grow तय करता है कि कोई item container की बची हुई space का कितना हिस्सा अपने siblings के relative claim करता है — किसी एक item पर flex-grow: 2 एक sibling के 1 set होने से दुगनी extra space claim करता है, जबकि default 0 का मतलब है कोई item कभी अपने content size से आगे नहीं बढ़ता।

यह सिर्फ तभी लागू होता है जब distribute करने के लिए बची हुई space हो।

Note:
  • Accepted values:
  • flex-grow: 0 — default, grow नहीं होता
  • flex-grow: 1 — free space का बराबर हिस्सा लेता है
  • flex-grow: <number> — बड़ी numbers proportionally ज़्यादा लेती हैं
  • नोट: negative values invalid हैं
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords

उदाहरण: flex-grow

css
<style>
.container {
  display: flex;
  background: #eef;
  padding: 10px;
  gap: 8px;
}
.a {
  flex-grow: 2;
  background: coral;
  color: white;
  padding: 10px;
}
.b {
  flex-grow: 1;
  background: dodgerblue;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div class="a">Grows 2x</div>
  <div class="b">Grows 1x</div>
</div>

flex-shrink

flex-shrink flex-grow का mirror है: यह control करता है कि कोई item कितना shrink होता है जब container सबको उनके natural size पर fit करने के लिए बहुत छोटा हो।

Default 1 है (सभी items proportionally shrink होते हैं); किसी item पर flex-shrink: 0 set करना इसे उसके flex-basis size पर pin कर देता है और इसके siblings को overflow absorb करने पर मजबूर करता है।

Note:
  • Accepted values:
  • flex-shrink: 1 — default, shrink हो सकता है
  • flex-shrink: 0 — shrink नहीं होता
  • flex-shrink: <number> — बड़ी numbers proportionally ज़्यादा shrink होती हैं
  • नोट: negative values invalid हैं
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords

उदाहरण: flex-shrink

css
<style>
.container {
  display: flex;
  width: 200px;
  background: #eef;
  padding: 10px;
  gap: 8px;
}
.pinned {
  flex-shrink: 0;
  width: 150px;
  background: coral;
  color: white;
  padding: 10px;
}
.shrinks {
  width: 150px;
  background: dodgerblue;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div class="pinned">Pinned size</div>
  <div class="shrinks">Shrinks to fit</div>
</div>

flex-basis और flex Shorthand

flex-basis grow/shrink calculations होने से पहले किसी item का starting size set करता है।

इसे एक ज़्यादा flexible width की तरह सोचें। तीनों values लगभग हमेशा flex shorthand (flex: grow shrink basis) के रूप में एक साथ लिखी जाती हैं, और common shortcut flex: 1 expand होकर flex: 1 1 0% बन जाता है, जिसका मतलब है "zero starting point से freely grow और shrink करो।"

Note:
  • Accepted values:
  • flex-basis: auto — default, width/height इस्तेमाल करता है
  • flex-basis: content — content से sized
  • flex-basis: <length> — जैसे 200px
  • flex-basis: <percentage> — container का
  • flex: 1 — 1 1 0% में expand होता है
  • flex: auto | none — 1 1 auto | 0 0 auto
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords

उदाहरण: flex-basis and the flex shorthand

css
<style>
.container {
  display: flex;
  background: #eef;
  padding: 10px;
  gap: 8px;
}
.item {
  flex: 1;
  background: dodgerblue;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div class="item">flex: 1 grows and shrinks from a 0% basis</div>
  <div class="item">Equal item</div>
</div>

align-self

align-self एक specific item के लिए container की align-items value को override करता है, वही values accept करते हुए (center, flex-start, flex-end, stretch, baseline)। यह वह इकलौता item-level property है जो main-axis sizing के बजाय cross-axis alignment में वापस पहुँचती है, उपयोगी जब एक single item — जैसे एक "Skip" link — को अपने siblings से अलग vertical alignment चाहिए।

Note:
  • Accepted values:
  • auto — default, container की align-items इस्तेमाल करता है
  • flex-start | flex-end | center — cross axis positions
  • stretch — cross axis भरता है
  • baseline — text baselines align करता है
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords

उदाहरण: align-self

css
<style>
.container {
  display: flex;
  align-items: flex-start;
  height: 150px;
  background: #eef;
}
.container div {
  background: dodgerblue;
  color: white;
  padding: 10px;
}
.skip {
  align-self: flex-end;
  background: coral;
}
</style>
<div class="container">
  <div>Normal</div>
  <div class="skip">Skip link, aligned differently</div>
</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. उस element पर flex-grow set करना जिसका parent flex container नहीं है, जिसका कोई असर नहीं होता।
  2. यह उम्मीद करना कि order screen readers और tab order क्या follow करते हैं यह बदल देगा।
  3. shorthand flex: 1 इस्तेमाल करना और उम्मीद करना कि यह अकेले flex-grow: 1 जैसा ही है, जबकि यह flex-basis: 0 भी set करता है।
ब्राउज़र सपोर्ट

Unprefixed Flexbox Chrome 29+, Firefox 28+, Safari 9+ और Edge 12+ में supported है।

🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.