HTML: Image won't display?
Asked Answered
F

8

14

I'm kind of new to HTML. I'm trying to display an image on my website but for some reason, it just shows a blue box with a question mark in it. I've looked everywhere on the internet, but none of the solutions seemed to work for me. I've tried:

<img src="iwojimaflag.jpg"/>

<img src="images/iwojimaflag.jpg"/>

<img src="Applications/MAMP/htdocs/Symfony/src/Acme/WebBundle/Resources/public/images/iwojimaflag.jpg"/>
Fire answered 17/6, 2014 at 0:31 Comment(2)
Where is your image stored relative to your HTML file (ex: HTML is at "myapp/index.html" and image is at "myapp/images/myimage.jpg" ?Hepler
image: "/Resources/public/images/iwojimaflag.jpg" -------------------- html: "/Resources/views/Default/index.html.twig"Fire
A
14

Just to expand niko's answer:

You can reference any image via its URL. No matter where it is, as long as it's accesible you can use it as the src. Example:

Relative location:

<img src="images/image.png">

The image is sought relative to the document's location. If your document is at http://example.com/site/document.html, then your images folder should be on the same directory where your document.html file is.

Absolute location:

<img src="/site/images/image.png">
<img src="http://example.com/site/images/image.png">

or

<img src="http://another-example.com/images/image.png">

In this case, your image will be sought from the document site's root, so, if your document.html is at http://example.com/site/document.html, the root would be at http://example.com/ (or it's respective directory on the server's filesystem, commonly www/). The first two examples are the same, since both point to the same host, Think of the first / as an alias for your server's root. In the second case, the image is located in another host, so you'd have to specify the complete URL of the image.

Regarding /, . and ..:

The / symbol will always return the root of a filesystem or site.

The single point ./ points to the same directory where you are.

And the double point ../ will point to the upper directory, or the one that contains the actual working directory.

So you can build relative routes using them.

Examples given the route http://example.com/dir/one/two/three/ and your calling document being inside three/:

"./pictures/image.png"

or just

"pictures/image.png"

Will try to find a directory named pictures inside http://example.com/dir/one/two/three/.

"../pictures/image.png"

Will try to find a directory named pictures inside http://example.com/dir/one/two/.

"/pictures/image.png"

Will try to find a directory named pictures directly at / or example.com (which are the same), on the same level as directory.

Antimacassar answered 17/6, 2014 at 2:5 Comment(10)
Ok so I tried going to "localhost/Applications/MAMP/htdocs/Symfony/src/Acme/WebBundle/…" and I got a 404 not found error, but I double checked that the picture is there!! I also double checked that the url is right!Fire
I also tried <img src="..."/>' and replaced the ...` with what you said above but according to my directory. Didnt work.Fire
If you're specifying the host's name you should precede it by http:// or whatever protocol you use to retrieve the image. Also note that the root of your site =/= the root of your filesystem. Http://localhost may point to /var/www/ not /.Antimacassar
You see, when you start a server, it'll set a virtual root on the filesystem, and it'll be located at the folder it's configured to be the website's root. Let's say your filesystem's root is /, and your server is configured to set it's root to /var/www/, so http://localhost/ will start at /var/www/, and if you try to access http://localhost/pic/image.png you have to have your image at /var/www/pic/image.pngAntimacassar
Likewise, if your document is at http://localhost/doc/index.html or /var/www/doc/index.html on the filesystem and if your document calls for an image with src=pic/image.png your image should be at /var/www/doc/pic/image.png. Or if src=/pic/image.png then it'll have to be at /var/www/pic/image.pngAntimacassar
@Fire are you sure that your src attribute reads images/image.jpg?Antimacassar
Yup. <img src="images/iwojimaflag.jpg"/> I literally copied and pasted that. Though I checked the file type and it says it's a jpeg image. But the extension is jpg..?Fire
The extension doesn't matter, have you tried removing that trailing /? have you tried with another image (on another host, like goo.gl/AJm0rS ?)Antimacassar
Yes, I've tried using another image. I don't know what you mean by the trailing / because I don't see it. The image only shows up if I just type in the website address that I got it from.Fire
Instead of <img src="…"/> use <img src="…">, what do you mean by just type in the website address that I got it from? where do you type the address?Antimacassar
S
5

Lets look at ways to reference the image.

Back a directory

../

Folder in a directory:

 foldername/

File in a directory

 imagename.jpg

Now, lets combine them with the addresses you specified.

 /Resources/views/Default/index.html
 /Resources/public/images/iwojimaflag.jpg

The first common directory referenced from the html file is three back:

 ../../../

It is in within two folders in that:

 ../../../public/images/

And you've reached the image:

 ../../../public/images/iwojimaflag.jpg

Note: This is assuming you are accessing a page at domain.com/Resources/views/Default/index.html as you specified in your comment.

Swath answered 17/6, 2014 at 1:32 Comment(4)
so... what am I supposed to write in my code? <img src="/public/images/iwojimaflag.jpg/>"? Because that didn't work for me. I also tried <img src="../../../public/images/iwojimaflag.jpg"/> and that also didn't work.Fire
Well..... what is the address in your address bar when you are viewing index.html?Swath
You already wrote it up there. ../Resources/views/Default/index.htmlFire
So... you already have the answer then. Just look above. If it doesn't work, look in your browser debugger and address errors... or fix your website setup. Good luckSwath
O
2

img {
  width: 200px;
}
<img src="https://image.ibb.co/gmmneK/children_593313_340.jpg"/>

<img src="https://image.ibb.co/e0RLzK/entrepreneur_1340649_340.jpg"/>

<img src="https://image.ibb.co/cks4Rz/typing_849806_340.jpg"/>

please see the above code.

Oddson answered 3/10, 2018 at 7:3 Comment(0)
E
0

If you put <img src="iwojimaflag.jpg"/> in html code then place iwojimaflag.jpg and html file in same folder.

If you put <img src="images/iwojimaflag.jpg"/> then you must create "images" folder and put image iwojimaflag.jpg in that folder.

Eldoneldora answered 17/6, 2014 at 1:1 Comment(0)
P
0

Very old case but...

There is still another reason for which the browser may not find your image. In fact is a sub-case of the previous "Incorrect file paths" issue.

Check if the HTML header includes a <base href="">. If that is the case, no matter where your HTML file is, all the paths will be anchored in the root folder defined in the <base> instead. So you may have your image file in the same folder that the HTML file and not being able to load it.

Pallor answered 18/9, 2023 at 13:45 Comment(0)
S
-1

I confess to not having read the whole thread. However when I faced a similar issue I found that checking carefully the case of the file name and correcting that in the HTML reference fixed a similar issue. So local preview on Windows worked but when I published to my server (hosted Linux) I had to make sure "mugshot.jpg" was changed to "mugshot.JPG". Part of the problem is the defaults in Windows hiding full file names behind file type indications.

Stamps answered 3/11, 2015 at 2:29 Comment(0)
I
-2

Here are the most common reasons:

  • Incorrect file paths
  • File names are misspelled
  • Wrong file extension
  • Files are missing
  • The read permission has not been set for the image(s)

Note: On *nix systems, consider using the following command to add read permission for an image:

chmod o+r imagedirectoryAddress/imageName.extension

or this command to add read permission for all images:

chmod o+r imagedirectoryAddress/*.extension

If you need more information, refer to this post.

Incommensurate answered 2/8, 2017 at 16:45 Comment(0)
C
-3

I found that skipping the quotation marks "" around the file and location name displayed the image... I am doing this on MacBook....

Cleveite answered 5/10, 2016 at 23:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.