Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to HTML Notes
Topic #407

Responsive Images

Responsive images automatically adapt to the visitor's screen size and resolution — serving a smaller file to a phone and a larger one to a high-resolution desktop monitor, instead of sending everyone the same oversized file.

Syntax

<img
    src="photo-800w.jpg"
    srcset="photo-400w.jpg 400w, photo-800w.jpg 800w, photo-1200w.jpg 1200w"
    sizes="(max-width: 600px) 100vw, 50vw"
    alt="..."
>

Basic Example

<img
    src="course-banner-800.jpg"
    srcset="course-banner-400.jpg 400w, course-banner-800.jpg 800w, course-banner-1600.jpg 1600w"
    sizes="100vw"
    alt="CodingNow course banner"
>

Browser output: visually just one image — but the browser silently picks whichever file in srcset best matches the visitor's actual screen width and pixel density, rather than always downloading the largest one.

How It Works

  1. The browser looks at the device's viewport width and pixel density.
  2. It checks sizes to work out how large the image will actually be displayed.
  3. It picks the smallest candidate from srcset that still looks sharp at that display size — a phone gets a small file, a large retina monitor gets a bigger one.

Important Attributes

AttributePurpose
srcA fallback image for browsers that don't support srcset
srcsetA list of candidate image files with their real widths — see srcset
sizesTells the browser how large the image will actually render at different viewport widths

Real-World Example

<picture>
    <source media="(max-width: 600px)" srcset="hero-mobile.jpg">
    <img src="hero-desktop.jpg" alt="CodingNow campus hero image">
</picture>

Combining responsive image techniques with <picture> for an art-directed hero banner — a genuinely different crop for mobile, not just a scaled-down version. See picture-element.

Common Mistakes

  • Serving the same large desktop-sized image to every device, wasting significant bandwidth on mobile connections
  • Writing an incorrect or missing sizes attribute, which can cause the browser to pick a larger (or smaller) image than actually needed

Best Practices

  • Generate multiple sizes of every important image and serve them via srcset
  • Set an accurate sizes value that reflects the image's real rendered width across breakpoints

Accessibility Considerations

No direct accessibility impact beyond the underlying image still needing correct alt text — responsive images are purely a performance/bandwidth technique.

SEO Considerations

Faster-loading, appropriately-sized images directly improve Core Web Vitals scores like Largest Contentful Paint — a real, measurable ranking factor, especially significant on mobile search.

Browser & Modern HTML Notes

srcset/sizes and <picture> are both well-supported HTML5-era additions, purpose-built for exactly this problem — before them, developers relied entirely on JavaScript workarounds.

Mini Practice

  1. Write a srcset with three candidate image widths for a banner image.
  2. Add a matching sizes attribute assuming the image displays at full viewport width on mobile and half viewport width on desktop.

Related HTML Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →