173

I wanted to add a full-screen image to the View so I write this code

return (
    <View style={styles.container}>
        <Image source={require('image!egg')}  style={styles.backgroundImage}/>
    </View>
)

and defined the style as

var styles = StyleSheet.create({
container: {
     flex: 1,
     justifyContent: 'center',
     alignItems: 'center',
     backgroundColor: '#F5FCFF',
     flexDirection: 'column',
},
     backgroundImage:{
     width:320,
     height:480,
   }
...

but in this way how am I supposed to find the actual iPhone screen size?

I've seen an API to access the Pixel Density but nothing about the screen size...

Any idea?

TylerH
  • 20,799
  • 66
  • 75
  • 101
talpaz
  • 1,956
  • 2
  • 13
  • 14
  • How about performance? Is it ok to use `.png` or `.jpg`? Is ok to store wallpaper image inside app directory tree? Or is it better to use something else? `.svg` or anything? – Green Jul 04 '18 at 15:57
  • @Sabito 錆兎standswithUkraine The criterion of React Native is pivotal to the question. so it belongs in the title. – TylerH Sep 19 '22 at 14:34

28 Answers28

196

(This has been deprecated now you can use ImageBackground)

This is how I've done it. The main deal was getting rid of the static fixed sizes.

class ReactStrap extends React.Component {
  render() {
    return (
      <Image source={require('image!background')} style={styles.container}>
        ... Your Content ...
      </Image>
    );
  }
}

var styles = StyleSheet.create({
  container: {
    flex: 1,
    // remove width and height to override fixed static size
    width: null,
    height: null,
  }
};
Yash Kumar Verma
  • 9,427
  • 2
  • 17
  • 28
oronbz
  • 2,219
  • 2
  • 12
  • 17
  • 1
    Yes, that's how it should be done by docs https://facebook.github.io/react-native/docs/image.html#background-image-via-nesting – Daniel Steigerwald Oct 19 '15 at 21:47
  • 3
    Thank you! Make sure the `Image` is your first component returned, the container. At first, I accidently nested the `Image` within a `View` that was the container. – Glavin001 May 18 '16 at 19:50
  • Does this work with https://facebook.github.io/react-native/docs/tabbarios.html as content. Doesn't work for me, tabs are not clickable – divyenduz Oct 29 '16 at 19:47
  • Id like to have the whole app (Or Navigator) wrapped inside but its not working... – sebap Nov 04 '16 at 18:54
  • Changing 'resizeMode' to 'stretch' didn't work! This worked like charm!! – Virat18 Jun 16 '17 at 11:45
  • 7
    YellowBox.js:76 Using with children is deprecated and will be an error in the near future. Please reconsider the layout or use instead. When I add contents to , this warning shows up. it's really annoying. – wzso Aug 01 '17 at 04:01
  • 5
    this is actually deprecated. Use ImageBackground or (best) position:absolute – Michele Dec 03 '17 at 14:14
  • How about performance? Is it ok to use `.png` or `.jpg`? Is ok to store wallpaper image inside app directory tree? Or is it better to use something else? `.svg` or anything? – Green Jul 04 '18 at 15:59
126

You can use flex: 1 styling on an <Image> element to have it fill the whole screen. You can then use one of the Image resize modes to have the image completely fill the element:

<Image source={require('image!egg')} style={styles.backgroundImage} />

Style:

import React from 'react-native';

let { StyleSheet } = React;

let styles = StyleSheet.create({
  backgroundImage: {
    flex: 1,
    resizeMode: 'cover', // or 'stretch'
  }
});

I'm pretty sure you can get rid of the <View> wrapping your image and this will work.

owencm
  • 8,384
  • 6
  • 38
  • 54
Josh
  • 1,362
  • 1
  • 10
  • 6
  • 2
    Yup, it works, I've moved the Image element as top most and set its style as `backgroundImage:{ justifyContent: 'center', alignItems: 'center', flex: 1, resizeMode: Image.resizeMode.contain, },` Thanks – talpaz Mar 28 '15 at 23:47
  • 4
    After struggling with this for a while I found it easiest to wrap the `Image` component in an absolutely positioned `View` to allow the background image to appear behind other content on the screen. – vhs May 29 '15 at 19:42
  • 3
    Important edit: `` is now `` – Username Jan 28 '16 at 02:28
  • Now that z-index is supported, would you change this answer? – makenova Jul 24 '16 at 22:13
  • It is fine but the image is stretching and scroll is enabled in screen – Ananta Prasad Jun 13 '18 at 11:17
  • How about performance? Is it ok to use `.png` or `.jpg`? Is ok to store wallpaper image inside app directory tree? Or is it better to use something else? `.svg` or anything? – Green Jul 04 '18 at 15:58
  • @Green https://medium.com/reactnative/background-images-in-react-native-191f3ed95a45 – julian salas Jul 10 '18 at 20:20
  • can you use @media queries to change image depending on the scroll? – Mr-Programs Jan 08 '19 at 18:08
45

Note: This solution is old. Please refer to https://facebook.github.io/react-native/docs/images.html#background-image-via-nesting instead

Try this solution. It is officially supported. I have just tested it and works flawlessly.

var styles = StyleSheet.create({
  backgroundImage: {
    flex: 1,
    alignSelf: 'stretch',
    width: null,
  }
});

And as for using it as Background image, just do the following.

<Image style={styles.backgroundImage}>
  <View>
    <Text>All your stuff</Text>
  </View>
</Image>
Vinod Sobale
  • 862
  • 11
  • 18
29

Update March 2018 Using Image is deprecated use ImageBackground

  <ImageBackground 
          source={{uri: 'https://images.pexels.com/photos/89432/pexels-photo-89432.jpeg?h=350&dpr=2&auto=compress&cs=tinysrgb'}}
          style={{ flex: 1,
            width: null,
            height: null,
            }}
        >
       <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Your Contents</Text>
       </View>

 </ImageBackground >
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • Documentation: https://facebook.github.io/react-native/docs/images.html#background-image-via-nesting – Joy Apr 18 '18 at 05:52
  • 1
    This is correct, now you have to use ImageBackground, Image as a container is deprecated. View as a container here is not required you can use just ImageBackground as the main container. – Diego Unanue Aug 27 '19 at 15:19
21

I tried several of these answers to no avail for android using react-native version = 0.19.0.

For some reason, the resizeMode inside my stylesheet did not work appropriately? However, when sytlesheet had

backgroundImage: {
flex: 1,
width: null,
height: null,
}

and, within the Image tag I specified the resizeMode:

<Image source={require('path/to/image.png')} style= {styles.backgroundImage} resizeMode={Image.resizeMode.sretch}>

It worked perfectly! As mentioned above, you can use Image.resizeMode.cover or contain as well.

Hope this helps!

Tab
  • 211
  • 2
  • 3
16

UPDATE to ImageBackground

Since using <Image /> as a container is deprecated for a while, all answers actually miss something important. For proper use choose <ImageBackground /> with style and imageStyle prop. Apply all Image relevant styles to imageStyle.

For example:

<ImageBackground
    source={yourImage}
    style={{
      backgroundColor: '#fc0',
      width: '100%', // applied to Image
      height: '100%' 
    }}
    imageStyle={{
      resizeMode: 'contain' // works only here!
    }}
>
    <Text>Some Content</Text>
</ImageBackground>

https://github.com/facebook/react-native/blob/master/Libraries/Image/ImageBackground.js

Pawel
  • 1,560
  • 1
  • 10
  • 11
13

Based on Braden Rockwell Napier's answer, I made this BackgroundImage component

BackgroundImage.js

import React, { Component } from 'react'
import { Image } from 'react-native'

class BackgroundImage extends Component {
  render() {
    const {source, children, style, ...props} = this.props
    return (
      <Image source={ source }
             style={ { flex: 1, width: null, height: null, ...style } }
             {...props}>
        { children }
      </Image>
    )
  }
}
BackgroundImage.propTypes = {
  source: React.PropTypes.object,
  children: React.PropTypes.object,
  style: React.PropTypes.object
}
export default BackgroundImage

someWhereInMyApp.js

 import BackgroundImage from './backgroundImage'
 ....
 <BackgroundImage source={ { uri: "https://facebook.github.io/react-native/img/header_logo.png" } }>
    <Text>Test</Text>
 </BackgroundImage>
Community
  • 1
  • 1
Ryan Wu
  • 5,963
  • 2
  • 36
  • 47
12

If you want to use it as a background image, you will need to use the new <ImageBackground> component introduced at the end of June 2017 in v0.46. It supports nesting while <Image> soon won't.

Here is the commit summary:

We are removing support of nesting views inside component. We decided to do this because having this feature makes supporting intrinsinc content size of the <Image> impossible; so when the transition process is complete, there will be no need to specify image size explicitly, it can be inferred from actual image bitmap.

And this is the step #0.

is very simple drop-in replacement which implements this functionality via very simple styling. Please, use instead of if you want to put something inside.

Antoine
  • 5,504
  • 5
  • 33
  • 54
11

Oh God Finally I find a great way for React-Native V 0.52-RC and native-base:

Your Content Tag Should be something like this: //==============================================================

<Content contentContainerStyle={styles.container}>
    <ImageBackground
        source={require('./../assets/img/back.jpg')}
        style={styles.backgroundImage}>
        <Text>
            Some text here ...
        </Text>
    </ImageBackground>
</Content>

And Your essential style is : //==============================================================

container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
},
backgroundImage:{
    flex : 1,
    width : '100%'
}

It works fine friends ... have fun

Ali Esfandiari
  • 316
  • 4
  • 13
  • Hi, Just want to make sure ImageBackground where is that imported from? – Rudolph Opperman Mar 04 '18 at 14:33
  • OK so ImageBackground I imported from react-native however, I can not set with:100% – Rudolph Opperman Mar 04 '18 at 14:40
  • Did you ever run into an issue where it displays on Android Emulator but not on the device? – Rudolph Opperman Mar 04 '18 at 19:16
  • Sorry for my late answer , you can Import ImageBackground from React Native: import {ImageBackground} from 'react-native'; witch device did you test? I didn't find any problem on device. – Ali Esfandiari Mar 05 '18 at 14:31
  • My Emulator was a Nexus 5 specs. And my android was Wileyfox Swift. The main difference was the resolution so I checked it on the image and seems like if the image resolution is higher then your device it does not show. I tried making the image smaller with styles but did not work so I changed resolution on image lower and now it seems to work fine on. The resolution being lower wasn't really an issue cause it is a login screen and there are some text inputs and buttons on it. Thank you very much. – Rudolph Opperman Mar 06 '18 at 06:41
8
import { ImageBackground } from "react-native";
<ImageBackground
     style={{width: '100%', height: '100%'}}
     source={require('../assets/backgroundLogin.jpg ')}> //path here inside
    <Text>React</Text>
</ImageBackground>
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Naved Khan
  • 1,699
  • 16
  • 13
5

For me this works fine, I used ImageBackground to set background image:

import React from 'react';
import { SafeAreaView, StyleSheet, ScrollView, View, Text, StatusBar, Image, ImageBackground } from 'react-native';
const App: () => React$Node = () => {
return (
    <>
      <StatusBar barStyle="dark-content" />
      <View style={styles.container}> 
      <ImageBackground source={require('./Assets.xcassets/AppBackGround.imageset/2x.png')} style={styles.backgroundImage}>
        <View style={styles.sectionContainer}>
              <Text style={styles.title}>Marketing at the speed of today</Text>
        </View>
      </ImageBackground>
      </View>
    </>
  );
};


const styles = StyleSheet.create({
  container: {
    flex: 1,
    fontFamily: "-apple-system, BlinkMacSystemFont Segoe UI",
    justifyContent: "center",
    alignItems: "center",
  },
  backgroundImage: {
    flex: 1,
    resizeMode: 'cover',
    height: '100%',
    width: '100%'
  },
  title:{
    color: "#9A9A9A",
    fontSize: 24,
    justifyContent: "center",
    alignItems: "center",
  },
sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
});

export default App;
Abdul Karim Khan
  • 4,256
  • 1
  • 26
  • 30
4

Since 0.14 this method won't work, so I built a static component that will make this simple for you guys. You can just paste this in or reference it as a component.

This should be re-useable and it will allow you to add additional styles and properties as-if it were a standard <Image /> component

const BackgroundImage = ({ source, children, style, ...props }) => {
  return (
      <Image
        source={source}
        style={{flex: 1, width: null, height: null, ...style}}
        {...props}>
        {children}
      </Image>
  );
}

just paste this in and then you can use it like image and it should fit the entire size of the view it is in (so if it's the top view it will fill your screen.

<BackgroundImage source={require('../../assets/backgrounds/palms.jpg')}>
    <Scene styles={styles} {...store} />
</BackgroundImage>

Click here for a Preview Image

4

Latest as of Oct'17 (RN >= .46)

import React from 'react';
import { 
  ...
  ImageBackground,
} from 'react-native';

render() {
  return (
    <ImageBackground source={require('path/to/img')} style={styles.urStyle}>
    </ImageBackground>
  );
}

http://facebook.github.io/react-native/releases/0.49/docs/images.html#background-image-via-nesting

Adesh Shah
  • 442
  • 4
  • 12
3

To handle this use case, you can use the <ImageBackground> component, which has the same props as <Image>, and add whatever children to it you would like to layer on top of it.

Example:

return (
  <ImageBackground source={...} style={{width: '100%', height: '100%'}}>
    <Text>Inside</Text>
  </ImageBackground>
);

For more: ImageBackground | React Native

Note that you must specify some width and height style attributes.

2

You need to ensure your Image has resizeMode={Image.resizeMode.contain} or {Image.resizeMode.stretch} and set image style width to null

<Image source={CharacterImage} style={{width: null,}} resizeMode={Image.resizeMode.contain}/>
Eric Kim
  • 10,617
  • 4
  • 29
  • 31
2

The width and height with value null doesn't work for me, then I thought to use top, bottom, left, and right position and it worked. Example:

bg: {
        position: 'absolute',
        top: 0,
        bottom: 0,
        left: 0,
        right: 0,
        resizeMode: 'stretch',
},

And the JSX:

<Image style={styles.bg} source={{uri: 'IMAGE URI'}} />
dapx
  • 59
  • 1
  • 2
2

(RN >= .46)

the component cannot contain children if you want to render content on top of the image, consider using absolute positioning.

or you can use ImageBackground

import React from 'react';
import { 
  ...
  StyleSheet,
  ImageBackground,
} from 'react-native';

render() {
  return (
    
  <ImageBackground source={require('path/to/img')} style={styles.backgroundImage} >
      <View style={{flex: 1, backgroundColor: 'transparent'}} />
      <View style={{flex: 3,backgroundColor: 'transparent'}} >
  </ImageBackground>
    
  );
}

const styles = StyleSheet.create({
  backgroundImage: {
        flex: 1,
        width: null,
        height: null,
        resizeMode: 'cover'
    },
});
2

Easiest way to implemet background:

<ImageBackground style={styles.container} source={require('../../images/screen_login.jpg')}>
  <View style={styles.logoContainer}>
    <Image style={styles.logo}
      source={require('../../images/logo.png')}
    />
  </View> 
  <View style={styles.containerTextInput}>
    < LoginForm />
  </View>   
</ImageBackground>

const styles = StyleSheet.create({
  container: {
    flex: 1,
    //   backgroundColor:"#0984e3"
  },
  containerTextInput: {
    marginTop: 10,
    justifyContent: 'center'
  },

  logoContainer: {
    marginTop: 100,
    justifyContent: 'center',
    alignItems: 'center'
  },
  logo: {
    height: 150,
    width: 150
  }
});
Samiksha Jagtap
  • 585
  • 2
  • 13
  • 29
Ahmad Sadiq
  • 153
  • 1
  • 1
  • 8
2

If you want to add a background image you can do so by using but first you have to import this from 'react-native' as follows:

import {ImageBackground} from 'react-native';

then:

    export default function App() {
    
    return (
        <View style={styles.body}>

            <ImageBackground source={require('./path/to/yourimage')} style={styles.backgroungImage}>
                <View style={styles.container}>Hello world!
                </View>
            </ImageBackground>
        </View>
    );
}

    const styles = StyleSheet.create({
    backgroungImage: {
    flex: 1,
    maxWidth: '100%',
  }
});
Sam Kihika
  • 101
  • 1
  • 6
2

import React from 'react';
import { 
  ...
  StyleSheet,
  ImageBackground,
} from 'react-native';

render() {
  return (
    
  <ImageBackground source={require('path/to/img')} style={styles.backgroundImage} >
      <View style={{flex: 1, backgroundColor: 'transparent'}} />
      <View style={{flex: 3,backgroundColor: 'transparent'}} >
  </ImageBackground>
    
  );
}

const styles = StyleSheet.create({
  backgroundImage: {
        flex: 1,
        width: null,
        height: null,
        resizeMode: 'cover'
    },
});
1

incase you didn't solve it yet, React Native v.0.42.0 have resizeMode

<Image style={{resizeMode: 'contain'}} source={require('..img.png')} />
1
import React, { Component } from 'react';
import { Image, StyleSheet } from 'react-native';

export default class App extends Component {
  render() {
    return (
      <Image source={{uri: 'http://i.imgur.com/IGlBYaC.jpg'}} style={s.backgroundImage} />
    );
  }
}

const s = StyleSheet.create({
  backgroundImage: {
      flex: 1,
      width: null,
      height: null,
  }
});

You can try it at: https://sketch.expo.io/B1EAShDie (from: github.com/Dorian/sketch-reactive-native-apps)

Docs: https://facebook.github.io/react-native/docs/images.html#background-image-via-nesting

Dorian
  • 22,759
  • 8
  • 120
  • 116
1

You can also use your image as a container:

render() {
    return (
        <Image
            source={require('./images/background.png')}
            style={styles.container}>
            <Text>
              This text will be on top of the image
            </Text>
        </Image>
    );
}


const styles = StyleSheet.create({
    container: {
        flex: 1,
        width: undefined,
        height: undefined,
        justifyContent: 'center',
        alignItems: 'center',
      },
});
Agu Dondo
  • 12,638
  • 7
  • 57
  • 68
1

I've heard about having to use BackgroundImage because in future you are supposed to not be able to nest the Image tag. But I could not get BackgroudImage to properly display my background. What I did was nest my Image inside a View tag and style both the outer View as well as the image. Keys were setting width to null, and setting resizeMode to 'stretch'. Below is my code:

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

export default class BasicImage extends Component {
 constructor(props) {
   super(props);

   this.state = {};
 }

 render() {
  return (
   <View style={styles.container}>
       <Image 
         source={this.props.source}
         style={styles.backgroundImage}
       />
      </View>
  )
 }
}

const styles = StyleSheet.create({   
  container: {
   flex: 1,
   width: null,
   height: null,
   marginBottom: 50
  },
    text: {
      marginLeft: 5,
      marginTop: 22,
      fontFamily: 'fontawesome',
        color: 'black',
        fontSize: 25,
        backgroundColor: 'rgba(0,0,0,0)',
    },
  backgroundImage: {
   flex: 1,
   width: null,
   height: null,
   resizeMode: 'stretch',
  }
});
Christopher Adams
  • 1,240
  • 10
  • 14
1

Use <ImageBackground> as already said by antoine129. Using <Image> with children is deprecated now.

class AwesomeClass extends React.Component {
  render() {
    return (
      <ImageBackground source={require('image!background')} style={styles.container}>
        <YourAwesomeComponent/>
      </ImageBackground>
    );
  }
}

var styles = StyleSheet.create({
  container: {
    flex: 1,
  }
};
Aung Myat Hein
  • 4,018
  • 1
  • 36
  • 42
0

An another easy solution:

<Image source={require('../assets/background.png')}
      style={{position: 'absolute', zIndex: -1}}/>

<View style={{flex: 1, position: 'absolute'}}>

  {/*rest of your content*/}
</View>
Serdar D.
  • 3,055
  • 1
  • 30
  • 23
0

I solved my background image issue using this code.

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

import { TextInput,Button,IconButton,Colors,Avatar } from 'react-native-paper';

class SignInScreen extends React.Component {

    state = {
       UsernameOrEmail  : '',
       Password : '',
     }
    render() {
      return (
             <ImageBackground  source={require('../assets/icons/background3.jpg')} style {styles.backgroundImage}>
              <Text>React Native App</Text>
            </ImageBackground>
          );
    }
  }


    export default SignInScreen;

    const styles = StyleSheet.create({
     backgroundImage: {
      flex: 1,
      resizeMode: 'cover', // or 'stretch'
     }
   });
Adeel Ahmed Baloch
  • 672
  • 2
  • 7
  • 15
0

ImageBackground might have limit

Actually, you can use directly and it is not deprecated.

If you want to add Background Image in React Native and also wants to add other elements on that Background Image, follow the step below:

  1. Create a Container View
  2. Create an Image element with 100% width and height. Also resizeMode: 'Cover'
  3. Create another View element under Image element with position: 'absolute'

This is the code I use:

import React, { Component } from 'react';
import {Text, View, Image} from 'react-native';
import Screen from '../library/ScreenSize'

export default class MenuScreen extends Component {
    static navigationOptions = {
      header: null
    }
    render() {
        return (
          <View style={{ flex: 1 }}>
            <Image
              style={{
                resizeMode: "cover",
                width: "100%",
                height: "100%",
                justifyContent: "center",
                alignItems: "center",
                opacity: 0.4
              }}
              source={require("../assets/images/menuBackgroundImage.jpg")}
            ></Image>

            <View style={{
                width: Screen.width,
                height: Screen.height * 0.55,
                position: 'absolute',
                bottom: 0}}>
                <Text style={{
                    fontSize: 48
                }}>Glad to Meet You!</Text>
            </View>
          </View>
        );
    }
}

Enjoy Coding....

Output:

This is the output of my code.

Community
  • 1
  • 1
Chhay Rith Hy
  • 1,359
  • 1
  • 7
  • 4