Answer the question
In order to leave comments, you need to log in
How to access a static image in the public folder from a css file of a React component?
There is a Header component
import React from "react";
import './Header.css';
export default class Header extends React.Component{
render() {
return (
<header className="header" id="header">
<div className="container">
<div className="row align-items-center">
<div className="col-2 offset-1 col-sm-2 offset-sm-1 col-md-4 offset-md-0 ">
<div className="header-logo"></div>
</div>
<div className="col-9 col-sm-9 col-md-8">
<div className="header-contacts">
<div className="header-contacts__phones">
<a href="tel:+380981234567" className="header-contacts__phone">38 (098)123-45-67</a>
<a href="tel:+380971234567" className="header-contacts__phone">38 (097)123-45-67</a>
</div>
<a className="button header-contacts__button button__cta" href="#popup">
<img src="/img/header/header-btn-icon.png" alt="phone-icon-btn"/>
<span className="hidden-mobile hidden-sm">Заказать звонок</span>
</a>
</div>
</div>
</div>
</div>
</header>
);
}
}
.header-logo{
width: 191px;
height: 61px;
background: url('/img/header/header-logo.png') 0 center; // ошибка !!!
background-size: cover;
}
./src/components/Header/Header.css (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-4-1!./node_modules/postcss-loader/src??postcss !./src/components/Header/Header.css)
Error: Can't resolve '/img/header/header-logo.png' in
Answer the question
In order to leave comments, you need to log in
Your path is not correct to the karinka in styles.
Perhaps the webpack settings are not configured, or env.
You can write the absolute path to the image, through ../../, etc.
Or create an .env file with the parent path. "NODE_PATH=src/"
Since you are using webpack, you need to import the image as a module, and then use
import React from 'react'
import Logo from 'путь_к_картинке'
......
<img src={Logo} />
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question