4

In a project bootstrapped with Create React App, in my public folder I have a assets folder with the file logo512.jpg.

When I reference the file in a component like so

<div>
    <img src='/assets/logo512.jpg'/>
</div>

I everything works perfectly. However, when I reference the same file in a CSS file for the same component like so:

background {
    background-image: url('assets/logo512.jpg');
}

Then the error Can't resolve '/assets/logo512.jpg' in DIRECTORY_OF_COMPONENT/assets/logo512.jpg

Note that DIRECTORY_OF_COMPONENT is not the public folder but the folder the CSS file is in. How do I make it so the url references the public folder instead?

Below is the full code along with my file structure:

// HomePage.js

import React from 'react';
import 'tachyons';
import './HomePage.css';

function HomePage() {
    return (
        <div className="background tc">
            <div>
                <img src='/assets/logo512.jpg'/> //this works 
            </div>
        </div>
    );
}

export default HomePage;



// HomePage.css
.background {
    height: 100%;
    display: flex;
    align-content: center;
    justify-content: center;
    background: url('/assets/logo512.jpg'); //this does not work
}

File Structure

1
  • You may try my answer, I have mentioned in detail how to do this :) If you still have issues I would like to check you folder structure Commented Jan 9, 2021 at 7:59

3 Answers 3

3

UPDATE:

CASE 1:

If you image is in the public folder. Then the code should work provided you are using assets folder right within the public folder.

Codesandbox Demo for all the three cases : https://codesandbox.io/s/images-not-loaded-hkzq1

CASE 2:

In case you want to use images from other than the public folder, You will need to import the image in your React project first. (Make sure the Path is correct and your image).

import MyImage from "./assets/logo512.jpg"

And then use in the src attribute.

<img src={MyImage}/>

Case 3: When using CSS to set image on a block level element:

.background  {
  height:200px; /* Make sure height of the div is given for image to be within*/
  background-image: url("assets/logo512.jpg");
}

This should work properly (provided you relative path is correct with regard to the CSS file)

NOTE: With SVG images React has come up with a component based way where we use as a component by importing our SVG with ReactComponent.

import { ReactComponent as Logo } from './logo.svg';

return(<Logo/>)
Sign up to request clarification or add additional context in comments.

16 Comments

My issue is that I have case 3 (with the image in public/assets and the url in the css), but the error is still present.
My code is (I believe - unless I am missing something) identical to case 3 of your demo
Let me check is there anything missing in your code :)
One reason: You need to set the height of your div first :) Check this May be this is the issue. Have updated my answer in case 3
I've edited the question with the relevant code and file structure. I'll hold off on closing the question for now since my issue is still present :)
|
0

I had this issue myself but found the other answer too vague.

To be clear: assets in your CSS corresponds to the src/assets dir in your project.

Here's me using a static asset in my CSS:

@font-face {
  font-family: "ITC Clearface";
  src: url("assets/ClearfaceStd-Regular.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
}

But you can also diagnose why your path isn't working from the error the compiler shows, eg:

Module not found: Error: Can't resolve 'assets/somefile' in '/home/mike/Code/myapp/website/src'

See if that file exists by running (bash or powershell)

ls /home/mike/Code/myapp/website/src/assets/somefile

If that doesn't work, then the file doesn't exist - you likely have a typo in the name! In my case it turns out the font file I was using in my CSS had a minor spelling error.

Comments

0

Here is how i do it and works pretty fine: it is two ways:

  1. I import that image from public, and i do it in tsx components like this:
const myBg = './images/bg.png';
// and then use this variable where ever i want: like this: 
<div style={{backgroundImage: `url('${myBg}')`}}< /div>
//remember to set width and height on that div , to display image inside
  1. Import it directly from public to css, by hard coding the full path: like:
    .my_bg{
        background-image: url('../../../public/images/bg.png') /* this is a full path to public from your css/sass , and
     the rest of your CSS ... */

}

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.