React Native Academy · पाठ

Flexbox दिशा, JustifyContent, AlignItems

flexDirection, justifyContent और alignItems का उपयोग करके नियंत्रित करें कि View के भीतर बाल कॉम्पोनेंट कैसे व्यवस्थित हों, केंद्रित हों और उनके बीच स्थान बाँटा जाए।

पाठ 2, कुल 4 में से13 चरण

Flexbox दिशा, JustifyContent, AlignItems, CoddyKit पर React Native Academy का एक निःशुल्क पाठ है। यह 4 में से 2वाँ पाठ है। इस अध्ययन पथ के 3 तक कोई भी पाठ पूरा पढ़ना निःशुल्क है — इसके बाद CoddyKit PRO हर पाठ अनलॉक करता है, साथ ही अंतर्निर्मित कोड संपादक और चौबीसों घंटे एआई शिक्षक के साथ व्यावहारिक अभ्यास भी उपलब्ध कराता है। यह React Native Academy सीखने के मार्ग का हिस्सा है और आपकी प्रगति वेब तथा CoddyKit ऐप पर सिंक होती रहती है। React Native Academy पाठ्यक्रम में कुल 4 पाठ शामिल हैं।

Flexbox डिफ़ॉल्ट Layout System है

React Native प्रत्येक View component के layout algorithm के रूप में Flexbox का उपयोग करता है — CSS की तरह block या inline layout नहीं होता। प्रत्येक View अपने-आप Flex container होता है, यानी इसके children डिफ़ॉल्ट रूप से Flexbox rules के अनुसार position किए जाते हैं। Web CSS Flexbox से मुख्य अंतर यह है कि React Native का default flexDirection 'column' है, जबकि web पर यह 'row' होता है। इसका अर्थ है कि children डिफ़ॉल्ट रूप से लंबवत रूप से एक-दूसरे के नीचे व्यवस्थित होते हैं, जो अधिकांश mobile screens के layout से मेल खाता है।

import { View, StyleSheet } from 'react-native';

export default function DefaultFlex() {
  return (
    // No flexDirection needed — 'column' is the default
    <View style={styles.container}>
      <View style={styles.box1} />
      <View style={styles.box2} />
      <View style={styles.box3} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, backgroundColor: '#f5f5f5', padding: 16 },
  box1: { height: 60, backgroundColor: '#ff6b6b', marginBottom: 8 },
  box2: { height: 60, backgroundColor: '#4f86f7', marginBottom: 8 },
  box3: { height: 60, backgroundColor: '#51cf66' },
});

flexDirection: row बनाम column

flexDirection Flex container का main axis सेट करता है। 'column' (default) में children ऊपर से नीचे तक stack होते हैं। 'row' में children बाएँ से दाएँ रहते हैं। 'column-reverse' और 'row-reverse' values क्रम को उलट देती हैं। flexDirection बदलने पर justifyContent और alignItems जिस axis को प्रभावित करते हैं, वह भी बदल जाता है — justifyContent हमेशा main axis पर और alignItems cross axis पर काम करता है। यह समझने के लिए सबसे महत्वपूर्ण Flexbox property है।

import { View, StyleSheet } from 'react-native';

export default function DirectionDemo() {
  return (
    <View style={{ flex: 1, gap: 16, padding: 16 }}>
      {/* Column: children stack vertically (default) */}
      <View style={{ flexDirection: 'column', height: 120, backgroundColor: '#f0f0f0' }}>
        <View style={styles.box} />
        <View style={styles.box} />
      </View>

      {/* Row: children sit side by side */}
      <View style={{ flexDirection: 'row', height: 60, backgroundColor: '#f0f0f0' }}>
        <View style={styles.box} />
        <View style={styles.box} />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  box: { width: 50, height: 50, backgroundColor: '#4f86f7', margin: 4 },
});

justifyContent: Main Axis पर Alignment

justifyContent यह नियंत्रित करता है कि children को main axis पर कैसे वितरित किया जाए (column के लिए लंबवत, row के लिए क्षैतिज)। सबसे उपयोगी values हैं: 'flex-start' (शुरुआत पर सघन करना, default), 'flex-end' (अंत पर सघन करना), 'center' (समूह को center करना), 'space-between' (पहला और आखिरी किनारों को छूते हैं, बाकी के बीच समान gaps), 'space-around' (प्रत्येक child के चारों ओर समान space), और 'space-evenly' (किनारों सहित समान space)। Navigation bars और action button rows के लिए 'space-between' विशेष रूप से आम है।

