31

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.

Hedge
  • 16,142
  • 42
  • 141
  • 246
  • 1
    Possible duplicate of [The create-react-app imports restriction outside of src directory](https://stackoverflow.com/questions/44114436/the-create-react-app-imports-restriction-outside-of-src-directory) – Will Ru Feb 17 '18 at 13:26
  • 1
    I think there's one extra `..` in the `url()`. I dunno how many times I've made the same mistake. – Faizuddin Mohammed Feb 17 '18 at 13:32
  • @FaizuddinMohammed This was the solution to my problem! – Hedge Feb 17 '18 at 13:55
  • Does this answer your question? [React path to public folder in css background image](https://stackoverflow.com/questions/57111197/react-path-to-public-folder-in-css-background-image) – user7610 Jan 16 '21 at 06:01

9 Answers9

28

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)

DiamondDrake
  • 974
  • 8
  • 24
  • 1
    This won't do if your PUBLIC_URL is different than '/' – rickerp Feb 20 '21 at 19:18
  • 1
    @rickerp relative urls depend on if the css is imported via webpack of if the css itself is loaded externally such as from the public folder. `url()` is a css function and the browser interprets the result based on the browsers understanding of the location of the css 'file'. Here leading it with a '/' makes it 'root relative' in most contexts (which is why it works in most situations regardless of how its imported, as long as the app is served from a root domain. you're correct that its possible that it won't work nested in a subdirectory. – DiamondDrake Mar 07 '21 at 04:53
3

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/

Sasi Kumar M
  • 2,440
  • 1
  • 23
  • 23
  • 7
    Thanks you for your answer. I can see that the images are actually being found by my CSS file in CRA now (before I had my images folder in /public) and nothing was working. However my application still does not compile and I get the following error: `./src/App.css (./node_modules/react-scripts/node_modules/css-loader/dist/cjs.js??ref--5-oneOf-4-1!./node_modules/postcss-loader/src??postcss!./src/App.css) Error: Can't resolve '/images/img-2.jpg' in '/Users/jameshubert/Documents/Programming/100DaysOfReact/day-20-briandesign-react-website/src'` – James Hubert Dec 15 '20 at 15:35
1

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` }} />;
}
Joseph Merdrignac
  • 3,510
  • 2
  • 19
  • 16
1

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."

atazmin
  • 4,757
  • 1
  • 32
  • 23
0

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

Faizuddin Mohammed
  • 4,118
  • 5
  • 27
  • 51
0

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.

J2R
  • 148
  • 2
  • 11
0

Using the URL directly doesn't work for my environment, for me I needed to specify the path TO the image FROM the current directory.

eg;

/src/styles/style.css
cursor: url("../../public/images/image.png"), auto;


/src/public/images/image.png
-2

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"); 
-3

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

vikas kumar
  • 53
  • 1
  • 4