Flexbox Layout

Loading concept...

πŸŽͺ The Magic Box Show: Mastering Flexbox in React Native

Imagine you have a magic box that can arrange your toys perfectlyβ€”no matter how many toys you have or how big they are. That’s Flexbox! It’s your superpower for arranging things on screen.


🎬 Our Story Begins…

Meet Flexi the Box. Flexi is specialβ€”she can stretch, shrink, and organize anything inside her. Whether you give her 2 toys or 20, she knows exactly where to put them.

Today, Flexi will teach you her 8 magic tricks for perfect layouts!


1️⃣ Flex Container Setup

The Magic Box Awakens

Before Flexi can do her magic, you need to wake her up. In React Native, every View is already a flex container by default!

// Flexi is ready!
<View style={{ flex: 1 }}>
  <Text>I'm inside a flex box!</Text>
</View>

What’s happening?

  • flex: 1 tells Flexi: β€œTake all the space you can!”
  • Without flex, Flexi only takes space her children need

Simple Example

<View style={{
  flex: 1,
  backgroundColor: '#E8F5E9'
}}>
  <Text>Box 1</Text>
  <Text>Box 2</Text>
  <Text>Box 3</Text>
</View>

πŸ’‘ Key Insight: In React Native, you don’t need display: flex. Every View is already flexing!


2️⃣ Flex Direction

Which Way Do We March?

Imagine your toys are soldiers. Flex direction decides if they march in a line or a column.

graph TD A[flexDirection] --> B[column] A --> C[row] A --> D[column-reverse] A --> E[row-reverse] B --> B1[↓ Top to Bottom] C --> C1[β†’ Left to Right] D --> D1[↑ Bottom to Top] E --> E1[← Right to Left]

The 4 Directions

Direction Toys March…
column ↓ Down (DEFAULT)
row β†’ Right
column-reverse ↑ Up
row-reverse ← Left

Code Example

// Toys march LEFT to RIGHT
<View style={{
  flexDirection: 'row'
}}>
  <Text>🎈</Text>
  <Text>🎁</Text>
  <Text>🧸</Text>
</View>
// Result: 🎈 🎁 🧸

// Toys march TOP to BOTTOM (default)
<View style={{
  flexDirection: 'column'
}}>
  <Text>🎈</Text>
  <Text>🎁</Text>
  <Text>🧸</Text>
</View>
// Result:
// 🎈
// 🎁
// 🧸

⚠️ React Native Default: Unlike web CSS (which defaults to row), React Native defaults to column!


3️⃣ Justify Content

Spreading Toys Along the Main Road

Think of your toys walking on a road. justifyContent decides how they spread out on that road.

If flexDirection: 'row' β†’ road goes left-right ↔️ If flexDirection: 'column' β†’ road goes top-bottom ↕️

graph LR JC[justifyContent] --> A[flex-start] JC --> B[flex-end] JC --> C[center] JC --> D[space-between] JC --> E[space-around] JC --> F[space-evenly]

Visual Guide

Value What It Does
flex-start 🧸🎁🎈______ All at start
flex-end ______🧸🎁🎈 All at end
center 🧸🎁🎈 All in middle
space-between 🧸___🎁___🎈 Space BETWEEN
space-around 🧸__🎁__🎈 Space AROUND each
space-evenly 🧸__🎁__🎈 Equal space EVERYWHERE

Code Example

<View style={{
  flexDirection: 'row',
  justifyContent: 'space-between',
  padding: 10
}}>
  <Text>🎈</Text>
  <Text>🎁</Text>
  <Text>🧸</Text>
</View>
// 🎈         🎁         🧸

4️⃣ Align Items

Lining Up Toys on the Cross Street

If justifyContent handles the main road, alignItems handles the cross street (perpendicular direction).

  • flexDirection: 'row' β†’ alignItems controls vertical ↕️
  • flexDirection: 'column' β†’ alignItems controls horizontal ↔️

The Options

Value What It Does
flex-start All at the start of cross axis
flex-end All at the end of cross axis
center All centered on cross axis
stretch Stretch to fill cross axis (DEFAULT)
baseline Align by text baseline

Code Example

<View style={{
  flexDirection: 'row',
  justifyContent: 'center',
  alignItems: 'center',
  height: 200,
  backgroundColor: '#E3F2FD'
}}>
  <Text style={{ fontSize: 30 }}>🎈</Text>
  <Text style={{ fontSize: 20 }}>🎁</Text>
  <Text style={{ fontSize: 40 }}>🧸</Text>
</View>
// All perfectly centered!

πŸ’‘ Pro Tip: To center something PERFECTLY in the middle of screen:

{
  flex: 1,
  justifyContent: 'center',
  alignItems: 'center'
}

5️⃣ Align Self

The Rebel Toy

What if ONE toy wants to be different? alignSelf lets a single child break the rules.

<View style={{
  flexDirection: 'row',
  alignItems: 'flex-start',
  height: 100
}}>
  <Text>🎈</Text>
  <Text>🎁</Text>
  <Text style={{
    alignSelf: 'flex-end'
  }}>🧸 I'm different!</Text>