import { View, Text, StyleSheet } from 'react-native';

export default function SpaceBetween() {
  return (
    <View style={styles.row}>
      <Text style={styles.tab}>Home</Text>
      <Text style={styles.tab}>Search</Text>
      <Text style={styles.tab}>Profile</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  row: {
    flexDirection: 'row',
    justifyContent: 'space-between', // distribute across full width
    paddingHorizontal: 24,
    paddingVertical: 16,
    backgroundColor: '#fff',
    borderTopWidth: 1,
    borderTopColor: '#eee',
  },
  tab: { fontSize: 14, color: '#666' },
});

alignItems: Cross Axis पर Alignment

alignItems यह नियंत्रित करता है कि children को cross axis पर कैसे position किया जाए (main axis के लंबवत)। Column container में यह children को क्षैतिज रूप से align करता है; row container में लंबवत रूप से। Values: 'flex-start' (Android पर column के लिए default, items शुरुआत से सटे रहते हैं), 'flex-end', 'center', और 'stretch' (items cross axis को भरने के लिए फैलते हैं — iOS पर यह default है और उन column layouts के लिए उपयोगी है, जहाँ आप full-width children चाहते हैं)। 'baseline' children के text baselines को align करता है।

import { View, Text, StyleSheet } from 'react-native';

export default function AlignDemo() {
  return (
    <View style={styles.container}>
      {/* Row: alignItems centers children vertically */}
      <View style={styles.row}>
        <Text style={styles.smallText}>Small</Text>
        <Text style={styles.largeText}>LARGE</Text>
        <Text style={styles.smallText}>Small</Text>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, padding: 20 },
  row: {
    flexDirection: 'row',
    alignItems: 'center', // vertically center all items
    backgroundColor: '#e8f0fe',
    padding: 16,
    borderRadius: 8,
  },
  smallText: { fontSize: 12, color: '#666', marginHorizontal: 8 },
  largeText: { fontSize: 28, fontWeight: 'bold', color: '#111', marginHorizontal: 8 },
});

flex, justify और align से Content को Center करना

एक बहुत सामान्य pattern है content को लंबवत और क्षैतिज, दोनों तरह से center करना — जैसे splash screens, empty states या loading indicators के लिए। Container को flex: 1 दें, ताकि वह उपलब्ध space भर दे, फिर justifyContent: 'center' और alignItems: 'center' लागू करें। चूँकि default flexDirection column है, इससे children screen के बीच में लंबवत और क्षैतिज, दोनों दिशाओं में center हो जाते हैं। यह तीन properties का संयोजन React Native में सबसे अधिक इस्तेमाल किए जाने वाले layout recipes में से एक है।

import { View, Text, StyleSheet } from 'react-native';

export default function CenteredScreen() {
  return (
    <View style={styles.container}>
      <Text style={styles.emoji}>🎯</Text>
      <Text style={styles.title}>Nothing here yet</Text>
      <Text style={styles.subtitle}>Add your first item to get started</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center', // vertical center (column main axis)
    alignItems: 'center',     // horizontal center (column cross axis)
    backgroundColor: '#f8f9fa',
  },
  emoji: { fontSize: 56, marginBottom: 16 },
  title: { fontSize: 22, fontWeight: 'bold', color: '#333' },
  subtitle: { fontSize: 14, color: '#999', marginTop: 8, textAlign: 'center' },
});

alignSelf: Cross-Axis Alignment को Override करना

alignSelf किसी विशेष child के लिए parent की alignItems को override करता है। यह alignItems जैसी ही values स्वीकार करता है: 'auto' (parent की setting का उपयोग करें), 'flex-start', 'flex-end', 'center', और 'stretch'। यह तब उपयोगी है, जब अधिकांश siblings का alignment एक जैसा हो, लेकिन किसी एक child को अलग तरह से रखना हो — उदाहरण के लिए, icons की एक row में एक icon बड़ा हो और उसे बाकी icons के center में होने के बजाय ऊपर align करना हो। alignSelf का सीमित उपयोग करें; parent की alignItems को बदलना और आवश्यकता पड़ने पर wrapper Views का उपयोग करना अधिक maintainable होता है।

