跳到正文
Skillpedia
图片与图标75k · #3034

srcset

它能做什么

这个技能检查网站的图片标签,在缺少的地方添加 srcset 和 sizes 属性,并修正与布局不符的属性。它面向希望图片在每种设备上都能高效加载的网站构建者。

适用场景

  • ✓你请智能体让网站上的图片为每种设备加载合适尺寸的时候。
  • ✓手机下载大型桌面图片,而使用移动数据的访客抱怨页面加载慢的时候。
  • ✓图片已有 srcset,但其 sizes 属性与图片的显示方式不符的时候。

技能链接

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/srcset/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/srcset/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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 srcset with width descriptors (400w, 800w) to all images wider than ~100px
  • Always pair srcset width descriptors with a sizes attribute—without sizes the browser assumes 100vw
  • Use density descriptors (1x, 2x) only for fixed-size images like avatars and icons
  • Keep src pointing to a fallback size for browsers that don't support srcset

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.

Rule page: https://frontendchecklist.io/rules/images/srcset

常见问题

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.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#3034
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能