Home >Web Front-end >CSS Tutorial >How to Build Responsive Images with srcset
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.
Key Takeaways:
srcset
attribute to offer various image sources optimized for different device pixel ratios.srcset
with the sizes
attribute to specify image display sizes relative to the viewport, guiding the browser to choose the best image.<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.src
attribute as a fallback for browsers lacking srcset
support.Understanding Responsive Images:
A responsive image adapts its presentation to the viewing device. "Best form" means different things:
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:
srcset
attributesizes
attribute<picture></picture>
elementThe 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:
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>
space-needle.jpg
for a device-pixel ratio of 1.space-needle-2x.jpg
for a device-pixel ratio of 2.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.)
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!