29

I'm trying to set a background image in React but it only covers about 75% of the height.

It seems that the component doesn't take up all of the height.

What's the solution?

In index.js:

ReactDOM.render(<Login />, document.getElementById('root'));

In index.css:

html, body, .Login-component {
  height: 100%;
}

In Login.js:

render() {
    return (
      <div className='Login-component'>
    );
}

In Login.css

.Login-component {
    background: url(../static/login-bg.jpg) no-repeat center center fixed;
  background-size: cover;
}

The end result: Screen shot

3
  • Could you show some code?
    – soupette
    Jan 16, 2018 at 19:24
  • Questions that don't show what you have tried are too broad, there are many solutions to this problem. Jan 16, 2018 at 19:32
  • Edit: Added the code :)
    – user9226007
    Jan 16, 2018 at 19:54

8 Answers 8

48

Use vh and vw properties:

const styles = {
    container: {
        backgroundImage: `url(${backgroundImage})`,
        backgroundPosition: 'center',
        backgroundSize: 'cover',
        backgroundRepeat: 'no-repeat',
        width: '100vw',
        height: '100vh'
    }
};
26

Try using the specific property values.

In Index.css:

body, html {
    height: 100%;
    margin: 0;
}

In Login.css:

.Login-component {
    /* The image used */
    background-image: url(../static/login-bg.jpg);

    /* Full height */
    height: 100%; 

    /* Center and scale the image nicely */
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}
17

The image should be inside your src folder besides that, inside your component you do the following.-

const imgMyimageexample = require('../assets/imageexample.jpg');
const divStyle = {
  width: '88%',
  height: '800px',
  backgroundImage: `url(${imgMyimageexample})`,
  backgroundSize: 'cover'   <---- This is important
};

export default class Mycomponent extends React.Component {
  render() {
    return (
      <div className="cComponent" style={divStyle} >
        <h1 align="center">Some header example</h1>
      </div>
    );
  }
}
2
  • 1
    It won't work with the screen more than 800px, will it? Jul 1, 2020 at 14:50
  • @YukiTanka maybe you can do a height: '100%' ? Apr 23, 2022 at 1:24
0

While it is hard to troubleshoot without any code, check the root element to which your react component is being rendered to. Setting height: '100%' in the style of your component will be in the context of it's parent div.

0

Edit the CSS.

This might be what you are looking for:

<Component style={{ minHeight: '100%' }} /> 

If you meant that the background image is not covering the whole height, then add a className to your component, import the CSS where you'll do something like this

0

An answer by Jorge Carretero is helpful and in case it still doesn't work for you, try adding default

const imgMyimageexample = require('../assets/imageexample.jpg').default;
const divStyle = {
  backgroundImage: `url(${imgMyimageexample})`,
  backgroundSize: 'cover'   <---- This is important
};
0

Full typescript solution:

import imgBG from "../assets/imgBG.jpg";

interface imgStyleType {
   backgroundImage: string;
   backgroundPosition: string;
   backgroundSize: string;
   backgroundRepeat: string;
   width: string;
   height: string;
}

const imgStyle: imgStyleType = {
   backgroundImage: `url(${imgBG})`,
   backgroundPosition: "center",
   backgroundSize: "cover",
   backgroundRepeat: "no-repeat",
   width: "100vw",
   height: "100vh",
};

export default function ImgBG() {
   return (
      <>
         <div style={imgStyle} />
      </>
   );
}
-1
.Login-component{

    background-image:url(path) ;
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
    height: 100vh;
  
}
1
  • 2
    Could you explain how this solves the problem, and possibly how it's different from the other answers?
    – DavidW
    Jan 7 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.