</View>

Result:

🎈 🎁
        🧸 ← This one moved to bottom!

Options (Same as alignItems)

  • auto β†’ Follow parent’s alignItems
  • flex-start β†’ Go to start
  • flex-end β†’ Go to end
  • center β†’ Go to center
  • stretch β†’ Stretch to fill

6️⃣ Flex Wrap

When Toys Overflow the Shelf

What happens when you have too many toys for one row? They can wrap to the next line!

graph TD FW[flexWrap] --> A[nowrap] FW --> B[wrap] FW --> C[wrap-reverse] A --> A1[Squeeze everything in one line] B --> B1[Wrap to next line] C --> C1[Wrap upward/backward]

Code Example

<View style={{
  flexDirection: 'row',
  flexWrap: 'wrap',
  width: 200
}}>
  <Text style={{ width: 80 }}>🎈</Text>
  <Text style={{ width: 80 }}>🎁</Text>
  <Text style={{ width: 80 }}>🧸</Text>
  <Text style={{ width: 80 }}>πŸŽͺ</Text>
</View>

Without wrap: 🎈🎁🧸πŸŽͺ (squished or overflow)

With wrap:

🎈 🎁
🧸 πŸŽͺ

7️⃣ Flex Item Properties

Giving Toys Superpowers

Each toy (child) can have special powers too!

The Three Musketeers

Property What It Does
flex How much space to take
flexGrow How much to grow
flexShrink How much to shrink
flexBasis Starting size

flex: The Shorthand

<View style={{ flexDirection: 'row' }}>
  <View style={{ flex: 1, backgroundColor: 'red' }}>
    <Text>1 part</Text>
  </View>
  <View style={{ flex: 2, backgroundColor: 'blue' }}>
    <Text>2 parts</Text>
  </View>
  <View style={{ flex: 1, backgroundColor: 'green' }}>
    <Text>1 part</Text>
  </View>
</View>
// Red: 25% | Blue: 50% | Green: 25%

flexGrow & flexShrink

// This box WILL grow to fill space
<View style={{ flexGrow: 1 }}>
  <Text>I grow!</Text>
</View>

// This box will NOT shrink
<View style={{ flexShrink: 0, width: 100 }}>
  <Text>I stay 100!</Text>
</View>

flexBasis

// Start at 100, then flex
<View style={{
  flexBasis: 100,
  flexGrow: 1
}}>
  <Text>Start at 100, grow from there</Text>
</View>

8️⃣ Gap Properties

Giving Toys Breathing Room

Instead of adding margin to each toy, use gap for consistent spacing!

graph TD G[Gap Properties] --> A[gap] G --> B[rowGap] G --> C[columnGap] A --> A1[Space in both directions] B --> B1[Space between rows] C --> C1[Space between columns]

Code Example

<View style={{
  flexDirection: 'row',
  flexWrap: 'wrap',
  gap: 10
}}>
  <Text>🎈</Text>
  <Text>🎁</Text>
  <Text>🧸</Text>
  <Text>πŸŽͺ</Text>
</View>
// 10px between EVERY toy, automatically!

Specific Gaps

<View style={{
  flexDirection: 'row',
  flexWrap: 'wrap',
  rowGap: 20,    // Space between rows
  columnGap: 10  // Space between columns
}}>
  {/* Children */}
</View>

πŸ’‘ Why gap is better than margin:

  • No double margins
  • No edge margin issues
  • One place to control spacing

🎯 Putting It All Together

Here’s a real-world card layout:

<View style={{
  flex: 1,
  flexDirection: 'row',
  flexWrap: 'wrap',
  justifyContent: 'space-between',
  alignItems: 'flex-start',
  gap: 16,
  padding: 16
}}>
  <View style={{
    flexBasis: '47%',
    backgroundColor: '#FFF',
    padding: 16,
    borderRadius: 8
  }}>
    <Text>Card 1</Text>
  </View>
  <View style={{
    flexBasis: '47%',
    backgroundColor: '#FFF',
    padding: 16,
    borderRadius: 8
  }}>
    <Text>Card 2</Text>
  </View>
</View>

🧠 Quick Memory Tricks

Property Remember
flexDirection Which way do soldiers march?
justifyContent Spread on MAIN road
alignItems Align on CROSS street
alignSelf The rebel child
flexWrap Overflow behavior
flex How much space to grab
gap Breathing room between items

πŸŽ‰ Congratulations!

You now know Flexi’s 8 magic tricks! With these powers, you can create any layout in React Native.

Remember: Flexbox is your friend. When layouts get tricky, just ask yourself:

  1. Which direction? β†’ flexDirection
  2. How spread on main axis? β†’ justifyContent
  3. How align on cross axis? β†’ alignItems
  4. Any rebel children? β†’ alignSelf
  5. Should items wrap? β†’ flexWrap
  6. How should items size? β†’ flex, flexGrow, flexShrink
  7. Need consistent spacing? β†’ gap

Now go build something amazing! πŸš€

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.