Example 1 — Basic Image
<img src="photo.jpg" alt="A scenic mountain landscape at sunrise"> How It Works
src points to the file; alt describes it for users who cannot see the image.

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.
Embed photos.
Accessibility.
JPEG PNG WebP.
Responsive.
Performance.
Captions.
<img> ElementThe <img> element embeds an image on a web page. It is a void element (no closing tag) and requires src and alt attributes:
<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.
Never skip alt. Describe what the image shows, not the filename. For decorative images only, use alt="".
Choosing the right format depends on the image type and how it will be used:
For photos on modern sites, WebP (with JPEG fallback via picture) is a common pattern.
Beyond src and alt, these attributes control appearance and behavior:
Set dimensions in pixels. Including both helps prevent layout shift while the image loads:
<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;
Provides extra information on hover (tooltip). Do not rely on title instead of alt—screen readers use alt, not title.
<img src="photo.jpg" alt="Mountain view" title="View from Mount Rainier"> Use loading="lazy" to defer loading off-screen images until the user scrolls near them:
<img src="large-photo.jpg" alt="A descriptive text" loading="lazy"> Images should look good on phones, tablets, and desktops. HTML offers two main approaches:
<picture> ElementSpecify different sources for different conditions such as screen width or image format. Place source and fallback img inside the element:
<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> Define multiple resolutions so the browser picks the best file for the user’s screen:
<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.
Images often account for most of a page’s download size. Optimize them to keep pages fast:
loading="lazy" to below-the-fold images.Make images usable for everyone, including people who use screen readers:
alt text. Use alt="" only for purely decorative images.<figure> and <figcaption> for complex images that need a visible caption.<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> img needs an alt attribute.source and img must be inside picture, not after it.| Attribute / Element | Purpose |
|---|---|
<img> | Embed an image |
src | Image URL or path |
alt | Text description (required) |
width / height | Dimensions in pixels |
loading="lazy" | Defer off-screen loading |
<picture> | Responsive source switching |
srcset | Multiple resolution options |
<figure> | Image + caption group |
Six examples from a basic image to a full page with responsive and lazy-loaded photos. Each includes View Output and Try It Yourself.
<img src="photo.jpg" alt="A scenic mountain landscape at sunrise"> src points to the file; alt describes it for users who cannot see the image.
<img src="photo.jpg" alt="Green landscape photo"
width="300" height="195" title="Hover tooltip text"> Dimensions reserve space before the image loads. title adds an optional hover tooltip.
<figure>
<img src="chart.jpg" alt="Bar chart showing 25% sales growth in Q1">
<figcaption>Figure 1: Sales growth in Q1.</figcaption>
</figure> figure groups the image and visible caption. alt still describes the image for screen readers.
<img src="large-photo.jpg" alt="High-resolution photo"
loading="lazy" width="500" height="320"> The browser downloads the image only when it is about to scroll into view, saving bandwidth on long pages.
<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> The browser picks the first matching source. The final img is the fallback.
Full page combining basic img, picture, figure, and lazy loading:
<!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> Demonstrates every technique from this tutorial on one page with semantic markup and proper alt text.
alt text for every imageloading="lazy" for below-the-fold photoswidth and height to prevent layout shiftpicture or srcset for responsive layoutsalt attribute entirelytitle instead of altpicture or figure before child elements<img> works in every browser. loading="lazy", picture, and srcset are supported in all modern browsers. Test responsive images on mobile Safari and Chrome.
<img> works in every browser. loading="lazy", picture, and srcset are supported in all modern browsers. Test responsive images on mobile Safari and Chrome.
Bottom line: Basic images are universal; responsive and lazy features cover virtually all users today.
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.
Always.
a11yPick right.
JPEG WebPResponsive.
MobileFaster load.
PerfCaptions.
SemanticThe 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.
Add basic images, responsive picture elements, and lazy loading, then preview live.
6 people found this page helpful