简体   繁体   中英

Correct way to set background image URL in React JSX object

I would like to set the background image in my style JS object. For some reason the following is not working, no image appears and no errors?

React

const homeStyle = {
    width: '100%',
    height: '100vh',
    backgroundImage: 'url("../../assets/images/home.png")',
    backgroundRepeat: 'no-repeat',
    backgroundSize: 'cover',
    paddingTop: '160px',
    paddingLeft: '400px'
  };

 <div style={homeStyle}></div>

Any idea's?

try to import the image with

import Background from '../../assets/images/home.png';

and replace the backgroundImage property with

backgroundImage: `url(${Background})`,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM