But it works: ...">

Img not working with relative path in src

This does not work:

<img src="../assets/images/image.jpg" alt="Alternative Text"> 

But it works:

 <img src="http://localhost/abc/def/geh/assets/images/image.jpg" alt="Alternative Text"> 

In my scenario, I just can't work with an absolute path. I have to use a relative path.

+5
source share
3 answers

"../assets/images/image.jpg" -This means

  • '../' go to the same directory where I am from now
  • find the folder "assets /"
  • find the "images" folder
  • find the image.jpg file.

This relative link will only work if your page is in a subfolder of

" http: // localhost / abc / def / geh / "

If the location of your page is really

"local / ASDF / ASDF / ASDF / ASDF / index.php"

(which seems ridiculous) to get to the asset folder relatively, you have to go all the way to the root.

"../../../../a/DEH/GEH/actives/images/image.jpg;

Alternatively, you can use the base tag in your header tag to make the URL in the actual src attribute more friendly.

+4
source

I suspect that you actually did not do what I told you, here is a screenshot:

enter image description here

If the image opens in a new tab, then you have some kind of error or extension that will ruin it in html. If you mess up the relative path, you are likely to get 404, but you can see the path as absolute. It may look like http://localhost/asdf/asdf/asdf/asdf/assets/assets/image.jpg In any case, send a screenshot of the above operation.

+1
source
 <img src="assets/images/image.jpg" alt="Alternative Text"> 

must work. You should not have put "../" at the beginning of the image path.

To better understand relative paths versus absolute paths, refer to this link.

0
source

All Articles