srcset
它能做什么
这个技能检查网站的图片标签,在缺少的地方添加 srcset 和 sizes 属性,并修正与布局不符的属性。它面向希望图片在每种设备上都能高效加载的网站构建者。
适用场景
- ✓你请智能体让网站上的图片为每种设备加载合适尺寸的时候。
- ✓手机下载大型桌面图片,而使用移动数据的访客抱怨页面加载慢的时候。
- ✓图片已有 srcset,但其 sizes 属性与图片的显示方式不符的时候。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/srcset/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我网站上的手机下载的是完整的桌面图片。请帮我添加 srcset,让每种设备都获得合适的尺寸吗?”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会找出宽度超过约 100 像素且缺少 srcset 的图片,并制作宽度为 400、800 和 1200 像素等的版本。它会添加 srcset 列表,以及与真实布局相符的 sizes 属性,并保留备用的 src。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Use srcset for responsive images
Without srcset, every user—regardless of their device—downloads the same large image. A 1600px hero image served to a 375px mobile display downloads 4x the data needed. The srcset attribute lets browsers choose the optimal file automatically, reducing bandwidth by 50-80% for mobile users with no change to visual quality.
Quick Reference
- Add
srcsetwith width descriptors (400w,800w) to all images wider than ~100px - Always pair
srcsetwidth descriptors with asizesattribute—withoutsizesthe browser assumes100vw - Use density descriptors (
1x,2x) only for fixed-size images like avatars and icons - Keep
srcpointing to a fallback size for browsers that don't supportsrcset
Check
Scan all <img> elements in this codebase. Flag: 1) Any <img> wider than 100px (in CSS or layout) that lacks a srcset attribute. 2) Any <img> with srcset but missing a sizes attribute (necessary for width descriptors). 3) Any <img> using only density descriptors (1x, 2x) where width descriptors (400w, 800w) would be more appropriate. 4) Any srcset that provides only one size (no benefit over plain src). Report each finding with file path and line number.
Fix
For each <img> missing srcset: 1) Generate multiple width variants: typically 400w, 800w, 1200w (add 1600w for full-bleed images). 2) Add srcset listing each variant with its width descriptor. 3) Add a sizes attribute describing actual CSS layout width at each breakpoint. 4) Keep src pointing to a mid-range size (800w) as the fallback. 5) Add width and height attributes if missing. For each image, show the complete corrected HTML including srcset and sizes.
Explain
Explain how srcset and the two descriptor types work. Width descriptors (400w, 800w) describe the intrinsic width of each candidate; the browser combines this with the sizes attribute to calculate which candidate provides the best pixel density for the current viewport. Density descriptors (1x, 2x) are simpler but less flexible—use them only for fixed-size images like avatars and icons. The sizes attribute is critical: without it, the browser assumes 100vw and always downloads the largest candidate even on narrow screens.
Code Review
Review image assets, markup, and delivery configuration related to Use srcset for responsive images. Flag exact files or components where format choice, sizing, or loading behavior violates the rule, and describe how to confirm the fix in DevTools.
For full implementation details, code examples, and framework-specific guidance,
see references/rule.md.
常见问题
sizes 为什么重要?
这个技能说,没有它,浏览器会假定图片占满整个视口宽度,从而可能选中过大的文件。
什么时候应使用密度描述符?
这个技能说,只对头像和图标等固定尺寸的图片使用 1x 和 2x。
这会改变页面的外观吗?
不会。这个技能说,它在不改变图片外观的情况下减小下载体积。
详情
作者的原始描述
Use when reviewing image assets, markup, and CDN or build transforms related to Use srcset for responsive images. Check encoded size, rendered size, loading strategy, and above-the-fold impact together.
热度
- GitHub 星标
- 75k
- 排名
- #3034
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。