The essentials

Quick reference

One focused task per row. Jump to the related section for complete, working examples.

UseSyntaxExamples
Embed an image<img src="team.jpg" alt="The product team in the studio">View examples
Hide a decorative image<img src="divider.svg" alt="">View examples
Reserve image space<img src="portrait.jpg" alt="Ada Lovelace" width="800" height="1000">View examples
Lazy-load an image<img src="gallery-4.jpg" alt="Mountain lake at sunrise" loading="lazy" width="1200" height="800">View examples
Request async decoding<img src="chart.png" alt="Quarterly revenue increased each month" decoding="async" width="960" height="540">View examples
Prioritize a key image<img src="hero.jpg" alt="Developer workspace" fetchpriority="high" width="1600" height="900">View examples
Offer density variants<img src="logo.png" srcset="logo.png 1x, logo@2x.png 2x" alt="Northwind" width="240" height="80">View examples
Offer width variants<img src="trail-800.jpg" srcset="trail-480.jpg 480w, trail-800.jpg 800w, trail-1280.jpg 1280w" alt="Forest trail">View examples
Describe the image slot<img src="trail-800.jpg" srcset="trail-480.jpg 480w, trail-800.jpg 800w, trail-1280.jpg 1280w" sizes="(max-width: 600px) 100vw, 50vw" alt="Forest trail">View examples
Offer a modern format<picture> <source srcset="portrait.avif" type="image/avif"> <img src="portrait.jpg" alt="Ada at her desk"> </picture>View examples
Offer several formats<picture> <source srcset="hero.avif" type="image/avif"> <source srcset="hero.webp" type="image/webp"> <img src="hero.jpg" alt="City skyline"> </picture>View examples
Change the crop<picture> <source media="(max-width: 600px)" srcset="speaker-close.jpg"> <img src="speaker-wide.jpg" alt="Speaker presenting on stage"> </picture>View examples
Combine media and widths<picture> <source media="(min-width: 900px)" srcset="banner-1200.jpg 1200w, banner-1800.jpg 1800w" sizes="80vw"> <img src="banner-720.jpg" alt="Conference attendees"> </picture>View examples
Caption an image<figure> <img src="diagram.svg" alt="Request flow from browser to server"> <figcaption>Figure 1: Request lifecycle</figcaption> </figure>View examples
Label an image link<a href="/reports/"> <img src="report-cover.jpg" alt="Open the annual report"> </a>View examples

Start with an img element that has a useful text alternative and intrinsic dimensions. Add srcset and sizes when the same image must adapt to its layout, and use picture when the content, crop, or file format should change under specific conditions.

Step by step

Detailed examples

01

Give every image an appropriate text alternative

Use concise alt text when an image conveys meaning, describing its purpose in context rather than its appearance alone. Use an empty alt attribute for a purely decorative image; omitting alt entirely can cause assistive technology to announce the filename instead.

Informative and decorative images
<article>
  <h2>Meet the product team</h2>
  <img src="team.jpg" alt="The product team in the studio">
  <img src="divider.svg" alt="">
  <p>We design and maintain the application together.</p>
</article>

Note: Do not begin alt text with phrases such as 'image of'; screen readers already expose the element as an image.

Back to quick reference ↑
02

Control layout, loading, and decoding deliberately

Set width and height to the source image's intrinsic pixel dimensions so the browser can calculate its aspect ratio. Lazy-load images below the initial viewport, but keep the primary above-the-fold image eager and use high fetch priority only for a genuinely important resource. Loading and decoding values are hints, so the browser retains final scheduling control.

Primary and deferred content images
<main>
  <img src="hero.jpg" alt="Developer workspace" fetchpriority="high" width="1600" height="900">
  <img src="portrait.jpg" alt="Ada Lovelace" width="800" height="1000">
  <img src="chart.png" alt="Quarterly revenue increased each month" decoding="async" width="960" height="540">
  <img src="gallery-4.jpg" alt="Mountain lake at sunrise" loading="lazy" width="1200" height="800">
</main>

Note: Do not lazy-load the likely largest contentful paint image; delaying it can make the page appear slower.

Back to quick reference ↑
03

Supply density variants for a fixed-size slot

Use x descriptors when an image renders at one stable CSS size but higher-density displays benefit from more source pixels. Keep the candidate dimensions proportional, and do not mix density descriptors with width descriptors in the same srcset.

One logo at two pixel densities
<img
  src="logo.png"
  srcset="logo.png 1x, logo@2x.png 2x"
  alt="Northwind"
  width="240"
  height="80">
Back to quick reference ↑
04

Match source width to the rendered slot

Use w descriptors when an image changes size with the layout. Each descriptor must state the candidate file's real intrinsic width, while sizes describes the slot width, not the source file width. The browser combines this information with display density and may choose any suitable candidate.

Responsive content image
<img
  src="trail-800.jpg"
  srcset="trail-480.jpg 480w, trail-800.jpg 800w, trail-1280.jpg 1280w"
  sizes="(max-width: 600px) 100vw, 50vw"
  alt="Forest trail"
  width="1280"
  height="853">

Note: Keep sizes synchronized with the CSS layout; an inaccurate slot size can make the browser download an unnecessarily large or visibly soft resource.

Back to quick reference ↑
05

Offer image formats with a dependable fallback

Place source elements before the required img fallback inside picture. The browser considers sources in document order and uses the type value to skip unsupported formats without downloading them. The img element still owns the alternative text, dimensions, and fallback URL.

AVIF, WebP, and JPEG sources
<picture>
  <source srcset="hero.avif" type="image/avif">
  <source srcset="hero.webp" type="image/webp">
  <img src="hero.jpg" alt="City skyline" width="1600" height="900">
</picture>
One modern format with fallback
<picture>
  <source srcset="portrait.avif" type="image/avif">
  <img src="portrait.jpg" alt="Ada at her desk" width="800" height="1000">
</picture>
Back to quick reference ↑
06

Use picture for art direction

Use media conditions when different viewport ranges need a genuinely different crop or composition. A source can also provide its own srcset and sizes. Keep the img alternative accurate for every selected source because all variants share that single accessible description.

Close crop on small screens
<picture>
  <source media="(max-width: 600px)" srcset="speaker-close.jpg">
  <img src="speaker-wide.jpg" alt="Speaker presenting on stage" width="1400" height="800">
</picture>
Wide responsive banner source
<picture>
  <source media="(min-width: 900px)" srcset="banner-1200.jpg 1200w, banner-1800.jpg 1800w" sizes="80vw">
  <img src="banner-720.jpg" alt="Conference attendees" width="720" height="720">
</picture>
Back to quick reference ↑

Local code tester

Try accessible image markup

Edit the figure, alternative text, dimensions, and loading hints, then render the markup locally.

Runs in your browser
Preview

Sources and further reading

References

Authoritative documentation used to verify and expand this cheat sheet.

  1. WHATWGHTML Standard — Imageshtml.spec.whatwg.org
  2. MDN Web DocsThe img elementdeveloper.mozilla.org
  3. MDN Web DocsThe picture elementdeveloper.mozilla.org
  4. MDN Web DocsUsing responsive images in HTMLdeveloper.mozilla.org

Help us improve

Found a typo or missing example?

Tell us what would make this cheat sheet clearer, more complete, or more useful.

Share feedback