Home >Web Front-end >CSS Tutorial >How to Build Responsive Images with srcset

How to Build Responsive Images with srcset

Joseph Gordon-Levitt
Joseph Gordon-LevittOriginal
2025-02-23 10:24:41275browse

This article, part of Microsoft's web development series, explores responsive images – a crucial aspect of creating websites that function flawlessly across diverse devices. It's no longer optional; it's a necessity in our multi-device world.

How to Build Responsive Images with srcset

Key Takeaways:

  • Use the srcset attribute to offer various image sources optimized for different device pixel ratios.
  • Combine srcset with the sizes attribute to specify image display sizes relative to the viewport, guiding the browser to choose the best image.
  • Leverage the <picture></picture> element for enhanced flexibility, enabling image selection based on media conditions (like CSS media queries) and supporting various image formats via the type attribute.
  • Always include a default image using the src attribute as a fallback for browsers lacking srcset support.
  • Thoroughly test responsiveness and performance across various devices and browsers using tools like those offered by Microsoft for compatibility checks and web experience optimization.

Understanding Responsive Images:

A responsive image adapts its presentation to the viewing device. "Best form" means different things:

  • Displaying different image assets based on screen size (e.g., a different image for a 13.5-inch laptop versus a 5-inch phone).
  • Using different images based on device resolution (or device-pixel ratio – the ratio of device pixels to CSS pixels).
  • Selecting an image format (like JPEG XR) if supported by the browser, potentially for better compression.

Enabling Responsive Images:

While older scripting methods exist, they're not recommended due to potential issues with multiple downloads or missing images. The preferred approach uses:

  1. srcset attribute
  2. sizes attribute
  3. <picture></picture> element

The srcset Attribute:

Before diving into srcset usage, let's define:

Device-Pixel Ratio: This is the number of device pixels per CSS pixel, influenced by:

  1. Device pixel density (physical pixels per inch): Higher resolution devices have higher density and thus a higher device-pixel ratio at the same zoom level.
  2. Browser zoom level: Higher zoom levels increase the device-pixel ratio for a given device.

Basic srcset implementation uses the x descriptor for device-pixel ratio:

<code class="language-html"><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174027748422704.jpg" class="lazy" alt="How to Build Responsive Images with srcset "></code>
  • 1x: space-needle.jpg for a device-pixel ratio of 1.
  • 2x: space-needle-2x.jpg for a device-pixel ratio of 2.
  • 3x: space-needle-hd.jpg for a device-pixel ratio of 3.

The src attribute serves as a fallback for browsers without srcset support. The w descriptor specifies image width:

<code class="language-html"><img  src="/static/imghwm/default1.png" data-src="images/space-needle.jpg" class="lazy" srcset="images/space-needle.jpg 200w, images/space-needle-2x.jpg 400w, images/space-needle-hd.jpg 600w" alt="How to Build Responsive Images with srcset" ></code>

The sizes Attribute:

To control image size based on viewport size, use the sizes attribute with the w descriptor in srcset.

Example 1 (Image at half viewport width):

<code class="language-html"><img  src="/static/imghwm/default1.png" data-src="images/space-needle.jpg" class="lazy" sizes="50vw" srcset="images/space-needle.jpg 200w, images/space-needle-2x.jpg 400w, images/space-needle-hd.jpg 600w" alt="How to Build Responsive Images with srcset" ></code>

Example 2 (Responsive sizing based on viewport width):

<code class="language-html"><img  src="/static/imghwm/default1.png" data-src="images/space-needle.jpg" class="lazy" sizes="(max-width: 40em) 100vw, 50vw" srcset="images/space-needle.jpg 200w, images/space-needle-2x.jpg 400w, images/space-needle-hd.jpg 600w" alt="How to Build Responsive Images with srcset" ></code>

The <picture></picture> Element:

The <picture></picture> element allows for image selection based on rendered size and browser capabilities. It contains <source></source> elements with srcset, sizes, media, and type attributes. An <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174027748575415.png" class="lazy" alt="How to Build Responsive Images with srcset ">

(Note: Image sources and sizes are illustrative.)

How to Build Responsive Images with srcset How to Build Responsive Images with srcset How to Build Responsive Images with srcset How to Build Responsive Images with srcset

Conclusion:

Responsive images enhance user experience across devices. Implement them for optimal website performance. Further resources and tools from Microsoft are available for cross-browser testing and web development best practices. A detailed FAQ section follows, addressing common questions about srcset implementation.

The above is the detailed content of How to Build Responsive Images with srcset. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn