HTML Images
Table of Contents + โ
In previous lessons, youโve learned about HTML Links and Navigation. Now we will learn about how to add images to your web page.
๐ผ๏ธ The Image Element
The <img>
tag is used to insert images in your webpage. Unlike other HTML elements, img
tag is self-closing we do not need to add a closing tag like </img>
.
๐๏ธ img
tag Syntax
<img src="path/to/image.jpg" alt="Description of the image">
src
= Source - the path to your image filealt
= Alternative text - text that display if the image cannot be loaded
โ ๏ธ Always Include Alt Text
The alt
attribute is important for user with disabilities, as it provides a text instead of image to describe what the image is about. It also helps with search engines to learn about your image content since they cannot โseeโ images.
๐ Your First Image
<img src="FreecodingSchool.jpg" alt="Freecoding School is a free coding school for everyone">
this will display the image FreecodingSchool.jpg
with the alt text โFreecoding School is a free coding school for everyoneโ as below:
When images are not available, the alt text will be displayed instead. For example, if image FreecodingSchool.jpg
is not found, the text โFreecoding School is a free coding school for everyoneโ will be shown as below:

๐ Image File Paths
Providing the correct path to your image file is very important. There are two main types of paths you can use:
- Relative Paths - its path from the current file location
- Absolute URLs - its full URL from the root of the website or an external website
๐ Relative Paths (Images in Your Project)
Relative paths are used when your images are stored in the same project folder or subfolders.
We have to understand how we can go up or down the folder structure to find the correct image file. Here are some examples:
- Same Folder: If your image is in the same folder as your HTML file, you can just use the image name.
- Subfolder: If your image is in a subfolder, you need to include the subfolder name in the path.
- Parent Folder: If your image is in a parent folder, you can use
../
to go up one level in the folder structure. - Subfolder of Parent Folder: You can combine
../
with the subfolder name to access images in a subfolder of the parent folder.
๐ Relative Path Examples
<!-- Image in the same folder as your HTML file --><img src="photo.jpg" alt="My photo">
<!-- Image in a subfolder --><img src="images/photo.jpg" alt="My photo">
<!-- Image in a parent folder --><img src="../photo.jpg" alt="My photo">
<!-- Image in a subfolder of parent folder --><img src="../images/photo.jpg" alt="My photo">
๐ Absolute URLs (Images from Other Websites)
Absolute URLs are taken from the root of the website. They start with / and after that the complete path to the the image file.
Like if we have image photo.jpg
in the root of our website, we can use the following absolute URL:
- Root Image: If the image is in the root folder of your website, you can use a path starting with
/
. - Nested Folder: If the image is in a nested folder, you can specify the full path from the root.
- External Image: If the image is hosted on another website, you can use the full URL.
<!-- Image in the root of your website --><img src="/photo.jpg" alt="External photo">
<!-- Image in a nested folder --><img src="/images/folder1/photo.jpg" alt="External photo">
<!-- External image from another website --><img src="https://example.com/images/photo.jpg" alt="External photo">
๐ฏ Best Practice
Keep your images organized in a dedicated images
or assets
folder. This makes your project cleaner and easier to manage!
๐ Recommended Folder Structure
Its a good practice to organize your images in a separate folder within your project.
my-website/โโโโ index.htmlโโโ about.htmlโโโ contact.htmlโโโโ images/ โโโ logo.png โโโ hero-banner.jpg โโโ team/ โ โโโ john.jpg โ โโโ sarah.jpg โโโ products/ โโโ product1.jpg โโโ product2.jpg
๐จ img
Tag Attributes
The <img>
tag has several attributes that help you control how images are displayed in browser like:
Attribute | Purpose | Example |
src | Image file path (required) | src="images/photo.jpg" |
alt | Alternative text (required) | alt="Beautiful sunset" |
width | Image width in pixels | width="300" |
height | Image height in pixels | height="200" |
title | Tooltip text on hover | title="Sunset at the beach" |
loading | When to load the image | loading="lazy" |
๐ Controlling Image Size
We can use width
and height
attributes to control the size of the image. There are some things to keep in mind:
<!-- Set specific dimensions --><img src="photo.jpg" alt="My photo" width="300" height="200">
<!-- Set only width (height adjusts proportionally) --><img src="photo.jpg" alt="My photo" width="300">
<!-- Set only height (width adjusts proportionally) --><img src="photo.jpg" alt="My photo" height="200">
๐ก Pro Tip
When you specify only width OR height, the browser automatically scales the other dimension proportionally so that the image maintains its aspect ratio and it does not look stretched or squished.
โก Lazy Loading for Performance
Sometimes, we may want to load images once the user scrolls to them, instead of loading all images at once when the page loads. This can speed up page loading times, especially for pages with many images.
<!-- Load image only when user scrolls near it --><img src="large-image.jpg" alt="Description" loading="lazy">
๐ฏ Writing Great Alt Text
We have to remember that Alt text is very important for accessibility and SEO. Hereโs how to write it well:
โ Good Alt Text Examples:
<!-- Descriptive and specific --><img src="golden-retriever.jpg" alt="Golden retriever playing fetch in a sunny park">
<!-- For decorative images, use empty alt --><img src="decorative-border.png" alt="">
<!-- For functional images (like buttons) --><img src="search-icon.png" alt="Search">
<!-- For informational images --><img src="sales-chart.png" alt="Sales increased 25% from January to March 2024">
โ Poor Alt Text Examples:
<!-- Too vague --><img src="dog.jpg" alt="dog">
<!-- Redundant --><img src="photo.jpg" alt="Photo of a dog">
<!-- Too long --><img src="dog.jpg" alt="This is a photo I took last summer of my golden retriever named Max who loves playing fetch and swimming in the lake near our house">
๐ฏ Alt Text Guidelines
- Be descriptive but concise (125 characters or less)
- Donโt start with โImage ofโ or โPicture ofโ
- For decorative images, use empty alt:
alt=""
- For complex images (charts, graphs), describe the key information
๐ผ๏ธ Common Image Formats
There are different image formats that work best for different purposes:
Format | Best For | Pros | Cons |
JPEG (.jpg) | Photos, complex images | Small file size, good quality | No transparency, lossy compression |
PNG (.png) | Graphics, logos, transparency | Transparency support, lossless | Larger file sizes |
GIF (.gif) | Simple animations, small graphics | Animation support, small files | Limited colors (256) |
SVG (.svg) | Icons, logos, simple graphics | Scalable, small files, editable | Not good for photos |
WebP (.webp) | Modern web images | Excellent compression, quality | Not supported by older browsers |
๐ฑ Responsive Images
This is very important concept in modern web development. We should always make sure that the images that we want to display on our website looks good on all devices like for phones it should not be too big and for desktops it should not be too small.
๐ฏ Basic Responsive Image
To make an image responsive, we can set its width to 100% and height to auto in style. This will make the image only take the width of its parent container and adjust its height automatically to maintain the aspect ratio.
<img src="photo.jpg" alt="Description" style="max-width: 100%; height: auto;">
๐ผ๏ธ Different Images for Different Screen Sizes
We can use the <picture>
element to provide different images for different screen sizes. Based on the deviceโs screen width, the browser will choose the correct image and display it.
Here is an example of how we can use the <picture>
element to provide different images for different screen sizes:
<picture> <source media="(max-width: 600px)" srcset="small-image.jpg"> <source media="(max-width: 1200px)" srcset="medium-image.jpg"> <img src="large-image.jpg" alt="Responsive image"></picture>
As you can see we have used the <source>
tag along with the media
attribute to make browser display image based on the screen size. Like if the screen width is less than or equal to 600px, it will display small-image.jpg
, if the screen width is less than or equal to 1200px, it will display medium-image.jpg
, and for larger screens, it will display large-image.jpg
.
๐ฎ Try It Yourself!
We have learned a lot about images in HTML. Now itโs time we use our knowledge.
- Make a travel blog post featuring:
- Multiple photos with descriptive captions
- A short video tour
- Audio narration or local music
๐ What Youโve Mastered
Fantastic work! You now know how to:
- โ Add images with proper src and alt attributes
- โ Organize images with folder structures
- โ Create responsive images for different devices
- โ Write accessible alt text for all users
- โ Optimize images for performance
- โ Use different media formats appropriately
๐ Whatโs Next?
In our next lesson, weโll look into HTML Tables - learn how to organize and display data in rows and columns.