Lesson 24 of 60 – Flexbox Layout in React Native
40%

Flexbox Layout in React Native

Flexbox is the main layout system used in React Native. It helps you arrange components horizontally or vertically and control their position, alignment, size and available space.

Note: React Native uses Flexbox for layout. Understanding flex, flexDirection, justifyContent and alignItems is essential for creating responsive mobile interfaces.

1. What is Flexbox?

Flexbox is a layout system that allows you to arrange child components inside a parent component.

const styles = StyleSheet.create({

    container: {
        flex: 1
    }

});

It is commonly used with View components.

2. Flexbox Container

The parent component that uses Flexbox properties is called the Flexbox container.

<View style={styles.container}>

    <View />
    <View />
    <View />

</View>

The child components are arranged according to the styles of the parent container.

3. The flex Property

The flex property controls how a component uses available space.

const styles = StyleSheet.create({

    container: {
        flex: 1
    }

});

A screen container commonly uses flex: 1 so it can occupy the available screen space.

4. flexDirection

The flexDirection property determines the main direction in which children are arranged.

const styles = StyleSheet.create({

    container: {
        flexDirection: 'row'
    }

});

The main values are row and column.

5. Row Direction

With flexDirection: 'row', children are arranged horizontally.

const styles = StyleSheet.create({

    container: {
        flexDirection: 'row'
    }

});

For example, three Views can appear from left to right.

6. Column Direction

With flexDirection: 'column', children are arranged vertically.

const styles = StyleSheet.create({

    container: {
        flexDirection: 'column'
    }

});

Column direction is the common default layout direction in React Native.

7. justifyContent

The justifyContent property controls the distribution of children along the main axis.

const styles = StyleSheet.create({

    container: {
        justifyContent: 'center'
    }

});

Its effect depends on the current flexDirection.

8. justifyContent: center

const styles = StyleSheet.create({

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

});

This places the children in the center along the main axis.

9. justifyContent: flex-start

const styles = StyleSheet.create({

    container: {
        justifyContent: 'flex-start'
    }

});

This positions children toward the beginning of the main axis.

10. justifyContent: flex-end

const styles = StyleSheet.create({

    container: {
        justifyContent: 'flex-end'
    }

});

This positions children toward the end of the main axis.

11. justifyContent: space-between

const styles = StyleSheet.create({

    container: {
        flexDirection: 'row',
        justifyContent: 'space-between'
    }

});

The available space is distributed between the children.

12. justifyContent: space-around

const styles = StyleSheet.create({

    container: {
        flexDirection: 'row',
        justifyContent: 'space-around'
    }

});

Space is distributed around the children.

13. justifyContent: space-evenly

const styles = StyleSheet.create({

    container: {
        flexDirection: 'row',
        justifyContent: 'space-evenly'
    }

});

The available space is distributed evenly between and around the children.

14. alignItems

The alignItems property controls the alignment of children along the cross axis.

const styles = StyleSheet.create({

    container: {
        alignItems: 'center'
    }

});

It is commonly used together with justifyContent.

15. alignItems: center

const styles = StyleSheet.create({

    container: {
        alignItems: 'center'
    }

});

This centers children along the cross axis.

16. alignItems: flex-start

const styles = StyleSheet.create({

    container: {
        alignItems: 'flex-start'
    }

});

This places children toward the beginning of the cross axis.

17. alignItems: flex-end

const styles = StyleSheet.create({

    container: {
        alignItems: 'flex-end'
    }

});

This places children toward the end of the cross axis.

18. alignSelf

The alignSelf property allows an individual child to override the parent's alignItems.

const styles = StyleSheet.create({

    container: {
        alignItems: 'center'
    },

    specialBox: {
        alignSelf: 'flex-end'
    }

});

19. flexWrap

The flexWrap property controls whether children can move to another line when there is not enough space.

const styles = StyleSheet.create({

    container: {
        flexDirection: 'row',
        flexWrap: 'wrap'
    }

});

This is useful for grids and collections of cards.

20. gap with Flexbox

The gap property can create consistent space between children.

const styles = StyleSheet.create({

    container: {
        gap: 15
    }

});

This can simplify layouts where children need equal spacing.

21. rowGap and columnGap

const styles = StyleSheet.create({

    container: {
        flexDirection: 'row',
        flexWrap: 'wrap',
        rowGap: 15,
        columnGap: 15
    }

});

These properties allow row and column spacing to be controlled separately.

22. Centering Content

A common React Native pattern is to center content both vertically and horizontally.

const styles = StyleSheet.create({

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

});

This is useful for welcome screens, loading screens and simple forms.

23. Creating a Row of Items

const styles = StyleSheet.create({

    row: {
        flexDirection: 'row',
        alignItems: 'center',
        gap: 10
    }

});

