Skip to content

<img> - The Image Element

Media Element HTML 2.0

The image element embeds an image into the document. It’s one of the most commonly used HTML elements and supports modern features like responsive images, lazy loading, and performance optimization.

Result
<img src="path/to/image.jpg" alt="Description of image">

The <img> element is a void element (self-closing) and requires no closing tag.

Specifies the URL or path to the image:

Result

Provides alternative text when the image cannot be displayed or for screen readers:

<!-- Informative image -->
<img src="chart.png" alt="Sales increased 25% in Q4 2024">
<!-- Decorative image -->
<img src="divider.png" alt="">
<!-- Functional image (button/link) -->
<a href="/home">
<img src="home-icon.png" alt="Go to homepage">
</a>
<!-- Complex image with details nearby -->
<img src="infographic.png"
alt="Climate change statistics. See detailed data below.">

Specify dimensions in pixels. Setting these prevents layout shifts during page load:

Result

Provides multiple image sources for different screen densities:

Result

Let the browser choose the best image based on viewport width:

Result

How sizes works:

  • (max-width: 600px) 100vw - On screens ≤600px, image fills viewport width
  • (max-width: 1200px) 50vw - On screens ≤1200px, image is 50% of viewport
  • 800px - Default: image displays at 800px
Result

Control when images load to improve page performance:

Result

Values:

  • lazy - Defer loading until image nears viewport (recommended for below-fold images)
  • eager - Load immediately (default, use for above-fold images)

Control how the browser decodes the image:

<!-- Async decoding: don't block page rendering -->
<img src="large-photo.jpg" alt="Large photo" decoding="async">
<!-- Sync decoding: decode before rendering (rare use case) -->
<img src="critical-ui.png" alt="UI element" decoding="sync">
<!-- Auto: let browser decide (default) -->
<img src="photo.jpg" alt="Photo" decoding="auto">

Use async decoding for large images to prevent blocking the main thread.

Hint to the browser about image loading priority:

<!-- High priority: critical above-fold image -->
<img src="hero.jpg"
alt="Hero banner"
fetchpriority="high"
loading="eager">
<!-- Low priority: below-fold image -->
<img src="footer-logo.png"
alt="Logo"
fetchpriority="low"
loading="lazy">
<!-- Auto: default browser behavior -->
<img src="content.jpg" alt="Content" fetchpriority="auto">

Use the <picture> element for format fallbacks:

Result

Format comparison:

  • AVIF: Best compression (~30% smaller than WebP), limited support
  • WebP: Great compression (~25% smaller than JPEG), wide support
  • JPEG/PNG: Universal support, larger file sizes

Control CORS requests for images:

<!-- Anonymous: no credentials -->
<img src="https://cdn.example.com/image.jpg"
alt="CDN image"
crossorigin="anonymous">
<!-- Use credentials: send cookies/auth -->
<img src="https://api.example.com/user/avatar.jpg"
alt="User avatar"
crossorigin="use-credentials">

When to use:

  • Canvas manipulation with drawImage()
  • CSS filters applied to cross-origin images
  • WebGL textures

Control what referrer information is sent:

<!-- Don't send referrer -->
<img src="https://external.com/pixel.gif"
alt="Tracking pixel"
referrerpolicy="no-referrer">
<!-- Send origin only -->
<img src="https://cdn.example.com/image.jpg"
alt="CDN image"
referrerpolicy="origin">
<!-- Full referrer for same-origin, origin for cross-origin -->
<img src="image.jpg"
alt="Image"
referrerpolicy="strict-origin-when-cross-origin">

Associate clickable areas with an image using <map> and <area>:

Result
Result
Result
Result
Result
Result
<!-- Descriptive and concise -->
<img src="sunset.jpg"
alt="Orange sunset over mountain peaks">
<!-- Empty for decorative -->
<img src="border.png" alt="">
<!-- Functional description for links -->
<a href="/search">
<img src="search-icon.svg" alt="Search">
</a>
<!-- Context-aware -->
<figure>
<img src="chart.png"
alt="Bar chart showing 40% revenue increase">
<figcaption>Quarterly revenue growth</figcaption>
</figure>

For complex images like infographics or charts:

Result
<!-- Avoid: Don't use title as primary description -->
<img src="photo.jpg" title="Beach photo">
<!-- Better: Use alt, add title only for extra context -->
<img src="photo.jpg"
alt="Sunset beach in Hawaii"
title="Photo taken in Maui, December 2024">
Result
Result
Result
FeatureChromeFirefoxSafariEdge
Basic <img>1+1+1+12+
srcset & sizes38+38+9+13+
loading="lazy"77+75+15.4+79+
decoding65+63+11.1+79+
fetchpriority101+17.2+101+
WebP format23+65+14+18+
AVIF format85+93+16+85+
  • ✅ Always set width and height to prevent layout shifts
  • ✅ Use loading="lazy" for below-fold images
  • ✅ Use srcset and sizes for responsive images
  • ✅ Compress images (use tools like ImageOptim, Squoosh)
  • ✅ Serve modern formats (WebP, AVIF) with fallbacks
  • ✅ Set fetchpriority="high" on LCP image
  • ✅ Use appropriate image dimensions (don’t serve 4K for 400px display)
  • ✅ Consider using a CDN with automatic optimization