Lesson 21 of 60 – StyleSheet in React Native
35%

StyleSheet in React Native

The StyleSheet API in React Native is used to organize and manage styles for mobile application components. It allows you to create reusable style objects for components such as View, Text, Image, TextInput and Pressable.

Note: React Native does not use traditional CSS files in the same way as a web application. Instead, styles are commonly written as JavaScript objects and managed using StyleSheet.create().

1. What is StyleSheet?

StyleSheet is a React Native API used to create and organize component styles.

import {
    StyleSheet
} from 'react-native';

It helps keep styling separate from the JSX structure and makes styles easier to reuse.

2. Importing StyleSheet

StyleSheet is imported from the react-native package.

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

3. Basic StyleSheet Syntax

const styles = StyleSheet.create({

    container: {
        flex: 1
    },

    title: {
        fontSize: 24
    }

});

The object contains named styles that can later be used with the style prop.

4. Using a Style with View

const styles = StyleSheet.create({

    container: {
        backgroundColor: 'lightblue',
        padding: 20
    }

});

<View style={styles.container}>
</View>

The styles.container object is applied to the View.

5. Styling Text

const styles = StyleSheet.create({

    title: {
        fontSize: 28,
        fontWeight: 'bold',
        color: 'blue'
    }

});

<Text style={styles.title}>
    Welcome
</Text>

Text styles commonly include font size, weight, color and alignment.

6. backgroundColor

The backgroundColor property changes the background color of a component.

const styles = StyleSheet.create({

    box: {
        backgroundColor: 'blue',
        padding: 20
    }

});

7. color

The color property changes the color of text.

const styles = StyleSheet.create({

    text: {
        color: 'white'
    }

});

Remember that color is mainly used with text components.

8. fontSize

The fontSize property controls the size of text.

const styles = StyleSheet.create({

    heading: {
        fontSize: 30
    },

    paragraph: {
        fontSize: 16
    }

});

9. fontWeight

The fontWeight property controls the thickness of text.

const styles = StyleSheet.create({

    title: {
        fontWeight: 'bold'
    }

});

Common values include normal and bold. Platform-specific supported values can vary.

10. textAlign

The textAlign property controls the alignment of text.

const styles = StyleSheet.create({

    title: {
        textAlign: 'center'
    }

});

Other common values include left and right.

11. Width and Height

The width and height properties control the dimensions of a component.

const styles = StyleSheet.create({

    box: {
        width: 200,
        height: 100
    }

});

12. Padding

Padding creates space inside a component between its content and its boundary.

const styles = StyleSheet.create({

    box: {
        padding: 20
    }

});

You can also control individual sides.

const styles = StyleSheet.create({

    box: {
        paddingTop: 10,
        paddingBottom: 20,
        paddingLeft: 15,
        paddingRight: 15
    }

});

13. Margin

Margin creates space outside a component.

const styles = StyleSheet.create({

    box: {
        margin: 20
    }

});

Individual margins can also be controlled.

const styles = StyleSheet.create({

    box: {
        marginTop: 20,
        marginBottom: 10
    }

});

14. Border Radius

The borderRadius property creates rounded corners.

const styles = StyleSheet.create({

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

});

It is commonly used for cards, buttons and input fields.

15. Border Properties

React Native provides several border-related style properties.

const styles = StyleSheet.create({

    box: {
        borderWidth: 1,
        borderColor: 'gray',
        borderRadius: 8
    }

});

These properties are useful for cards, forms and TextInput fields.

16. Flex Property

The flex property controls how a component uses available space.

const styles = StyleSheet.create({

    container: {
        flex: 1
    }

});

A common screen layout starts with a container having flex: 1.

17. flexDirection

The flexDirection property controls the direction of children in a Flexbox container.

const styles = StyleSheet.create({

    container: {
        flexDirection: 'row'
    }

});

Common values are row and column.

18. justifyContent

The justifyContent property controls how children are positioned along the main axis.

const styles = StyleSheet.create({

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

});

Common values include center, flex-start, flex-end, space-between and space-around.

19. alignItems

The alignItems property controls how children are positioned along the cross axis.

const styles = StyleSheet.create({

    container: {
        alignItems: 'center'
    }

});

It is frequently used together with justifyContent.

20. Combining Multiple Styles

Multiple style objects can be combined using an array.

const styles = StyleSheet.create({

    box: {
        padding: 20,
        borderRadius: 10
    },

    blueBox: {
        backgroundColor: 'blue'
    }

});

<View
    style={[
        styles.box,
        styles.blueBox
    ]}
>
</View>

21. Conditional Styles

