HTML Images

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 6 Examples + 6 Try It
img / picture / alt

Introduction

Images are an integral part of web design. They enhance visual appeal and help convey information quickly. HTML provides simple, flexible methods to embed images on any page.

Understanding how to add images correctly—with proper attributes, formats, and optimization—is key to building visually rich and performance-friendly websites. This tutorial covers everything from basic <img> tags to responsive <picture> elements.

What You’ll Learn

01

img

Embed photos.

02

alt

Accessibility.

03

Formats

JPEG PNG WebP.

04

picture

Responsive.

05

lazy

Performance.

06

figure

Captions.

The <img> Element

The <img> element embeds an image on a web page. It is a void element (no closing tag) and requires src and alt attributes:

html
<img src="photo.jpg" alt="A descriptive text about the image">
  • src — the URL or path to the image file.
  • alt — a brief description for accessibility; shown if the image fails to load.

See the img tag reference for every attribute.

💡
Beginner Tip

Never skip alt. Describe what the image shows, not the filename. For decorative images only, use alt="".

Image Formats

Choosing the right format depends on the image type and how it will be used:

  • JPEG (.jpg / .jpeg) — best for photos and images with many colors.
  • PNG (.png) — ideal for transparency or lossless quality (logos, screenshots).
  • GIF (.gif) — simple animations and low-color graphics.
  • SVG (.svg) — scalable vector graphics for logos and icons; sharp at any size.
  • WebP (.webp) — modern format with superior compression and smaller file sizes.

For photos on modern sites, WebP (with JPEG fallback via picture) is a common pattern.

Setting Image Attributes

Beyond src and alt, these attributes control appearance and behavior:

width and height

Set dimensions in pixels. Including both helps prevent layout shift while the image loads:

html
<img src="photo.jpg" alt="A descriptive text" width="300" height="200">

For responsive layouts, combine HTML dimensions with CSS: max-width: 100%; height: auto;

title

Provides extra information on hover (tooltip). Do not rely on title instead of alt—screen readers use alt, not title.

html
<img src="photo.jpg" alt="Mountain view" title="View from Mount Rainier">

loading

Use loading="lazy" to defer loading off-screen images until the user scrolls near them:

html
<img src="large-photo.jpg" alt="A descriptive text" loading="lazy">

Responsive Images

Images should look good on phones, tablets, and desktops. HTML offers two main approaches:

<picture> Element

Specify different sources for different conditions such as screen width or image format. Place source and fallback img inside the element:

html
<picture>
  <source srcset="photo-small.jpg" media="(max-width: 600px)">
  <source srcset="photo-large.jpg" media="(min-width: 601px)">
  <img src="photo-default.jpg" alt="A descriptive text">
</picture>

srcset and sizes Attributes

Define multiple resolutions so the browser picks the best file for the user’s screen:

html
<img src="photo-default.jpg"
  srcset="photo-600w.jpg 600w, photo-1200w.jpg 1200w"
  sizes="(max-width: 600px) 100vw, 50vw"
  alt="A descriptive text">

Learn more in the picture tag reference.

Optimizing Images for Performance

Images often account for most of a page’s download size. Optimize them to keep pages fast:

  • Compress images — use tools like Squoosh, TinyPNG, or ImageOptim to reduce file size.
  • Use WebP — convert photos to WebP for smaller files; provide JPEG fallbacks when needed.
  • Lazy loading — add loading="lazy" to below-the-fold images.
  • Right-size assets — do not serve 4000px-wide images when 800px displays on screen.
  • Set dimensions — width and height (or aspect-ratio in CSS) prevent layout jumps.

Accessibility and Images

Make images usable for everyone, including people who use screen readers:

  • Always provide meaningful alt text. Use alt="" only for purely decorative images.
  • Use <figure> and <figcaption> for complex images that need a visible caption.
  • Avoid putting important text inside images—use real HTML text when possible.
  • Ensure sufficient color contrast if text overlays an image.
html
<figure>
  <img src="chart.jpg" alt="Sales chart for Q1 showing 25% growth">
  <figcaption>Figure 1: A sales chart showing growth in Q1.</figcaption>
</figure>

Common Pitfalls

  • Missing alt text — hurts accessibility and SEO. Every img needs an alt attribute.
  • Overusing large images — oversized files slow page load; resize and compress before upload.
  • Improper aspect ratio — mismatched width/height distorts the image. Keep proportions correct.
  • Closing tags in wrong placesource and img must be inside picture, not after it.
  • Placeholder as alt — do not copy filename or “image” as alt text; describe the content.

⚡ Quick Reference

Attribute / ElementPurpose
<img>Embed an image
srcImage URL or path
altText description (required)
width / heightDimensions in pixels
loading="lazy"Defer off-screen loading
<picture>Responsive source switching
srcsetMultiple resolution options
<figure>Image + caption group

Examples Gallery

