HTML Tutorial

HTML img Tag: src, alt, Size, Lazy Loading with Examples

TGTeam GyaniPanditNovember 18, 20265 min read

Hello friends! A web page without images feels plain and boring, right? In HTML, adding a picture is very simple. The HTML <img> tag puts an image on a web page. It is an empty element: it has no closing tag and no content inside it. Everything is controlled through attributes, the two most important being src (where the image is) and alt (a text description of it). Let’s start with a quick look at all the attributes you will use.

<img src="taj-mahal.jpg" alt="The Taj Mahal in Agra at sunrise">

Important Attributes of the img Tag

Attribute What it does
src Path or URL of the image file (required)
alt Text shown if the image can’t load, and read aloud by screen readers (always add it)
width, height Size in pixels; also lets the browser reserve space before the image loads
loading="lazy" Loads the image only when the user scrolls near it, which makes pages faster
title Tooltip shown when you hover over the image
srcset, sizes Give the browser different image sizes for different screens

src: Relative and Absolute Paths

<!-- image in the same folder as the HTML file -->
<img src="logo.png" alt="School logo">

<!-- image inside an "images" folder -->
<img src="images/logo.png" alt="School logo">

<!-- image from a full URL -->
<img src="https://example.com/photos/logo.png" alt="School logo">

If the image does not show, the path is wrong in most cases. Check the folder name, the spelling and the extension (.jpg vs .jpeg vs .png). On most web servers file names are case-sensitive, so Logo.PNG and logo.png are different files.

Why alt Text Matters

Many beginners skip the alt attribute, but it is more important than it looks. Here is why:

  • Accessibility: screen readers read the alt text to people who cannot see the image.
  • Broken images: if the image fails to load, the alt text is shown instead.
  • SEO: search engines use alt text to understand what the image shows.

Describe the image in a short, useful way: alt="Students planting trees in the school garden" is far better than alt="image1". For purely decorative images, use an empty alt (alt="") so screen readers skip them.

Setting the Size

<img src="tiger.jpg" alt="Bengal tiger resting in the grass" width="400" height="300">

Always give both width and height that match the image’s real proportions. The browser then reserves the right space, so the page does not jump while images load. To make an image shrink on small screens, add CSS:

img {
  max-width: 100%;
  height: auto;
}

Lazy Loading for Faster Pages

If your page has many images, here is a simple trick to make it load faster:

<img src="gallery-photo.jpg" alt="Annual day group photo" width="800" height="533" loading="lazy">

Images further down the page are loaded only when needed. Don’t lazy-load the main image at the very top of the page, because it should load immediately.

Making an Image a Link

<a href="https://gyanipandit.com/programming/">
  <img src="logo.png" alt="GyaniPandit Programming home">
</a>

Read more: HTML image link

Image with a Caption: figure and figcaption

<figure>
  <img src="red-fort.jpg" alt="Red Fort in Delhi" width="600" height="400">
  <figcaption>The Red Fort, Delhi</figcaption>
</figure>

Which Image Format Should You Use?

You may have noticed that images come in different formats like JPG, PNG and WebP. So which one should you pick? Here is a quick guide:

Format Best for
JPEG / JPG Photos
PNG Screenshots, images needing a transparent background
WebP / AVIF Modern formats with much smaller files; supported by current browsers
SVG Logos and icons that must stay sharp at any size
GIF Simple short animations (video formats are often smaller)

Common Mistakes

Before we finish, watch out for these common mistakes:

  • Writing a closing tag: </img> is not needed and is invalid.
  • Leaving out alt, or filling it with keywords instead of a real description.
  • Uploading a huge 5 MB photo and shrinking it with width. The user still downloads the full file. Resize and compress images before uploading.
  • Using spaces in file names. Prefer red-fort.jpg over red fort.jpg.

So friends, to sum up: the <img> tag has no closing tag, so both <img ...> and <img ... /> work. Always add an alt text, because it is used by screen readers, shown when the image fails to load, and read by search engines. And if your image is not showing, the src path is usually the culprit, so check the folder, spelling, capital letters and the file extension. Add a few images to your own page and try these attributes yourself.

Related: HTML attributes · Anchor tag · CSS images

Turn reading into skillsCertificate courses in AI, Data Science and programming.
Explore Courses