[CSS3] Image Width with sizes (srcset & sizes)
What if the image won't be displayed at the full viewport width? Then you need something more than srcset
, which assumes the image will be full viewport width.
Add a sizes
attribute to the image with a media query and a vw
value. srcset
and sizes
together tell the browser the natural width of the image, and how wide the image will be displayed relative to viewport width. Knowing the display width of the image and the widths of the image files available to it, the browser has the information it needs to download the image with the right resolution for its needs that is as small as possible. And it can make this choice early in the page load while the HTML is still being parsed.
srcset with sizes Syntax
Here's an example:
<img src="images/great_pic_800.jpg" sizes="(max-width: 400px) 100vw, (min-width: 401px) 50vw" srcset="images/great_pic_400.jpg 400w, images/great_pic_800.jpg 800w" alt="great picture">
sizes
consists of comma separated mediaQuery width
pairs. sizes
tells the browser early in the load process that the image will be displayed at some width
when the mediaQuery
is hit.
In fact, if sizes
is missing, the browser defaults sizes
to 100vw
, meaning that it expects the image will display at the full viewport width.
sizes
gives the browser one more piece of information to ensure that it downloads the right image file based on the eventual display width of the image. Just to be clear, it does not actually resize the image - that's what CSS does.
In this example, the browser knows that the image will be full viewport width if the browser's viewport is 400px wide or less, and half viewport width if greater than 400px. It knows that it has two image options - one with a natural width of 400px and the other 800px.
A word about how w
units work, from Yoav Weiss who implemented responsive images for Chrome and other browsers:
The selection logic is not defined in the spec (on purpose) so that every browser can apply their own selection logic, and be able to optimize the selected resources over time, in order to achieve the best quality/byte-size tradeoff for their users.
Blink's current selection logic Blink is the rendering engine used by Chrome is based on a geometric mean of adjacent (sorted by density) resource candidates.
If the DPR is a value between the densities of two adjacent candidates, the browser calculates the candidates' geometric mean. If DPR is higher than the geo mean, the candidate with the larger density value "wins". Otherwise, it's the smaller one.
For your example, the geo mean of 500w and 1000w is 707, which explains why only above that value, the larger resource gets picked.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2017-03-06 [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
2017-03-06 [Angular] Using ngTemplateOutlet to create dynamic template
2017-03-06 [Angular] Create dynamic content with <tempalte>
2017-03-06 [Postgres] Create a Postgres Table
2017-03-06 [Typescript 2] Nullable Types - Avoiding null and undefined Bugs
2017-03-06 [Ramda] Sort, SortBy, SortWith in Ramda
2017-03-06 [Django] Creating an app, models and database