-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.

3
  • I'd suggest you find a html tutorial. Commented May 24, 2023 at 1: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? Commented May 24, 2023 at 2: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. Commented May 24, 2023 at 7:13

1 Answer 1

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">
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.