skip to Main Content

I’m having trouble adding a background image to a div. In regular JavaScript, it works and allows me to define an object with a specific size and color. However, the same approach isn’t working in React. I’ve tried different methods, including adding the color in both the CSS file and the JSX file, but nothing seems to work.

I’ve tried the following methods, but they only work when I add text between the elements. I need them to work without text. Why is this happening?

import DiskImg from "../img/favicon.png";

<div 
      className="muplayer-songcover" 
      style={{ 
        backgroundImage: `url(${DiskImg})`, 
        backgroundSize: 'cover', 
        backgroundPosition: 'center',
      }}
    >
    </div>
.muplayer-songcover {
  background-image: url(../img/favicon.png);
  background-size: cover;
  width: 20rem;
  height: 20rem;
}

2

Answers


  1. import css file from jsx file
    import "./file.css"

    Login or Signup to reply.
  2. It is because you are using a div tag, no matter what you do, give div a colour or whatever, unless it contains some content, it won’t show up. Try some other tag, like with tag.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search