I found the following question on StackOverflow:
How do I get my image to appear on my webpage?
I improved the question into: how do I make images appear on localhost? I do that because I was having that problem, and I could not explain the issue. Also, if that person asks the question in the context of an online website, the problem will be very easy to solve. This article answers both scenarios.
Understanding your root problem
In a computer, developers can activate localhost anywhere within their file system. They can start their localhost from the Desktop
folder if they are on Windows.
We use the following file structure for the working directory:
my-project/
├─ images/
│ ├─ panda.png
├─ index.html
As an example, I can activate a localhost with the following steps:
- I start a localhost from
Desktop
. - I open the
my-project
folder, which results inhttp://localhost:5000/Desktop/my-project
.
The problem with this approach is that if the src
attribute for the <img>
element is /images/panda.png
, the image will not show up. The full file path is http://localhost:5000/images/panda.png
because the root folder is the Desktop
folder. If you move the images
folder to the Desktop
folder, the image will show up.
When you refer to a file with a relative file path, the root can be anything. In this case, the root folder is the Desktop
folder. The root means the /
or the http://localhost:5000/
. This means if you activate localhost at the Desktop
folder, the http://localhost:5000/
refers to Desktop
as the root folder.
To fix the problem, you can activate the localhost inside the my-project
folder, or you can change the file path to ./images/panda.png
.
Getting to know other potential issues
If the problem persists, consider the following:
- Check the file path. Make sure there is no typo.
- Make sure the file exists—no problem, my friend.
- Probably the program that runs your localhost has a bug.
- Your browser extension may disable images.
- Your browser can be the problem.
- Add more details to your question so that we can help you.
- Remember that other people can not read your mind and do not know your exact environment.