1

I am trying to add a image in my React component but for some reason the alternative text is always displayed

This is how I am reaching for the picture in the component Navbar

<img src="../assets/images/nav_logo.png" alt="Petapilot" className="site-logo"></img>

And this is my project structure

enter image description here

Is my path wrong?

Fabio
  • 343
  • 1
  • 6
  • 17

3 Answers3

1

Depending on how webpack bundles your files, when React compiles, the images may be somewhere else.

Try this approach:

import NavLogo from '../assets/images/nav_logo.png';


<img src={NavLogo} alt="Petapilot" className="site-logo"></img>
Squiggs.
  • 4,299
  • 6
  • 49
  • 89
1

You need to import the image to your component like this

import logo from '../assets/images/nav_logo.png'

And then use it like thisin your img tag

<img src={logo} alt="Petapilot" className="site-logo"></img>
Shmili Breuer
  • 3,927
  • 2
  • 17
  • 26
1

giving relative path to the image won't work in this case, You can keep it in the public folder and give absolute path of the imags or you can import the image like this

import Img from '../assets/images/nav_logo.png';
..
....
.....
<img src={Img} alt="Petapilot" className="site-logo"/>
fahad991
  • 452
  • 3
  • 8