import { View, Text, StyleSheet } from 'react-native';

export default function AlignSelfExample() {
  return (
    <View style={styles.row}>
      <View style={styles.boxSmall} />
      {/* This box aligns to flex-end independently */}
      <View style={[styles.boxSmall, { alignSelf: 'flex-end', backgroundColor: 'red' }]} />
      <View style={styles.boxSmall} />
    </View>
  );
}

const styles = StyleSheet.create({
  row: {
    flexDirection: 'row',
    alignItems: 'center', // default for siblings
    height: 100,
    backgroundColor: '#e8f0fe',
    padding: 8,
    gap: 8,
  },
  boxSmall: { width: 50, height: 50, backgroundColor: '#4f86f7', borderRadius: 6 },
});

gap, rowGap और columnGap

प्रत्येक child में margin जोड़ने के बजाय, flex children के बीच समान spacing सेट करने के लिए container पर gap का उपयोग करें। gap row और column दोनों के gaps सेट करता है, जबकि rowGap और columnGap उन्हें स्वतंत्र रूप से नियंत्रित करते हैं। इससे आखिरी child को छोड़कर सभी children पर marginBottom लगाने या container में negative margin जोड़ने जैसी तरकीब की आवश्यकता नहीं रहती। React Native 0.71 में gap support जोड़ा गया था (Expo SDK 48+ में उपलब्ध), इसलिए पुराने projects में इसका उपयोग करने से पहले अपना RN version जाँच लें।

import { View, StyleSheet } from 'react-native';

export default function GapExample() {
  return (
    <View style={styles.container}>
      {[1, 2, 3, 4].map(n => (
        <View key={n} style={styles.card} />
      ))}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    flexWrap: 'wrap',
    gap: 12,           // equal gap between all children
    padding: 16,
    backgroundColor: '#f5f5f5',
  },
  card: {
    width: '47%',      // two columns
    height: 100,
    backgroundColor: '#4f86f7',
    borderRadius: 10,
  },
});

flexWrap: Children को अगली Lines में Wrap करना

डिफ़ॉल्ट रूप से flex children एक ही line में फिट होने के लिए सिकुड़ते हैं और कभी wrap नहीं होते। Container पर flexWrap: 'wrap' सेट करें, ताकि जगह समाप्त होने पर children अगली row (row direction के लिए) या अगले column (column direction के लिए) में चले जाएँ। यह tag clouds, percentage widths वाले grid-जैसे layouts और ऐसे किसी भी UI का आधार है, जहाँ items स्वाभाविक रूप से अगली line पर जाने चाहिए। 'nowrap' default है; 'wrap-reverse' विपरीत दिशा में wrap करता है।

import { View, Text, StyleSheet } from 'react-native';

const tags = ['React Native', 'Mobile', 'iOS', 'Android', 'JavaScript', 'Flexbox', 'Expo'];

export default function TagCloud() {
  return (
    <View style={styles.container}>
      {tags.map(tag => (
        <View key={tag} style={styles.tag}>
          <Text style={styles.tagText}>{tag}</Text>
        </View>
      ))}
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flexDirection: 'row', flexWrap: 'wrap', gap: 8, padding: 16 },
  tag: { backgroundColor: '#e8f0fe', paddingHorizontal: 12, paddingVertical: 6, borderRadius: 16 },
  tagText: { color: '#4f86f7', fontSize: 13, fontWeight: '500' },
});

एक Practical Header Layout

एक सामान्य mobile app pattern बनाने के लिए अपने Flexbox ज्ञान का उपयोग करें: एक navigation header जिसमें बाईं ओर back button, बीच में centered title और दाईं ओर menu icon हो। Header container पर flexDirection: 'row' और alignItems: 'center' का उपयोग करें। Title को flex: 1 दें, ताकि वह बची हुई जगह भरकर fixed-width side icons के बीच अपने-आप center हो जाए। Centering के लिए position: 'absolute' इस्तेमाल करने की तुलना में यह अधिक साफ़ तरीका है और अलग-अलग title lengths को सही ढंग से संभालता है।

