1

I want to display image to full page.

[index.js]

import React, { Component } from 'react';
import './index.css';

export default class index extends Component {
  render() {
    return (
        <div className="container">
            123
        </div>
    );
  }
}

[index.css]

body {
    background: url("../../img/image.jpg");
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
    background-color: bisque;
}

But it seems doesn't work properly.

enter image description here

When I made this with html/css(not react), it works perfectly.

How can I solve this issue?

Hide
  • 3,199
  • 7
  • 41
  • 83

4 Answers4

2

You should have a look over to this tutorial from css-tricks.com. They explain in full detail on how to get your background image to be displayed on the full page.

In your code you should try to apply the background to the html tag instead of the body tag.

Bernhard
  • 476
  • 4
  • 7
1

Don't remember the correct way of solving this but this helped me out when I searched for the answer.

Setting a backgroundImage With React Inline Styles

G.F
  • 31
  • 1
  • 5
1

Inline style to set any full screen image:

            backgroundImage: "url(" + "https://images.pexels.com/photos/34153/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350" + ")",
            backgroundPosition: 'center',
            backgroundSize: 'cover',
            backgroundRepeat: 'noRepeat',
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
0

The background position sets the starting point of background image to center. So, set background position to its default value(top left).

body {
background: url("../../img/image.jpg");
background-position: center;
background-size: cover;
background-repeat: no-repeat;
background-color: bisque;}
noName94
  • 3,783
  • 1
  • 16
  • 32