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
}