import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';

export default function Header({ title, onBack, onMenu }) {
  return (
    <View style={styles.header}>
      <TouchableOpacity style={styles.sideBtn} onPress={onBack}>
        <Text style={styles.icon}>←</Text>
      </TouchableOpacity>

      <Text style={styles.title} numberOfLines={1}>{title}</Text>

      <TouchableOpacity style={styles.sideBtn} onPress={onMenu}>
        <Text style={styles.icon}>⋯</Text>
      </TouchableOpacity>
    </View>
  );
}

const styles = StyleSheet.create({
  header: {
    flexDirection: 'row',
    alignItems: 'center',
    paddingHorizontal: 16,
    paddingVertical: 12,
    backgroundColor: '#fff',
    borderBottomWidth: StyleSheet.hairlineWidth,
    borderBottomColor: '#ddd',
  },
  sideBtn: { width: 40, alignItems: 'center' },
  title: { flex: 1, fontSize: 18, fontWeight: '600', color: '#111', textAlign: 'center' },
  icon: { fontSize: 22, color: '#333' },
});

Flexbox Layouts को Debug करना

Layout development के दौरान Flexbox bugs आम होते हैं। उपयोगी debugging techniques: प्रत्येक View में अस्थायी backgroundColor जोड़ें, ताकि उसकी boundaries दिखाई दें; component tree में computed layout का निरीक्षण करने के लिए React DevTools का उपयोग करें; जाँचें कि ancestor containers में flex: 1 तो missing नहीं है (जिस container में flex: 1 नहीं होता, उसकी height शून्य हो जाती है और उसके children दिखाई नहीं देते); और सुनिश्चित करें कि flexDirection वही value है जो आप चाहते हैं (याद रखें, React Native में default column है, row नहीं)। सभी styles हटाकर उन्हें एक-एक करके फिर से जोड़ना layout bugs को अलग करने का विश्वसनीय तरीका है।

// Debug technique: color-code your Views temporarily
const debugStyles = StyleSheet.create({
  container: { flex: 1, backgroundColor: 'rgba(255,0,0,0.1)' }, // red tint
  inner: { backgroundColor: 'rgba(0,0,255,0.1)' },              // blue tint
  text: { backgroundColor: 'rgba(0,255,0,0.1)' },               // green tint
});

// Then you can see exactly where each View starts and ends
// Remove these debug colors before shipping!

Icon, Text और Action वाली Row

सबसे सामान्य mobile UI patterns में से एक है ऐसी list row जिसमें बाईं ओर icon, बीच में text और दाईं ओर action button हो। इसे container पर flexDirection: 'row', सभी children को लंबवत align करने के लिए alignItems: 'center', बाईं ओर fixed-size icon View, बीच में flex: 1 वाला text area (ताकि वह उपलब्ध जगह भरने के लिए फैल सके), और दाईं ओर fixed-size action button के साथ लागू करें। यह pattern settings screens, contact lists और mobile apps के data-dense table views में दिखाई देता है।

import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';

export default function SettingsRow({ icon, label, subtitle, onPress }) {
  return (
    <TouchableOpacity style={styles.row} onPress={onPress}>
      {/* Left: icon */}
      <View style={styles.iconContainer}>
        <Text style={{ fontSize: 22 }}>{icon}</Text>
      </View>

      {/* Middle: text content — flex: 1 fills space */}
      <View style={{ flex: 1 }}>
        <Text style={styles.label} numberOfLines={1}>{label}</Text>
        {subtitle && <Text style={styles.subtitle} numberOfLines={1}>{subtitle}</Text>}
      </View>

      {/* Right: chevron */}
      <Text style={styles.chevron}>›</Text>
    </TouchableOpacity>
  );
}

const styles = StyleSheet.create({
  row: { flexDirection: 'row', alignItems: 'center', paddingHorizontal: 16, paddingVertical: 12, backgroundColor: '#fff', gap: 12 },
  iconContainer: { width: 36, height: 36, borderRadius: 8, backgroundColor: '#f0f4ff', alignItems: 'center', justifyContent: 'center' },
  label: { fontSize: 16, color: '#222' },
  subtitle: { fontSize: 13, color: '#999', marginTop: 2 },
  chevron: { fontSize: 22, color: '#ccc', fontWeight: '300' },
});

