1

i'm new to ReactJs and my question may seem stupid to many of you, but i have a problem with setting the background. My React code is:

<header className="masthead ttt">
          <div className="overlay"></div>
          <div className="container">
            <div className="row">
              <div className="col-lg-8 col-md-10 mx-auto">
                <div className="site-heading">
                  <h1>Blog Name</h1>
                  <span className="subheading">blablablablabla</span>
                </div>
              </div>
            </div>
          </div>
        </header> 

So basically i'm trying to set an image as background in my header , so i created a class .ttt and modified it to my css that way :

.ttt{
  background-image: url("../img/home-bg.jpg");
}

And finally here's the way the folders are set :

enter image description here

Am i doing anything wrong in my code ? Or there's another way of doing things in ReactJS! Thanks!

Hiimdjango
  • 431
  • 5
  • 21

3 Answers3

1

So far what I understood from your problem is that you are not getting the image on the front-end. While checking it through developer options it must be pointing you to a blank div at the top of the screen. If that's the case then it is because that every background image needs some size to be displayed.

You can either give width and height or you can give background-size property as follows:

.ttt{
    background-image: url("../img/home-bg.jpg");
    width: 100%;
    height: 100%;  // giving height is not compulsary but just for good practice.
}

OR

.ttt{
    background-image: url("../img/home-bg.jpg");
    background-size: 100% 100%; // First value: {Width}   Second value: {Height}
}

This would resolve your issue.

Prakhar Mittal
  • 6,315
  • 1
  • 14
  • 31
0

Use inline css in javascript like this:

import Background from '../img/home-bg.jpg'; // make sure path is correct

<header className="masthead" style={{ backgroundImage: `url(${Background})` }} >

ref Be mindful of using backgroundImage instead of background-image

Fawaz
  • 3,404
  • 3
  • 17
  • 22
Aditya
  • 21
  • 4
0

Working for me

<div className="drag-dashboard" style={{ backgroundImage: `url(${config.DESTINATION_MEDIA_CDN+ "assets/icons_skining/upload_placeholder_skining.png"})`}}>
                                   </div>