Lesson 23 of 60 – Spacing in React Native
38%

Spacing in React Native

Spacing is an important part of designing React Native applications. Proper spacing makes mobile interfaces easier to read, understand and use. React Native mainly uses properties such as padding, margin, gap, rowGap and columnGap to control spacing between components.

Note: React Native uses numbers for most spacing values. For example, padding: 20 means a spacing value of 20, rather than writing 20px as you commonly would in web CSS.

1. What is Spacing?

Spacing means the empty area between or around UI elements. It helps separate different parts of an application.

const styles = StyleSheet.create({

    box: {
        padding: 20,
        margin: 10
    }

});

Good spacing creates a cleaner and more comfortable user interface.

2. Padding vs Margin

Padding creates space inside a component, while margin creates space outside a component.

const styles = StyleSheet.create({

    box: {
        padding: 20,
        margin: 10
    }

});

Understanding this difference is essential when designing layouts.

3. Basic Padding

The padding property adds equal space inside all four sides of a component.

const styles = StyleSheet.create({

    card: {
        padding: 20
    }

});

All four sides receive the same padding value.

4. paddingTop

The paddingTop property controls the space inside the top edge of a component.

const styles = StyleSheet.create({

    box: {
        paddingTop: 30
    }

});

It is useful when content needs additional space above it.

5. paddingBottom

The paddingBottom property controls the space below the content inside a component.

const styles = StyleSheet.create({

    box: {
        paddingBottom: 20
    }

});

6. paddingLeft and paddingRight

These properties control the internal space on the left and right sides.

const styles = StyleSheet.create({

    box: {
        paddingLeft: 15,
        paddingRight: 15
    }

});

They are useful for controlling horizontal spacing.

7. Horizontal and Vertical Padding

You can use paddingVertical and paddingHorizontal for easier two-direction spacing.

const styles = StyleSheet.create({

    button: {
        paddingVertical: 12,
        paddingHorizontal: 20
    }

});

This is commonly useful for buttons, cards and form elements.

8. Basic Margin

The margin property creates space outside a component.

const styles = StyleSheet.create({

    box: {
        margin: 20
    }

});

The same margin value is applied around all four sides.

9. marginTop

The marginTop property adds space above a component.

const styles = StyleSheet.create({

    title: {
        marginTop: 20
    }

});

It is commonly used to separate one section from another.

10. marginBottom

The marginBottom property adds space below a component.

const styles = StyleSheet.create({

    title: {
        marginBottom: 15
    }

});

It is frequently used to create space between headings and other components.

11. marginLeft and marginRight

const styles = StyleSheet.create({

    box: {
        marginLeft: 10,
        marginRight: 10
    }

});

These properties control the external horizontal spacing of a component.

12. Horizontal and Vertical Margin

React Native provides marginVertical and marginHorizontal for convenient spacing.

const styles = StyleSheet.create({

    card: {
        marginVertical: 10,
        marginHorizontal: 15
    }

});

13. Padding in Cards

Cards commonly use padding to keep their content away from the edges.

const styles = StyleSheet.create({

    card: {
        backgroundColor: 'white',
        padding: 20,
        borderRadius: 12
    }

});

Without enough padding, card content can look crowded.

14. Margin Between Cards

const styles = StyleSheet.create({

    card: {
        padding: 20,
        marginBottom: 15,
        borderRadius: 10
    }

});

Using bottom margin is a simple way to create vertical space between multiple cards.

15. Spacing in Forms

const styles = StyleSheet.create({

    input: {
        borderWidth: 1,
        borderColor: '#CBD5E1',
        borderRadius: 8,
        padding: 12,
        marginBottom: 15
    }

});

A bottom margin separates one input field from the next field.

16. Spacing Around Buttons

const styles = StyleSheet.create({

    button: {
        paddingVertical: 14,
        paddingHorizontal: 20,
        marginTop: 10,
        marginBottom: 10
    }

});

Padding controls the internal button size while margin separates the button from nearby components.

17. gap Property

The gap property can be used to create consistent spacing between children in a layout.

const styles = StyleSheet.create({

    container: {
        gap: 15
    }

});

It can make layouts easier to read because spacing can be defined in one place.

18. rowGap

The rowGap property controls spacing between rows of children.

const styles = StyleSheet.create({

    container: {
        rowGap: 15
    }

});

It is useful when arranging multiple elements vertically or in wrapped layouts.

19. columnGap

The columnGap property controls spacing between columns of children.

const styles = StyleSheet.create({

    container: {
        columnGap: 15
    }

});

It can be useful for horizontally arranged components.

20. Spacing with Flexbox

Flexbox properties such as justifyContent and alignItems can also help position components.

const styles = StyleSheet.create({

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

});

Flexbox controls positioning, while padding and margin provide additional spacing.

21. space-between

justifyContent: 'space-between' distributes available space between children along the main axis.

const styles = StyleSheet.create({

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

});

This is useful for layouts such as headers with content on opposite sides.

22. Creating a Spacing System

A spacing system uses a small set of reusable values throughout the application.