त्वरित जाँच

इस lesson में React Native Mobile Development की concepts के बारे में अपनी समझ जाँचें।

Lesson का पुनरावलोकन

इस पाठ में आपने सीखा: flexDirection मुख्य अक्ष निर्धारित करता है (React Native में डिफ़ॉल्ट रूप से column, row नहीं), justifyContent मुख्य अक्ष पर child elements को वितरित करता है, और alignItems cross axis पर child elements को संरेखित करता है। अब हम flex प्रॉपर्टी और अलग-अलग स्क्रीन आयामों के अनुसार responsive sizing को समझेंगे।

शुरुआत निःशुल्क

एआई शिक्षक के साथ JavaScript सीखें — निःशुल्क

अपने ब्राउज़र में वास्तविक कोड लिखें और चलाएँ, चौबीसों घंटे एआई शिक्षक से तुरंत सहायता पाएँ, और वेब या ऐप पर वहीं से शुरू करें जहाँ आपने छोड़ा था।

पाठ्यक्रम
30
पाठ
120

अक्सर पूछे जाने वाले प्रश्न

क्या “Flexbox दिशा, JustifyContent, AlignItems” पाठ निःशुल्क है?

हाँ — React Native Academy अध्ययन पथ के 3 तक कोई भी पाठ, जिसमें “Flexbox दिशा, JustifyContent, AlignItems” भी शामिल है, यहाँ वेब पर पूरा पढ़ना निःशुल्क है। इसके बाद CoddyKit PRO हर पाठ अनलॉक करता है, साथ ही अंतर्निर्मित कोड संपादक और चौबीसों घंटे एआई शिक्षक के साथ इंटरैक्टिव अभ्यास भी उपलब्ध कराता है। React Native Academy पाठ्यक्रम में कुल 4 पाठ शामिल हैं।

“Flexbox दिशा, JustifyContent, AlignItems” में मैं क्या सीखूँगा?

flexDirection, justifyContent और alignItems का उपयोग करके नियंत्रित करें कि View के भीतर बाल कॉम्पोनेंट कैसे व्यवस्थित हों, केंद्रित हों और उनके बीच स्थान बाँटा जाए। आप ब्राउज़र में सीधे चलाए जाने वाले व्यावहारिक कोड के साथ React Native Academy का अभ्यास करते हैं, और पाठ पूरा करते समय 24/7 एआई ट्यूटर आपके प्रश्नों के उत्तर देता है।

क्या React Native Academy शुरू करने के लिए मुझे किसी अनुभव की आवश्यकता है?

पहले के अनुभव की आवश्यकता नहीं है। CoddyKit पर React Native Academy शुरुआती से लेकर उन्नत शिक्षार्थियों तक सभी के लिए व्यवस्थित किया गया है, इसलिए आप यहीं से या शुरुआत से सीखना शुरू कर सकते हैं और अपनी गति से आगे बढ़ सकते हैं। यह 4 में से 2वाँ पाठ है।

“Flexbox दिशा, JustifyContent, AlignItems” पाठ पूरा करने में कितना समय लगता है?

CoddyKit का अधिकांश पाठ लगभग 5–10 मिनट में पूरा हो जाता है। हर पाठ छोटा और संवादात्मक है, इसलिए आप लगातार प्रगति करते हैं और वेब या ऐप पर वहीं से सीखना जारी रख सकते हैं जहाँ आपने छोड़ा था।

क्या मैं इस React Native Academy पाठ में कोड लिख और चला सकता हूँ?

हाँ। हर React Native Academy पाठ में एक अंतर्निर्मित कोड संपादक शामिल है, जिससे आप सीधे अपने ब्राउज़र में वास्तविक कोड लिख और चला सकते हैं और तुरंत एआई प्रतिक्रिया पा सकते हैं—स्थानीय सेटअप की आवश्यकता नहीं है।

इस पाठ्यक्रम के सभी पाठ

  1. StyleSheet.create और इनलाइन शैलियाँ
  2. Flexbox दिशा, JustifyContent, AlignItems
  3. flex, flexGrow और अनुकूल आकार
  4. अनुकूल कार्ड ग्रिड बनाना
← React Native Academy पर वापस जाएँ