Responsive Images

Lecture notes: the picture tag, the srcset attribute and optimizing graphics for different devices

1. The picture tag

The <picture> tag lets you show different images on different devices. It is a wrapper (like <div>) that does not display anything itself.

Structure

<picture>
<source
srcset="banner-mobile.webp 1x, banner-mobile-2x.webp 2x"
media="(max-width: 480px)"
>
<source
srcset="banner-desktop.webp 1x, banner-desktop-2x.webp 2x"
>
<img src="banner-desktop.webp" alt="Banner">
</picture>

How it works

  • <source> — an equivalent of <img>, sets the image via srcset
  • The media attribute — the same media queries as in CSS (min-width, orientation, etc.)
  • The srcset attribute — a list of images with conditions (1x, 2x for pixel density or 320w, 768w for width)
  • <img> at the end — mandatory; needed for the alt attribute and as a fallback when loading fails

When to use it

  • Banners — a horizontal banner on desktop, a vertical one on mobile
  • Different formats — WebP for modern browsers, PNG/JPEG as a fallback
  • Different content — a detailed image on a large screen, a simplified one on a small screen

2. The srcset attribute

The srcset attribute can be used both in <source> and directly in <img>:

<img
src="photo.jpg"
srcset="photo.jpg 1x, photo-2x.jpg 2x, photo-3x.jpg 3x"
alt="Photo"
>

Descriptors

  • 1x, 2x, 3x — pixel density (1x = a regular screen, 2x = Retina)
  • 320w, 768w — the image width in pixels (the browser picks the best one for the viewport width)

Caching specifics

The browser caches images and does not “downgrade” quality: if the 2x version has already been loaded, it will not load the 1x version when the window shrinks. For testing, enable Disable cache in DevTools.

SVG as a responsive image

You can write media queries inside an SVG file. The SVG elements will show or hide depending on the size. For example, a house icon: simplified on a small screen, and detailed (windows, doors) on a large one.

3. Summary table

Topic What to remember
<picture> Different images on different devices; <source> + media + srcset; <img> is mandatory
srcset A list of images with descriptors: 1x/2x (density) or 320w/768w (width)

Vitalii Kaplia

Founder, Web Developer & WordPress Expert

I became interested in programming back in 1997. The first acquaintance with a future profession was using Visual Basic. In…

More about author

A digital product engineer and web solutions architect

Free consultation + cost calculation

Let’s discuss your project?

Free consultation + cost calculation

More interesting articles

Customer login

This site uses cookies

We use cookies to personalize content and ads, provide social media features, and analyze our traffic. We also share information about your use of our website with our social media, advertising, and analytics partners, who may combine it with other information you have provided to them or collected when you use their services. By continuing to use our site, you consent to our use of cookies and accept our Privacy Policy and Terms of Use.

Virtual assistant
Hi! I'm the KAPLIA.PRO virtual assistant! I can tell you about our services, portfolio projects, ballpark pricing, and how we work — from websites and eCommerce to web apps, CRMs, bots, and AI agents. Ask away!

By continuing with the AI assistant, I agree to the site’s terms of use and privacy policy.

AI assistant may make mistakes in responses