Lesson 28 of 60 – Image Styling in React Native
47%

Image Styling in React Native

Images are an important part of mobile applications. React Native provides the Image component for displaying local and remote images. By using styles, you can control image size, shape, borders, spacing, alignment, and how the image fits inside its container.

Note: The Image component uses the same React Native styling system as other components. Common properties include width, height, borderRadius, margin, padding, and resizeMode.

1. What is Image Styling?

Image styling means controlling the appearance and layout of an image using React Native style properties.

<Image
    source={require('./assets/logo.png')}
    style={{
        width: 150,
        height: 150
    }}
/>

The width and height properties determine the size of the image.

2. Importing Image

The Image component can be imported from React Native.

import {
    Image
} from 'react-native';

You can then use the component inside your application.

3. Basic Image Styling

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

const styles = StyleSheet.create({

    image: {
        width: 200,
        height: 150
    }

});

Both width and height should normally be provided when displaying an image.

4. Setting Image Width

const styles = StyleSheet.create({

    image: {
        width: 250,
        height: 150
    }

});

The width property controls the horizontal size of the image.

5. Setting Image Height

const styles = StyleSheet.create({

    image: {
        width: 200,
        height: 250
    }

});

The height property controls the vertical size of the image.

6. Creating a Square Image

const styles = StyleSheet.create({

    image: {
        width: 150,
        height: 150
    }

});

Using the same width and height creates a square image area.

7. Border Radius

The borderRadius property rounds the corners of an image.

const styles = StyleSheet.create({

    image: {
        width: 200,
        height: 150,
        borderRadius: 15
    }

});

8. Circular Image

A square image can be displayed as a circle by using a borderRadius equal to approximately half of its size.

const styles = StyleSheet.create({

    avatar: {
        width: 120,
        height: 120,
        borderRadius: 60
    }

});

Circular images are commonly used for profile pictures.

9. Image Border

You can add a border around an image using borderWidth and borderColor.

const styles = StyleSheet.create({

    image: {
        width: 150,
        height: 150,
        borderWidth: 2,
        borderColor: '#333'
    }

});

10. Border with Border Radius

const styles = StyleSheet.create({

    image: {
        width: 180,
        height: 180,
        borderWidth: 3,
        borderColor: '#444',
        borderRadius: 90
    }

});

This can create a circular image with a border.

11. resizeMode

The resizeMode property controls how an image fits inside its defined dimensions.

<Image
    source={require('./assets/photo.jpg')}
    style={styles.image}
    resizeMode="cover"
/>

Common values include cover, contain, stretch, repeat, and center.

12. resizeMode: cover

<Image
    source={require('./assets/photo.jpg')}
    style={{
        width: 300,
        height: 200
    }}
    resizeMode="cover"
/>

cover scales the image so the entire image area is covered. Some parts of the image may be cropped.

13. resizeMode: contain

<Image
    source={require('./assets/logo.png')}
    style={{
        width: 300,
        height: 200
    }}
    resizeMode="contain"
/>

contain keeps the complete image visible inside the available image area. Empty space may remain around the image.

14. resizeMode: stretch

<Image
    source={require('./assets/photo.jpg')}
    style={{
        width: 300,
        height: 200
    }}
    resizeMode="stretch"
/>

stretch stretches the image to fill the available width and height. This can change the image's original proportions.

15. resizeMode: center

<Image
    source={require('./assets/logo.png')}
    style={{
        width: 300,
        height: 200
    }}
    resizeMode="center"
/>

The center mode displays the image centered without scaling it beyond its original size.

16. Image Margin

Margin creates space outside the image.

const styles = StyleSheet.create({

    image: {
        width: 200,
        height: 150,
        margin: 20
    }

});

17. Image Alignment

An image can be aligned inside a parent using Flexbox.

const styles = StyleSheet.create({

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

    image: {
        width: 200,
        height: 150
    }

});

Here, alignItems: 'center' centers the image horizontally inside the parent.

18. Full Width Image

const styles = StyleSheet.create({

    image: {
        width: '100%',
        height: 220
    }

});

A percentage width can make an image adapt to the width of its parent.

19. Responsive Image

const styles = StyleSheet.create({

    image: {
        width: '100%',
        height: 200,
        borderRadius: 12
    }

});

Combining percentage width with an appropriate height can create an image that adapts to different parent widths.

20. Image Inside a Card

<View style={styles.card}>

    <Image
        source={require('./assets/course.jpg')}
        style={styles.image}
        resizeMode="cover"
    />

    <Text style={styles.title}>
        React Native Course
    </Text>