Six examples from a basic image to a full page with responsive and lazy-loaded photos. Each includes View Output and Try It Yourself.

Example 1 — Basic Image

html
<img src="photo.jpg" alt="A scenic mountain landscape at sunrise">
Try It Yourself

How It Works

src points to the file; alt describes it for users who cannot see the image.

Example 2 — Width, Height, and Title

html
<img src="photo.jpg" alt="Green landscape photo"
  width="300" height="195" title="Hover tooltip text">
Try It Yourself

How It Works

Dimensions reserve space before the image loads. title adds an optional hover tooltip.

Example 3 — Figure and Caption

html
<figure>
  <img src="chart.jpg" alt="Bar chart showing 25% sales growth in Q1">
  <figcaption>Figure 1: Sales growth in Q1.</figcaption>
</figure>
Try It Yourself

How It Works

figure groups the image and visible caption. alt still describes the image for screen readers.

Example 4 — Lazy Loading

html
<img src="large-photo.jpg" alt="High-resolution photo"
  loading="lazy" width="500" height="320">
Try It Yourself

How It Works

The browser downloads the image only when it is about to scroll into view, saving bandwidth on long pages.

Example 5 — Responsive Picture

html
<picture>
  <source srcset="photo-small.jpg" media="(max-width: 600px)">
  <source srcset="photo-large.jpg" media="(min-width: 601px)">
  <img src="photo-default.jpg" alt="Responsive landscape photo">
</picture>
Try It Yourself

How It Works

The browser picks the first matching source. The final img is the fallback.

Example 6 — Complete Images Page

Full page combining basic img, picture, figure, and lazy loading:

html
<!DOCTYPE html>
<html lang="en">
<head>
  <title>HTML Images Example</title>
</head>
<body>
  <h1>HTML Images Example</h1>
  <h2>Basic Image</h2>
  <img src="photo.jpg" alt="A beautiful landscape" width="400">
  <h2>Figure with Caption</h2>
  <figure>
    <img src="chart.png" alt="Sales chart for Q1">
    <figcaption>Figure 1: Sales growth in Q1.</figcaption>
  </figure>
  <h2>Lazy Loading</h2>
  <img src="large.jpg" alt="High-resolution image" loading="lazy">
</body>
</html>
Try It Yourself

How It Works

Demonstrates every technique from this tutorial on one page with semantic markup and proper alt text.

Best Practices

✅ Do

  • Write descriptive alt text for every image
  • Compress and resize images before uploading
  • Use loading="lazy" for below-the-fold photos
  • Set width and height to prevent layout shift
  • Use picture or srcset for responsive layouts

❌ Don’t

  • Omit the alt attribute entirely
  • Use huge uncompressed photos for small thumbnails
  • Put essential text only inside image files
  • Rely on title instead of alt
  • Close picture or figure before child elements

Universal Browser Support

<img> works in every browser. loading="lazy", picture, and srcset are supported in all modern browsers. Test responsive images on mobile Safari and Chrome.

Baseline · Since HTML

HTML img + lazy loading

<img> works in every browser. loading="lazy", picture, and srcset are supported in all modern browsers. Test responsive images on mobile Safari and Chrome.

98% Modern browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
HTML img + lazy loading Modern browsers

Bottom line: Basic images are universal; responsive and lazy features cover virtually all users today.

Conclusion

Using images effectively in HTML is key to creating visually appealing and accessible web pages. By choosing the right formats, attributes, and optimization techniques, your images enhance both the look and performance of your site.

Next, style text around your images with HTML Text Formatting, or explore the img tag reference and HTML SVG for vector graphics.

Key Takeaways

📁 02

Formats

Pick right.

JPEG WebP
📱 03

picture

Responsive.

Mobile
04

lazy

Faster load.

Perf
📝 05

figure

Captions.

Semantic

❓ Frequently Asked Questions

The img element embeds an image on a web page. It is a void (self-closing) tag that requires src for the image URL and alt for a text description used by screen readers and when the image fails to load.
Alt text describes the image for blind users and search engines. It also appears if the image file is missing. Write concise, meaningful descriptions—not filenames like photo.jpg.
Use alt="" only for purely decorative images that add no information (background patterns, spacer graphics). The attribute must still be present—just empty.
img displays one image. picture wraps multiple source elements so the browser picks the best file for screen size or format, with img as the fallback.
It tells the browser to defer downloading the image until it is near the viewport. This speeds up initial page load when many images appear below the fold.
JPEG for photos, PNG for transparency, SVG for logos and icons, WebP for modern browsers with smaller file sizes. Provide fallbacks when using newer formats.
Did you know?

The decoding="async" attribute hints that the browser may decode the image off the main thread, which can improve page responsiveness when many images load at once. It is safe to add on most img tags.

Build an image gallery in the editor

Add basic images, responsive picture elements, and lazy loading, then preview live.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful