Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
Images not loading when relative path is used in React

I was importing the images previously like this in React:

import person from '../images/image1.png'

And then using them like this:

<img src={person} alt="" />

Now I want to specify the path in the src itself due to some reason, like this:

<img src="../images/image1.png" alt="" />

But it's not working even though it should.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

There are two ways to add images in your JSX with Create React App. First one is to use import like you did, to import an image that is somewhere in src folder, like so:

import person from '../images/image1.png'
<img src={person} alt="" />

Second one is to give directly an image's path to <img>'s src attribute, but it's always relative to public folder, in a way that the path should starts with /, and / means public folder.

See Adding Images, Fonts, and Files and Using the Public Folder on Create React App's documentation.

For your second attempt to work, you could put images folder inside public folder, and get your images like so:

<img src="/images/image1.png" alt="" />

If your React app will be in a sub folder when deployed, for the second way to work, you should add in your package.json this "homepage":"www.example.com/folder", and use process.env.PUBLIC_URL as below.

 <img src={process.env.PUBLIC_URL + "/images/image1.png"} alt="" />
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda