Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Setting inline backgroundImage is not working in React

I am using a function which will return the full path of the image.

  const imagePath = (imagePath) => {
    const domainPath = "http://localhost:9005/gallery/original/";
    const fullImagePath = domainPath + imagePath;
    return fullImagePath;
  };

Now If I use below code, it will work and show image on browser:

<img
 src={imagePath(item.mediaPath)}
 alt="cow images"
/>

But If I use below code, it will NOT work:

<div
  className="bg-image-blur"
  style={{
    backgroundImage:
      "url(" +
      imagePath(item.mediaPath) +
      ")",
  }}
></div>

Please see given below image from my inspect element. Style is added to the element :
enter image description here

and It will also not work if I don't use imagePath function :

<div
  className="bg-image-blur"
  style={{
    backgroundImage:
      "url(" +
      "http://localhost:9005/gallery/original/" +
      id.mediaPath +
      ")",
  }}
></div>

What is the issue ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It does not work because you haven't put '' for url. And I'd suggest you use template literals for your string concat instead for easier look

<div
  className="bg-image-blur"
  style={{
    backgroundImage: `url('${imagePath(item.mediaPath)}')`,
  }}
></div>

You can check this document to understand how background-image looks like with url

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!