Mastering HTML Images: The <img> Tag

Bring your pages to life by learning how to properly add, describe, and size images for a faster, more accessible web.

Lesson ProgressStep 1 of 9
🖼️A placeholder
🖼️❌

A cute cat.

Text...

More text...

0 EXP

Ready to bring visuals to your website? Let's learn about the <img> tag, which we use to display images.

Embedding with <img>

The <img> tag is the fundamental way to embed an image on a web page. Unlike tags like `<p>` or `<h1>`, it is aself-closing (or "empty") element. This means it doesn't need a closing `</img>` tag.

It doesn't contain any content itself; instead, it uses attributes to tell the browser what to show and how to show it.


<img ... >


<img ... ></img>

System Check

Does the <img> tag require a closing </img> tag?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🖼️
Image Embedder

Successfully embed an image with <b>src</b> and <b>alt</b> attributes.

🗣️
Accessibility Advocate

Correctly provide meaningful <b>alt</b> text for an image.

Performance Tuner

Define <b>width</b> and <b>height</b> to prevent layout shift.

Mission: Embed the Logo

Write a single HTML tag to display the image located at `logo.png`. It must have alternative text of "Site Logo" and a width of "150".

A.D.A. Feedback:

> Awaiting input...

Challenge: Complete the Tag

Fill in the missing parts to create a valid, accessible image tag.

<="avatar.png" ="User avatar">

Challenge: Structure the Figure

Drag the elements into the correct order to semantically group an image with its caption.

<figcaption>A photo of the Earth from space.</figcaption>
<figure>
</figure>
<img src="earth.jpg" alt="The Earth">

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "Embed the Logo" project for feedback from other Net-Runners.

Beyond the Basics: Mastering HTML Images for a Modern Web

You've learned the <img> tag with its three core attributes:src, alt, and width/height. This is the essential foundation. However, the modern web demands more. We need images that are **accessible**, **performant**, and **responsive**. Let's explore the advanced techniques you'll use every day.

1. The Art of alt Text: Accessibility Deep Dive

The alt attribute is the single most important tool for web accessibility. But how to write *good* alt text is a skill.

  • Be descriptive, but concise. Don't start with "An image of..." or "A picture of...". Screen readers announce it's an image. Get straight to the point.
    • Bad: `alt="A dog"`
    • Good: `alt="A golden retriever catching a red frisbee."`
  • When to use an empty alt attribute (`alt=""`). If an image is purely decorative—like a background texture, a dividing line, or an icon next to text that says the same thing—use an emptyalt attribute. This tells screen readers to ignore the image, reducing noise for the user.

2. Performance: Lazy Loading and Async Decoding

Why force a user to download an image at the bottom of a long page if they never scroll that far?

  • Native Lazy Loading: By adding `loading="lazy"`, you tell the browser to wait until the image is about to scroll into view before downloading it. This is a massive performance win.
  • Asynchronous Decoding: Add `decoding="async"` to hint to the browser that it can decode the image in the background, preventing it from blocking other content from rendering.
<img src="heavy-image.jpg" alt="..." width="800" height="600" loading="lazy" decoding="async">

3. Responsive Images: `srcset` and `sizes`

A 1200px wide image is great for a desktop, but it's wasteful and slow on a 360px wide phone. Responsive images solve this.

  • `srcset` (Source Set): This attribute provides a comma-separated list of image files and their widths (e.g., `image-small.jpg 400w`, `image-large.jpg 800w`). The browser then picks the best one based on the user's screen resolution and connection speed.
  • `sizes` (Sizes): This attribute tells the browser how wide the image will *actually be* in your layout at different breakpoints (e.g., `(max-width: 600px) 100vw, 50vw`). This helps the browser pick the right `srcset` image even before the CSS loads.
<img src="fallback.jpg" 
     alt="A responsive image" 
     width="800" height="600" 
     srcset="img-400w.jpg 400w, img-800w.jpg 800w, img-1200w.jpg 1200w" 
     sizes="(max-width: 600px) 100vw, (max-width: 900px) 50vw, 800px">

4. Art Direction with the <picture> Element

Sometimes you don't just want a *smaller* image, you want a *different* image. On a wide screen, you might show a whole landscape, but on a phone, you want to show a cropped, zoomed-in version of the main subject. This is called "art direction," and the <picture> tag is built for it.

It's also the best way to serve modern formats like **WebP** or **AVIF** with a **JPEG/PNG** fallback for older browsers.

<picture>
  <source srcset="image.avif" type="image/avif">
  <source srcset="image.webp" type="image/webp">
  <source srcset="image-wide.jpg" media="(min-width: 800px)">
  <source srcset="image-tall.jpg" media="(max-width: 400px)">
  <img src="fallback.jpg" alt="..." width="800" height="600">
</picture>
Key Takeaway: A modern HTML image tag is more than just <img src>. It's a powerful tool for performance and accessibility. Always use alt,width, and height. Always consider loading="lazy",srcset, and the <picture> element to deliver the best possible experience to every user.

HTML Image Glossary

<img>
The self-closing HTML tag used to embed an image onto a web page.
src
The **Source** attribute. It is mandatory and specifies the path (URL) to the image file.
alt
The **Alternative Text** attribute. It provides a textual description of the image for screen readers and search engines, and is displayed if the image fails to load.
width / height
Attributes that define the image's dimensions in pixels. They are critical for performance, as they allow the browser to reserve space and prevent **Cumulative Layout Shift (CLS)**.
`srcset`
An attribute that provides a set of image sources for the browser to choose from, based on the user's screen resolution and density. Used for responsive images.
`sizes`
Used with `srcset`, this attribute tells the browser how much space the image will occupy in the layout at different breakpoints.
`loading="lazy"`
A performance attribute that tells the browser to defer loading the image until it is close to the user's viewport.
`<picture>`
An element used to wrap multiple `<source>` elements and one<img> tag. It allows for "art direction" (serving different-cropped images) and serving modern formats with fallbacks.
`<figure>`
A semantic tag used to wrap a self-contained piece of content, such as an image, diagram, or chart, that is referenced in the main text.
`<figcaption>`
Used inside a `<figure>` element to provide a semantic caption for it.
`WebP` / `AVIF`
Modern image formats that provide superior compression and quality compared to older formats like JPEG and PNG.
`SVG`
**Scalable Vector Graphics.** An XML-based vector image format. It's ideal for logos and icons as it scales perfectly to any size without losing quality.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching HTML and building robust, accessible, and performant web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest HTML5 specifications and WAI-ARIA accessibility guidelines.

External Resources

Found an error or have a suggestion? Contact us!