<View style={styles.row}>

    <Text>Home</Text>
    <Text>Profile</Text>
    <Text>Settings</Text>

</View>

The items are placed horizontally with spacing between them.

24. Creating a Simple Card Grid

const styles = StyleSheet.create({

    grid: {
        flexDirection: 'row',
        flexWrap: 'wrap',
        gap: 12
    },

    card: {
        width: 150,
        height: 100,
        padding: 15,
        backgroundColor: 'white'
    }

});

Flexbox wrapping can be used to create grid-like interfaces.

25. Header Layout with Flexbox

const styles = StyleSheet.create({

    header: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',
        padding: 15
    }

});

<View style={styles.header}>

    <Text>My App</Text>

    <Text>Menu</Text>

</View>

This pattern is useful for application headers.

26. Flexbox Comparison

Property Purpose
flex Controls use of available space
flexDirection Controls row or column direction
justifyContent Controls main-axis distribution
alignItems Controls cross-axis alignment
alignSelf Overrides alignment for one child
flexWrap Controls wrapping of children
gap Creates spacing between children

27. Complete Flexbox Example

import React from 'react';

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

export default function App() {

    return (

        <View style={styles.container}>

            <View style={styles.header}>

                <Text style={styles.title}>
                    Student Dashboard
                </Text>

                <Text style={styles.menu}>
                    Menu
                </Text>

            </View>

            <View style={styles.cards}>

                <View style={styles.card}>
                    <Text>Courses</Text>
                </View>

                <View style={styles.card}>
                    <Text>Attendance</Text>
                </View>

                <View style={styles.card}>
                    <Text>Fees</Text>
                </View>

                <View style={styles.card}>
                    <Text>Results</Text>
                </View>

            </View>

            <Pressable style={styles.button}>

                <Text style={styles.buttonText}>
                    View Profile
                </Text>

            </Pressable>

        </View>

    );

}

const styles = StyleSheet.create({

    container: {
        flex: 1,
        padding: 20,
        backgroundColor: '#F8FAFC'
    },

    header: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',
        marginBottom: 25
    },

    title: {
        fontSize: 22,
        fontWeight: 'bold'
    },

    menu: {
        fontSize: 16
    },

    cards: {
        flexDirection: 'row',
        flexWrap: 'wrap',
        gap: 12
    },

    card: {
        width: '48%',
        padding: 20,
        backgroundColor: 'white',
        borderRadius: 10
    },

    button: {
        marginTop: 25,
        padding: 15,
        backgroundColor: '#2563EB',
        borderRadius: 8,
        alignItems: 'center'
    },

    buttonText: {
        color: 'white',
        fontWeight: 'bold'
    }

});

28. Common Flexbox Mistakes

  • Forgetting to use flex: 1 when a screen container needs to fill available space.
  • Confusing the main axis with the cross axis.
  • Using justifyContent when you actually need cross-axis alignment.
  • Using alignItems without understanding the current direction.
  • Forgetting flexWrap when creating multi-item layouts.
  • Using too many fixed widths for responsive layouts.
  • Creating crowded layouts without enough spacing.
  • Not testing layouts on different screen sizes.

29. Best Practices for Flexbox

  • Use Flexbox for responsive mobile layouts.
  • Understand the main axis and cross axis.
  • Use flexDirection intentionally.
  • Use justifyContent for main-axis distribution.
  • Use alignItems for cross-axis alignment.
  • Use flexWrap for layouts that need multiple rows.
  • Use gap when consistent child spacing is appropriate.
  • Combine Flexbox with padding and margin.
  • Keep layouts flexible instead of depending unnecessarily on fixed dimensions.

30. Complete Flexbox Workflow

Create Parent View
       ↓
Set flex: 1 if Needed
       ↓
Choose flexDirection
       ↓
Understand Main Axis
       ↓
Set justifyContent
       ↓
Set alignItems
       ↓
Add flexWrap if Needed
       ↓
Add Gap / Margin / Padding
       ↓
Create Responsive Layout
       ↓
Test on Different Screens

Flexbox is one of the most important concepts in React Native. Once you understand direction, main-axis distribution, cross-axis alignment, wrapping and spacing, you can build professional layouts for mobile applications.

📌 Key Points

  • Flexbox is the main layout system in React Native.
  • flex: 1 is commonly used for screen containers.
  • flexDirection controls row or column direction.
  • justifyContent controls the main axis.
  • alignItems controls the cross axis.
  • alignSelf controls alignment for an individual child.
  • flexWrap allows children to wrap onto multiple lines.
  • gap can create consistent spacing between children.
  • Flexbox works together with margin and padding.
  • Flexbox helps create responsive mobile interfaces.

🧠 Quick Quiz

Question: Which React Native Flexbox property controls the direction in which child components are arranged?