</View>

Images can be combined with text and other components to create cards.

21. Image Background Style

The ImageBackground component can be used when text or other components need to appear over an image.

import {
    ImageBackground,
    Text
} from 'react-native';

<ImageBackground
    source={require('./assets/banner.jpg')}
    style={styles.banner}
>

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

</ImageBackground>

22. Image Opacity

The opacity property controls how transparent an image appears.

const styles = StyleSheet.create({

    image: {
        width: 200,
        height: 150,
        opacity: 0.7
    }

});

The value normally ranges from 0 to 1.

23. Image with Shadow

On supported platforms, shadow-related style properties can be used to create visual depth.

const styles = StyleSheet.create({

    image: {
        width: 180,
        height: 180,
        borderRadius: 15,
        elevation: 5
    }

});

For cross-platform designs, remember that shadow properties can behave differently between platforms.

24. Image with Conditional Styling

<Image
    source={require('./assets/photo.jpg')}
    style={[
        styles.image,
        isLarge ? styles.large : styles.small
    ]}
/>

const styles = StyleSheet.create({

    image: {
        borderRadius: 12
    },

    large: {
        width: 300,
        height: 200
    },

    small: {
        width: 150,
        height: 100
    }

});

Multiple styles can be combined based on application conditions.

25. Image with useWindowDimensions

import {
    View,
    Image,
    useWindowDimensions
} from 'react-native';

export default function App() {

    const { width } =
        useWindowDimensions();

    return (

        <View>

            <Image
                source={require('./assets/photo.jpg')}
                style={{
                    width: width - 40,
                    height: 220,
                    borderRadius: 15
                }}
                resizeMode="cover"
            />

        </View>

    );

}

The image width changes according to the available window width.

26. Common Image Styling Mistakes

  • Forgetting to specify an appropriate image size.
  • Using extremely large fixed dimensions.
  • Using stretch when preserving image proportions is important.
  • Forgetting to choose an appropriate resizeMode.
  • Using a very large border radius without a square image.
  • Making images wider than their parent container.
  • Ignoring different screen sizes.
  • Using very large local images unnecessarily.

27. Complete Image Styling Example

import React from 'react';

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

export default function App() {

    return (

        <View style={styles.container}>

            <Image
                source={require('./assets/student.jpg')}
                style={styles.image}
                resizeMode="cover"
            />

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

            <Text style={styles.description}>
                React Native Student
            </Text>

        </View>

    );

}

const styles = StyleSheet.create({

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

    image: {
        width: 180,
        height: 180,
        borderRadius: 90,
        borderWidth: 3,
        borderColor: '#333',
        marginBottom: 20
    },

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

    description: {
        fontSize: 16,
        marginTop: 8
    }

});

28. Image Styling Best Practices

  • Always choose appropriate image dimensions.
  • Use resizeMode according to the design.
  • Use borderRadius carefully.
  • Use percentage widths for responsive layouts when appropriate.
  • Use useWindowDimensions when image size depends on screen width.
  • Use cover when filling a defined image area is important.
  • Use contain when the complete image should remain visible.
  • Keep image spacing consistent with the rest of the interface.
  • Test images on different screen sizes.

29. Choosing the Right resizeMode

resizeMode Purpose
cover Fills the image area and may crop the image.
contain Shows the complete image inside the area.
stretch Stretches the image to fill the area.
center Centers the image without scaling it beyond its original size.
repeat Repeats the image to fill the area where supported.

30. Complete Image Styling Workflow

Select Image
      ↓
Import Image Component
      ↓
Set Width and Height
      ↓
Choose resizeMode
      ↓
Add Border Radius if Needed
      ↓
Add Border if Needed
      ↓
Add Margin and Alignment
      ↓
Make Image Responsive
      ↓
Test Different Screen Sizes
      ↓
Improve Image Appearance

Image styling becomes easier when you understand dimensions, border radius, resize modes, alignment, spacing, and responsive design. These techniques can be combined to create professional image-based interfaces.

📌 Key Points

  • Image is used to display images in React Native.
  • width and height control image size.
  • borderRadius can create rounded or circular images.
  • borderWidth and borderColor create image borders.
  • resizeMode controls how an image fits its area.
  • cover fills the area and may crop the image.
  • contain keeps the complete image visible.
  • Percentage widths can help create responsive images.
  • useWindowDimensions can be used for dynamic image sizing.
  • Good image styling should work across different screen sizes.

🧠 Quick Quiz

Question: Which resizeMode is commonly used when an image should fill the available image area, even if some parts of the image are cropped?