Jomatter

How to Add an Image From a Folder in HTML

To add an image from a folder in HTML, use the <img> element and place the path to the image file inside the src attribute. The path can point to an image stored in the same folder as your HTML file, a subfolder, or a parent folder.

The basic syntax is:

<img src="images/photo.jpg" alt="Description">

Here, images/photo.jpg tells the browser to look for a folder named images and then load the file named photo.jpg from that folder.

Add an Image From a Subfolder

The most common situation is having your HTML file and an image folder inside the same website directory.

For example:

website/
├── index.html
└── images/
    └── photo.jpg

Because the images folder is inside the same directory as index.html, you can reference the image with:

<img src="images/photo.jpg" alt="Example photo">

The browser starts from the location of index.html, enters the images folder, and loads photo.jpg.

You can also write the same path with ./:

<img src="./images/photo.jpg" alt="Example photo">

The ./ means the current directory. Both forms work in this example.

Add an Image When the HTML File Is Inside a Folder

The important rule is that the image path is normally calculated relative to the HTML document containing the <img> element.

Consider this structure:

website/
├── index.html
├── images/
│   └── logo.png
└── pages/
    └── about.html

From index.html, the image path is:

<img src="images/logo.png" alt="Company logo">

However, about.html is inside the pages folder. To reach the images folder, you must move up one directory first:

<img src="../images/logo.png" alt="Company logo">

The ../ means “go up one folder.”

So the path logic is:

about.html
   ↓
../
   ↓
website/
   ↓
images/
   ↓
logo.png

This relative-path relationship is one of the most important concepts when adding images from folders in HTML.

Using Multiple Folder Levels

You may need to move up more than one directory.

For example:

website/
├── images/
│   └── banner.jpg
└── pages/
    └── blog/
        └── article.html

The article.html file is two folders below the main website directory. To reach banner.jpg, use:

<img src="../../images/banner.jpg" alt="Website banner">

The first ../ moves from blog to pages, and the second moves from pages to the website root.

The Image File Extension Must Match

The filename in the HTML path must match the actual image file. For example, if the folder contains:

photo.jpg

use:

<img src="images/photo.jpg" alt="Photo">

Do not reference it as:

<img src="images/photo.png" alt="Photo">

unless the actual file is a PNG.

Common image extensions include .jpg, .jpeg, .png, .gif, .webp, and .svg.

On many web servers, filenames are case-sensitive. Therefore:

photo.jpg

and:

Photo.jpg

may be treated as different files. Use the exact filename and capitalization used by the image.

Using an Absolute Website Path

You can also reference an image using a root-relative path when the image is stored within the website’s public root.

For example:

website/
├── index.html
└── images/
    └── logo.png

You can write:

<img src="/images/logo.png" alt="Company logo">

The / at the beginning means the path starts from the website root rather than from the folder containing the current HTML file.

This is useful when different pages are located in different directories but all need to reference the same image folder.

Do Not Use a Windows File System Path

A common mistake is copying a computer file path such as:

<img src="C:\Users\John\Pictures\photo.jpg" alt="Photo">

That is not the correct way to reference an image for a normal website. HTML image paths should use web paths, such as:

<img src="images/photo.jpg" alt="Photo">

The image should be located within the website’s files and uploaded to the server along with the HTML page.

For local testing, opening an HTML document directly from your computer may allow certain local file references to work, but those paths should not be used for a published website.

Add Alt Text to the Image

The alt attribute provides a text alternative for the image:

<img src="images/product.jpg" alt="Blue running shoes">

Use an accurate description when the image conveys meaningful information. For decorative images that do not add informational content, an empty alt attribute is generally appropriate:

<img src="images/divider.png" alt="">

The alt attribute should describe the image’s purpose rather than simply repeat a filename.

Add Width and Height When Appropriate

You can also specify dimensions:

<img src="images/photo.jpg" alt="Example photo" width="800" height="500">

The width and height attributes can help the browser reserve space for the image while the page loads. CSS is often used when you need more flexible responsive sizing.

For example:

<img src="images/photo.jpg" alt="Example photo" class="responsive-image">
.responsive-image {
    max-width: 100%;
    height: auto;
}

This keeps the image from overflowing its containing element on smaller screens.

If the Image Is Not Showing

If the image appears as a broken image, check the path first.

For this HTML:

<img src="images/photo.jpg" alt="Photo">

verify that the structure is actually:

index.html
images/
└── photo.jpg

Then check the spelling, capitalization, file extension, and folder name. Also verify that you have calculated the path relative to the HTML file, especially when the HTML file is inside another directory.

For example, if your structure is:

index.html
pages/
└── contact.html
images/
└── logo.png

then this is correct inside contact.html:

<img src="../images/logo.png" alt="Company logo">

while this is incorrect:

<img src="images/logo.png" alt="Company logo">

because the browser would look for images inside the pages folder.

Final Example

A complete example looks like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Example</title>
</head>
<body>

    <h1>My Image</h1>

    <img src="images/photo.jpg"
         alt="A landscape photograph"
         width="800"
         height="500">

</body>
</html>

With this folder structure:

website/
├── index.html
└── images/
    └── photo.jpg

the browser will correctly load the image from the images folder.

The key rule is simple: put the image path in the <img> element’s src attribute, and make sure that path is correct relative to the HTML file. Use images/photo.jpg for a subfolder, ../images/photo.jpg to move up one directory, and /images/photo.jpg for a root-relative website path.

Relevant Guide

How To Get Html Code From A Website
How To Hide Text In Html
What Are Tags In Websites
What Does Hr Stand For In Html
What Is The Difference Between Css And Html
How To Add An Image From A Folder In Html
How To Add Expires Headers In Html Catswhocode
How To Copy Html Code From A Website
How To Create A Banner In Html
How To Insert A Horizontal Line In Html
What Is The Correct Html For Adding A Background Colour
How To Convert Html To Powerpoint