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

172
Views
react jsx inline style not rendering when populated from a variable

I added an inline style element to a div tag in a react component along with a className element. The className property renders but the style is not displaying. I am setting from an object variable. However, if I set the style element directly using double curly braces it works, just not working from a variable.

Here is an image of the object value I am using to set the style element.

enter image description here

This shows where the style element should be getting set as the component is rendering, the styleElement object has a value set at this point

enter image description here

This shows the component post rendering and the style element is not present

enter image description here

What would prevent the style element from rendering even though is it populated from an object containing CSS properties?

over 4 years ago · Santiago Trujillo
6 answers
Answer question

0

use ES6 for this.

background: `url(${imageUrl}) no-repeat`
over 4 years ago · Santiago Trujillo Report

0

Add " surrounding the imageURL

{ background: "url(\"" + imageUrl + "\") no-repeat ..."}
over 4 years ago · Santiago Trujillo Report

0

It seems like your issue lies in the !important part. React doesn't support using !important in the style object. If you can get rid of that !important part it would be for the best, but if you can't then here is a workaround (taken from this SO answer):

<div
  ref={(node) => {
    if (node) {
      node.style.setProperty('background', `url(${imageUrl}) no-repeat 50% 40%`, 'important');
    }
  }}
/>
over 4 years ago · Santiago Trujillo Report

0

I believe that !important is the main culprit here. Simply remove it and everything should render with the variable inside.

Inline CSS will, as a general rule, override any CSS in your stylesheet assuming the same level of specificity.

Have a look at MDN for more info about specificity and !important to learn more

over 4 years ago · Santiago Trujillo Report

0

the background image syntax is

background-image: url("img_tree.gif");

You are missing the " on between the file name. change to

{ background: 'url("' + imageUrl + '") no-repeat 50% 40% !important' }

the other thing you might want to check is make sure img_tree.gif is actually the correct path, usually, it should relative path or absolute path, something like ./img_tree.gif or http://example.com/img_tree.gif

also make sure have a background size

background-size: cover 
over 4 years ago · Santiago Trujillo Report

0

A rule about !important

  • Any values in an element’s style property take precedence over anything inherited by a style sheet (even if the style sheet rule uses the !important annotation). So you do not have to use !important inside inline styling

I think you might need to import the path in jsx

// require also works
background:`url(${import("./images/your_image")}) no-repeat 50% 40%`

If it does not work, try this:

 style={{
      backgroundImage: `url(${require('./images/your_image')})`,
      backgroundRepeat: 'no-repeat',
    }}
over 4 years ago · Santiago Trujillo 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!