Skip to content
Advertisement

Setting a backgroundImage With React Inline Styles

I’m trying to access a static image to use within an inline backgroundImage property within React. Unfortunately, I’ve run up dry on how to do this.

Generally, I thought you just did as follows:

JavaScript

This works for <img> tags. Can someone explain the difference between the two?

Example:

<img src={ Background } /> works just fine.

Thank you!

Advertisement

Answer

The curly braces inside backgroundImage property are wrong.

Probably you are using webpack along with image files loader, so Background should be already a String: backgroundImage: "url(" + Background + ")"

You can also use ES6 string templates as below to achieve the same effect:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement