'Reference file in public folder from CSS in create-react-app

I am using creat-react-app (CRA) and simply want to include a png file placed in the public folder via CSS (I keep all image files there). Now I am trying to reference this image via CSS (I only added the background-image line to a freshly generated CRA app):

.App-header {
  background-color: #222;
  height: 150px;
  padding: 20px;
  color: white;
  background-image: url("../public/example.png");
}

You attempted to import example.png which falls outside of the project src/ directory

How do I reference the image file from within the CSS-file without copying somewhere inside /src? I also don't want to eject the application and get rid of the error message.

Edit: This question is different from The create-react-app imports restriction outside of src directory because it does not show how to solve this problem at a CSS level. The proposed solution is to add the style inside the JavaScript file which I don't want to do.



Solution 1:[1]

Just use a / before the name, this will make it relative to the output root, which includes anything in the public folder (provided the finished hosted application is served at the root of a domain).

so for the question asked above:

.App-header {
  background-color: #222;
  height: 150px;
  padding: 20px;
  color: white;
  background-image: url("/example.png");
}

the critical part being

/example.png 

refers to a file, example.png, that is in the public folder (served at the root level)

Could also be relative:

one could also use

./example.png

provided that the css file was also imported from the public/build directory, this would be relative to the css file and not depend on being served at the domain root, but typically in CRA webpack will bundle the CSS and it may or may not be loaded from this location. (you could import it in the html file directly using rel tag with the %PUBLIC_URL%/Styles.css macro)

Solution 2:[2]

In my case, to access the images from css/scss, had to move the images directory as well as fonts, to the src directory. After which i was able to refer them in the css/scss files directly,

 background-image: url("/images/background.jpg");

references:

https://github.com/facebook/create-react-app/issues/829

https://create-react-app.dev/docs/using-the-public-folder/

Solution 3:[3]

there is a special page about the /public folder in the official documentation:
https://create-react-app.dev/docs/using-the-public-folder/

Here is how you should refer to the public folder:

<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />

or

render() {
  return <img src={process.env.PUBLIC_URL + '/img/logo.png'} />;
}

BUT, you can't use those solution in a css file. So you'll have to precise the ressource from the js files:

const MyComp = () => {
  return <div style={{ backgroundImage: `${process.env.PUBLIC_URL}/img/logo.png` }} />;
}

Solution 4:[4]

I was using "public" folder but it says here to use images inside "src" folder:

https://create-react-app.dev/docs/using-the-public-folder/

"Normally we recommend importing stylesheets, images, and fonts from JavaScript."

Solution 5:[5]

Remove the extra .. in the relative path. :)

Solution 6:[6]

Use "./image.png".

The logic behind is that the css file you will place the code in, will be part of index.html when the app is finished, built and deployed.

Solution 7:[7]

The only way that helped me is adding full URL-adress to the image path. So this one can help:

background-image: url("http://localhost:3000/background.jpg"); 

Solution 8:[8]

background-image: url("http:/images/pexels-photo-1.jpg");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sasi Kumar M
Solution 3 Joseph Merdrignac
Solution 4 atazmin
Solution 5 Faizuddin Mohammed
Solution 6 J2R
Solution 7 user2408962
Solution 8 vikas kumar