HTML: HTML Responsive Images

Last updated: 2026-09-08

The same image should display at different sizes and resolutions on phones, tablets, and desktops — this is "responsive images." Learn how to make images clear, bandwidth-efficient, and fast-loading across all screen sizes.

Tags in this section: <meta> <picture> <source> <img>

1. Viewport Settings

The first step in responsive design is setting the viewport. Without it, mobile browsers shrink the page to a 980px width for display, making text and images very small. Add this <meta> tag and the page width will match the device screen:

▶ Example

▶ Try it Yourself

2. Image Responsiveness

The two most commonly used CSS properties for making images automatically adapt to their container width:

▶ Example

▶ Try it Yourself
📌 Image loading speed matters. Using CSS to shrink a large image only makes it "look smaller" — the browser still downloads the full-size original file. It's best to crop images to the right size before uploading:small images for mobile, large images for desktop. This can dramatically improve loading speed.


3. The srcset Attribute

srcset lets the browser automatically select the most suitable image based on screen density (1x, 2x, 3x) or viewport width. You simply provide multiple image versions, and the browser "smartly chooses":

▶ Example

▶ Try it Yourself

There are two common ways to use srcset:


4. The <picture> Element

<picture> is more flexible than srcset. It lets you load completely different images at different screen widths — for example, a panoramic image on wide screens and a cropped close-up on narrow screens (this is called "art direction" in design):

▶ Example

▶ Try it Yourself

<picture> is also commonly used to offer multiple image formats (such as WebP), with the browser selecting the first supported format:

▶ Example

▶ Try it Yourself

The browser checks each <source> from top to bottom. If it supports the format, it loads it; if none are supported, it falls back to <img>. This way, you enjoy the compression benefits of new formats while maintaining compatibility.

Tags in this section: <meta> <picture> <source> <img>

❓ FAQ

Q What's the difference between srcset and sizes?
A srcset provides multiple image sources with their resolutions (like 480w, 768w), while sizes tells the browser the actual display width of the image at different viewport widths (like (max-width: 480px) 100vw). The browser calculates which image to load based on both. Simply put: srcset is "what images are available," sizes is "how big will the image be in different situations."
Q Why not use JavaScript to detect screen size and switch images?
A Using srcset+sizes has three advantages: 1) Browsers can decide which image to load while parsing HTML, without waiting for JavaScript; 2) Works even when JavaScript is disabled; 3) Browsers can choose more intelligently (considering network conditions, device pixel ratio, etc.). JavaScript is only suitable for complex art direction scenarios (like different compositions); simple resolution switching is handled by srcset.
Q When should I use <picture> vs srcset?
A srcset is for "different resolution versions of the same image" — the browser automatically selects the appropriate sharpness. <picture> is for "displaying different images in different scenarios" — which could be different compositions (art direction) or different formats (WebP/AVIF/JPG). Simple rule: if only sharpness differs, use srcset; if composition or format differs, use <picture>.
Q Do responsive images affect SEO?
A Properly used responsive images positively impact SEO: 1) Improve page load speed (Core Web Vitals metrics); 2) Enhance mobile user experience; 3) Search engines can index alt text. Note: images in srcset must be different versions of the same content — don't use completely different images, or search engines may consider it deceptive.

📖 Summary

📝 Exercises

  1. Responsive Image Gallery: Create a gallery page with 3 images, using max-width: 100% on each to ensure they display completely on mobile. Use srcset to provide 1x and 2x versions for each image.
  2. Art Direction Practice: Use <picture> to implement: desktop shows a 1200px-wide landscape photo, tablet shows a 768px cropped version, phone shows a 480px close-up. Use min-width media queries to control breakpoints.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