const spacing = {

    xs: 4,
    sm: 8,
    md: 16,
    lg: 24,
    xl: 32

};

You can then reuse these values in component styles.

23. Using Spacing Constants

const spacing = {

    sm: 8,
    md: 16,
    lg: 24

};

const styles = StyleSheet.create({

    card: {
        padding: spacing.md,
        marginBottom: spacing.lg
    },

    title: {
        marginBottom: spacing.sm
    }

});

Reusable spacing values help maintain consistency across screens.

24. Spacing in Screen Layouts

const styles = StyleSheet.create({

    container: {
        flex: 1,
        padding: 20
    },

    header: {
        marginBottom: 25
    },

    section: {
        marginBottom: 20
    },

    footer: {
        marginTop: 30
    }

});

Different spacing values can separate major sections of a screen.

25. Spacing and Responsive Design

Mobile devices have different screen sizes. Good spacing should work well across different devices.

const styles = StyleSheet.create({

    container: {
        flex: 1,
        paddingHorizontal: 20
    },

    content: {
        marginVertical: 15
    }

});

Flexbox and relative layouts are generally more flexible than depending on fixed screen dimensions.

26. Spacing with Safe Areas

Safe-area handling and normal component spacing solve different layout problems. Safe-area insets help keep content away from unsafe screen regions, while padding and margin control the spacing of the content itself.

<SafeAreaView
    style={styles.container}
>

    <View style={styles.content}>

        <Text>
            Welcome
        </Text>

    </View>

</SafeAreaView>
const styles = StyleSheet.create({

    container: {
        flex: 1
    },

    content: {
        padding: 20
    }

});

27. Complete Spacing Example

import React from 'react';

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

const spacing = {

    sm: 8,
    md: 16,
    lg: 24

};

export default function App() {

    return (

        <View style={styles.container}>

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

            <Text style={styles.label}>
                Name
            </Text>

            <TextInput
                style={styles.input}
                placeholder="Enter your name"
            />

            <Text style={styles.label}>
                Email
            </Text>

            <TextInput
                style={styles.input}
                placeholder="Enter your email"
            />

            <Pressable
                style={styles.button}
            >

                <Text style={styles.buttonText}>
                    Register
                </Text>

            </Pressable>

        </View>

    );

}

const styles = StyleSheet.create({

    container: {
        flex: 1,
        padding: spacing.lg,
        justifyContent: 'center'
    },

    title: {
        fontSize: 26,
        fontWeight: 'bold',
        textAlign: 'center',
        marginBottom: spacing.lg
    },

    label: {
        fontSize: 16,
        fontWeight: '600',
        marginBottom: spacing.sm
    },

    input: {
        borderWidth: 1,
        borderColor: '#CBD5E1',
        borderRadius: 8,
        padding: 12,
        marginBottom: spacing.md
    },

    button: {
        backgroundColor: '#2563EB',
        paddingVertical: 14,
        borderRadius: 8,
        alignItems: 'center',
        marginTop: spacing.sm
    },

    buttonText: {
        color: '#FFFFFF',
        fontSize: 16,
        fontWeight: 'bold'
    }

});

This example uses padding, margins and reusable spacing constants to create a simple registration form.

28. Common Spacing Mistakes

  • Confusing padding with margin.
  • Using too much empty space between components.
  • Using too little space and making the interface crowded.
  • Using different spacing values without a consistent system.
  • Using fixed dimensions unnecessarily.
  • Forgetting horizontal padding on smaller screens.
  • Relying only on margin when internal padding is required.
  • Ignoring spacing when designing forms and buttons.

29. Best Practices for Spacing

  • Use consistent spacing throughout the application.
  • Create reusable spacing values for larger projects.
  • Use padding for internal component space.
  • Use margin for space outside components.
  • Use Flexbox for positioning and layout.
  • Use gap where appropriate for child spacing.
  • Keep form fields separated clearly.
  • Give buttons enough internal padding for comfortable interaction.
  • Test spacing on different screen sizes.

30. Complete Spacing Workflow

Create Screen
     ↓
Set Container Padding
     ↓
Create Sections
     ↓
Add Internal Padding
     ↓
Add Margins Between Sections
     ↓
Use Flexbox
     ↓
Use Gap When Appropriate
     ↓
Create Consistent Spacing
     ↓
Test Different Screen Sizes
     ↓
Improve Layout

Spacing is a fundamental part of mobile UI design. By combining padding, margin, gap and Flexbox correctly, you can create clean, comfortable and professional React Native interfaces.

📌 Key Points

  • padding creates space inside a component.
  • margin creates space outside a component.
  • paddingTop, paddingBottom, paddingLeft and paddingRight control individual padding sides.
  • marginTop, marginBottom, marginLeft and marginRight control individual margin sides.
  • paddingHorizontal and paddingVertical simplify two-direction spacing.
  • marginHorizontal and marginVertical simplify two-direction spacing.
  • gap can create spacing between children.
  • rowGap and columnGap provide directional gap control.
  • Flexbox properties help position components.
  • A reusable spacing system keeps designs consistent.

🧠 Quick Quiz

Question: Which property creates space inside a React Native component?