-1

Before I begin, I have received so much help from this forum over the years, so thanks!

Trying to add an image to an HTML file. I'm working on HTML and J2EE. Total newcomer to web programming.

I started putting the entire path for the .jpg file (macintosh hd/Users/myDir/Desktop.image.jpg) into the HTML file.

When that failed, I began peeling off path elements. Replacing "macintosh hd," the above became "../Users/myDir/Desktop.image.jpg" then, "./Users/myDir/Desktop.image.jpg", then "/Users/myDir/Desktop.image.jpg" and, finally, "Users/myDir/Desktop.image.jpg".

When all those failed, I moved on to replacing "Users" with "../myDir/Desktop.image.jpg", "./myDir/Desktop.image.jpg", and so on until all I was left with was "image.jpg".

Failing at all the above, I made a local copy of the image in the same dir as my HTMl file (webapp). Starting at "image.jpg" I reversed my process above, adding and running, until I was back to macintosh hd/Users.../webapp/image.jpg"

<img class="fit-picture" src="macintosh hd/Users/myDir/Desktop/My Library/workspaces/EclipseEE-Workspaces/TestDir /TestProject/src/main/webapp/image.jpg" alt="should be seeing my image">

The alt statement is showing up just fine, but I'd really rather the image make an appearance. I'd have thought, at some point, the path would match requirements by persistence and/or dumb luck.

Clearly I'm confuzzled by something. I welcome and and all suggestions. Thanks.

Shawn
  • 1
  • I'd suggest you find a html tutorial. – Robert May 24 '23 at 01:14
  • Is the file you're accessing on your computer or is it on the server or whatever? If it is on your computer, is the HTML file on it too? – Major_Flux- May 24 '23 at 02:05
  • This question seems to get asked on SO every hour on the hour. The answer is always the same. Your pathname or image location will be wrong. – Rob May 24 '23 at 07:13

1 Answers1

2

You should consider the path relative to the file on which you are looking for the picture in the public repository from which you are serving your html page.

Let's take this arborescence

src/
 - index.html
 assets/
   - image1.jpg

Then path to your image will be ./assets/image1.jpg

<img class="fit-picture" src="./assets/image1.jpg" alt="should be seeing my image">

In another case

src/
 - image1.jpg
 anyfolder/
   - index.html

Then path to your image will be ../image1.jpg

<img class="fit-picture" src="../image1.jpg" alt="should be seeing my image">

And in the same dir

src/
 - image1.jpg
 - index.html

Then path to your image will be ./image1.jpg

<img class="fit-picture" src="./image1.jpg" alt="should be seeing my image">
Porzio Jonathan
  • 536
  • 2
  • 13
  • Thanks Porzio Jonathan. The problem is I repeatedly tried that syntax for the file path every step of the way from just the jpg file all the way to the full path. If this were the problem, I would have, at some point hit upon the correct relative path. It never showed up. – Shawn May 25 '23 at 00:52