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

199
Views
React div background in react not rendering

I have a prop being injected in my component called headerImage. I know it's there because when I place it in the HTML of the component like so:

<h1>Hello, {headerImage}</h1>

It renders correctl URI of the image in the above snippet.

However when I place it in my div background style, it won't render for whatever reason:

<div
                className="hide-on-mobile center-image"
                style={{
                  backgroundImage: `url({headerImage})`,
                  backgroundSize: "cover",
                  backgroundRepeat: "no-repeat",
                  backgroundPosition: "right",
                  width: "100%",
                  height: "auto",
                }}
              ></div>

I see it in the developer tools not being properly evaluated:

enter image description here

What am I doing wrong here?

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

0

I think you may just be missing a $ evaluation before you try and pass through ${headerImage}.

<div className="hide-on-mobile center-image"
  style={{
    backgroundImage: `url(${headerImage})`,
    backgroundSize: "cover",
    backgroundRepeat: "no-repeat",
    backgroundPosition: "right",
    width: "100%",
    height: "auto",
  }}
></div>

about 4 years ago · Juan Pablo Isaza Report

0

So, to nest variables within an attribute, you must quote the whole attribute, then inject an interpreted variable in to that using the ${} notation.

This is the same technique that you would use if you need to inject more than one class names. Since that is an simpler example, let me share that first, which may make more sense due to the simplicity and that it's using two variables instead of just one, then I'll expand it to your attribute.

So to start off with, I have a css source tied to my component. I'm including this so you know where "styles" is defined. import styles from './PageHeader.module.css';

Then within my component, where I use the two class names from "styles":

<span className={`${styles.left} ${styles.headerlink}`}>

Notice className={} has the back-tick quote within the {}.

So then to extend to your attribute:

  className="hide-on-mobile center-image"
      style={`
          backgroundImage: "url(${headerImage})",
          backgroundSize: "cover",
          backgroundRepeat: "no-repeat",
          backgroundPosition: "right",
          width: "100%",
          height: "auto"
       `}

Hopefully that helps.

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!