8

I have an image inside src/assets/images folder in my ReactJs project. How can I use it in a CSS file as background-image without use relative paths?

I added in jsconfig.json the src folder in compilerOptions.baseUrl so in js files I can do:

import logo from 'assets/images/logo.svg'
<img src={logo} />

but what about css files? I have several folders and I'd like to avoid ugly strings like

background-image: url(../../../assets/images/logo.svg)

I'm using React 16.8.6

EDIT: I used create-react-app, so I haven't webpack files exposed and the ejection shoud be avoided.

4 Answers 4

15

You can use (./) . Webpack automatically link the image for eg:-

.user-background{
    background: url("./images/all-bg-title.jpg") no-repeat center center;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -ms-background-size: cover;
    -o-background-size: cover;
     background-size: cover;
  }

Above image(all-bg-title.jpg) is at folder /src/images/all-bg-title.jpg

React Docs Link here

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I needed, thank you. Solved all of my errors. To repeat the steps: 1. Put images into an src/images folder in CRA 2. Reference the image in CSS with a relative path using "." at the beginning of the path so Webpack can find it You saved me!
2

Consider using Webpack for static assets bundle, this tutorial might help you: https://webpack.js.org/guides/asset-management/

Basically, 3 steps:

  1. First use file-loader for any extensions of images you want.

  2. import your image in the js file.

  3. Now CSS file can simply get the simplified url

Update: Since you don't want to modify the webpack config file, you might consider making your image inline. It improves performance by not creating another http request for the image(only for small size images)

5 Comments

I used create-react-app, so I haven't webpack files exposed and the ejection shoud be avoided.
Do you mean something like background-image: url("data:image/svg+xml;utf8,<svg......></svg>")?
Consider make a base64 version of it, or any format of inline style can do the trick.
So I need to put it in a variable (with Sass or var) in case I need to use it in several places. But what if my image is not small?
Http request is expensive, so unless ur pic is high resolution, doing it should be ok. But it is a bad approach for long term development since u need every base64 representation of images. The best approach in my opinion is creating ur own app so u can take advantage of webpack.
1

You can set public path / so images path resolved with respect to public so if you have images in public path then just do images/assets/asd.jph it will resolve it with public path

4 Comments

What if the image is not in public path but in src or node_modules folder? I could make a symbolic link but I don't think that this is a reactjs-oriented solution.
You can set in your webpack config how to resolve specific files
I used create-react-app, so I haven't webpack files exposed and the ejection shoud be avoided .
Then I am not sure of any other way
0

try using svg format on images, then make a react class, then create an index.js where in you will import everything in one file then import image to wherever you want

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.