HTML Images And File Paths

HTML Images are used to display pictures using <img> tag. File paths define where the image is located (relative or absolute).

5 views
5 min read
Try It Yourself

Experiment with the code in an interactive editor

What are Images?

The <img> tag adds pictures to your webpage.

Basic Image:

<img src="photo.jpg" alt="Description of photo">

Two important attributes:

Attribute

What it does

Why it's important

src

Source (where the image is located)

Without this, image won't load

alt

Alternative text (shown if image fails)

Blind users hear this. Google uses this for SEO

ACCESSIBILITY TIP: Always add alt attribute. Blind people using screen readers hear this text. Google also uses it to understand your image!

File Paths (How to find your images):

Simple words: File path is the address of your image file, just like your home address.

Path Type

Example

What it means

Same folder

cat.jpg

Image is in the SAME folder as your HTML file

Sub-folder

images/cat.jpg

Go into "images" folder first

Parent folder

../cat.jpg

Go ONE folder UP (.. means parent)

Root path

/images/cat.jpg

Start from the main folder (/)

Full URL

https://site.com/cat.jpg

Image on another website

Visual example of folder structure:

1your-project/
2│── index.html
3│── cat.jpg          ← Same folder: "cat.jpg"
4│── images/
5│   └── cat.jpg      ← Sub-folder: "images/cat.jpg"
6│── parent-folder/
7│   └── your-file.html  ← From here, "../cat.jpg" goes up one level

Examples:

1<!-- Image in same folder -->
2<img src="profile.jpg" alt="My profile picture">
3
4<!-- Image in "images" folder -->
5<img src="images/banner.jpg" alt="Website banner">
6
7<!-- Image one folder up -->
8<img src="../logo.png" alt="Company logo">
9
10<!-- Image from another website -->
11<img src="https://placekitten.com/200/300" alt="Cute kitten">

Responsive Images (for different screen sizes):

<img src="default.jpg" srcset="small.jpg 480w, medium.jpg 768w, large.jpg 1200w" sizes="(max-width: 600px) 480px, (max-width: 1200px) 768px, 1200px" alt="Fully responsive image" loading="lazy">

What this does: Mobile phones get small image, desktops get large image. Faster loading!