Styles can be applied conditionally using JavaScript expressions.

const active = true;

<View
    style={[
        styles.box,
        active && styles.activeBox
    ]}
>
</View>

This is useful when the appearance of a component depends on application state.

22. StyleSheet with TextInput

const styles = StyleSheet.create({

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

});

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

StyleSheet makes form fields easier to keep consistent.

23. StyleSheet with Pressable

const styles = StyleSheet.create({

    button: {
        backgroundColor: 'blue',
        padding: 15,
        borderRadius: 8,
        alignItems: 'center'
    },

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

});

<Pressable style={styles.button}>

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

</Pressable>

24. StyleSheet with Image

const styles = StyleSheet.create({

    image: {
        width: 150,
        height: 150,
        borderRadius: 75
    }

});

<Image
    source={require('./assets/profile.png')}
    style={styles.image}
/>

StyleSheet can control image dimensions, borders and other visual properties.

25. StyleSheet Organization

For small components, styles can be placed at the bottom of the same file.

export default function App() {

    return (
        <View style={styles.container}>
            <Text style={styles.title}>
                Hello
            </Text>
        </View>
    );

}

const styles = StyleSheet.create({

    container: {
        flex: 1
    },

    title: {
        fontSize: 24
    }

});

For larger projects, shared styles can also be organized into separate files when that improves maintainability.

26. StyleSheet vs Inline Styles

StyleSheet Inline Style
Styles are named and organized Styles are written directly in JSX
Easy to reuse Useful for small or dynamic styles
Keeps JSX cleaner Can make JSX longer
Good for larger components Useful for simple one-off styles
<View
    style={{
        padding: 20,
        backgroundColor: 'blue'
    }}
>
</View>

Both approaches are valid. StyleSheet is useful for keeping reusable styles organized.

27. Complete StyleSheet Example

import React from 'react';

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

export default function App() {

    return (

        <View style={styles.container}>

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

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

            <TextInput
                style={styles.input}
                placeholder="Enter password"
                secureTextEntry
            />

            <Pressable
                style={styles.button}
            >

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

            </Pressable>

        </View>

    );

}

const styles = StyleSheet.create({

    container: {
        flex: 1,
        justifyContent: 'center',
        padding: 20,
        backgroundColor: '#F5F5F5'
    },

    title: {
        fontSize: 28,
        fontWeight: 'bold',
        textAlign: 'center',
        marginBottom: 25
    },

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

    button: {
        backgroundColor: 'blue',
        padding: 15,
        borderRadius: 8,
        alignItems: 'center'
    },

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

});

This example demonstrates how several React Native components can share styles created with StyleSheet.

28. Common StyleSheet Mistakes

  • Forgetting to import StyleSheet.
  • Using web CSS syntax that React Native does not support.
  • Forgetting to pass the style using the style prop.
  • Using incorrect property names.
  • Writing very large style objects without organization.
  • Forgetting that layout uses Flexbox by default.
  • Using CSS units such as px in standard React Native style values.
  • Applying text-specific properties to components where they do not apply.

29. Best Practices for StyleSheet

  • Use meaningful style names.
  • Keep reusable styles in StyleSheet.create().
  • Use Flexbox for responsive layouts.
  • Keep spacing consistent throughout the application.
  • Use arrays when combining multiple styles.
  • Use conditional styles when the UI state changes.
  • Keep component-specific styles close to the component.
  • Move genuinely shared styles into reusable files when appropriate.
  • Test layouts on different screen sizes.

30. Complete StyleSheet Workflow

Create Component
       ↓
Create StyleSheet
       ↓
Define Named Styles
       ↓
Apply style Prop
       ↓
Use Flexbox
       ↓
Add Spacing
       ↓
Add Colors
       ↓
Add Typography
       ↓
Combine Styles
       ↓
Test on Device
       ↓
Improve Responsive Layout

StyleSheet provides a structured way to manage the visual appearance of React Native applications. Understanding StyleSheet together with Flexbox, spacing, typography and component-specific properties will help you create professional mobile interfaces.

📌 Key Points

  • StyleSheet is used to organize React Native styles.
  • StyleSheet.create() creates named style objects.
  • Styles are applied using the style prop.
  • React Native layouts primarily use Flexbox.
  • Common properties include padding, margin, backgroundColor, fontSize and borderRadius.
  • Multiple styles can be combined using an array.
  • Conditional styles can be used for dynamic interfaces.
  • StyleSheet works with View, Text, Image, TextInput and other components.
  • Good style organization makes applications easier to maintain.

🧠 Quick Quiz

Question: Which React Native API is commonly used to create and organize styles?