33

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.

4

11 Answers 11

31

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)

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

3 Comments

This won't do if your PUBLIC_URL is different than '/'
@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.
i have the same setup, still this doesnt work
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/

1 Comment

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'
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` }} />;
}

Comments

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

Comments

1

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

1 Comment

This worked for me from a css module to access the public folder.
0

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

5 Comments

There was two more dots when I answered. They were removed from the question for some reason.
Correct. One ..
Well that will give a build error like in the linked duplicate.
It didn't for the OP 🤷🏻‍♂️
Why is this an answer? Removing the dots does nothing to fix the error message.
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.

Comments

0

body{
  overflow-x: hidden;
  background-image: url('/public/imagenes/fondo.jpeg');
  background-size: cover;
}

1 Comment

needs formatting
0

background-image: url('/public/images/banner.jpeg');

this works for me. No need to move it inside ./src

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
-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"); 

1 Comment

This can't go to prod is it...
-3

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

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.