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

picture-element

它能做什么

这个技能检查项目中的 picture 元素,并修复缺少 img 备用元素或关键属性的部分。它面向使用现代图片格式的网站构建者。

适用场景

  • ✓你请智能体检查项目中的 picture 元素是否缺少备用元素的时候。
  • ✓某些浏览器中图片不显示,而 picture 标记可能不完整的时候。
  • ✓你在添加现代图片格式,并希望从一开始标记就正确的时候。

技能链接

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

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

使用方法

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

可直接粘贴的消息

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

可以这样问 AI

“请帮我检查网站上的 picture 元素,并修复缺少备用图片或 alt 文本的部分吗?”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会检查每个 picture 元素是否以带有 alt、width 和 height 属性的 img 元素结尾。它还会检查 source 元素是否排在前面、是否有 srcset 或 media 值,以及是否包含用于格式选择的 type。然后它会补上缺失的部分。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Use <picture> with an <img> fallback

A <picture> without an <img> fallback renders nothing in browsers that don't support <picture> (IE11) or don't support any of the <source> formats. The <img> child is also where alt, width, height, loading, and fetchpriority attributes live—these apply regardless of which source the browser selects.

Quick Reference

  • Every <picture> must end with an <img> element—it is the required fallback and carries alt
  • <source> elements are evaluated top-to-bottom; the first match wins
  • Use type="image/avif" and type="image/webp" on <source> for format selection
  • The <img src> should point to a JPEG or PNG—the universal fallback for all browsers

Check

Scan all <picture> elements in this codebase. Verify: 1) Every <picture> has an <img> as its last child (required fallback). 2) The <img> has an alt attribute. 3) The <img> has width and height attributes. 4) <source> elements appear before the <img> child. 5) Each <source> has either a srcset attribute or a media attribute. 6) format-selecting <source> elements have a type attribute (type="image/avif", type="image/webp"). Flag any <picture> missing an <img> child or with <img> not as the last child.

Fix

For each <picture> element with issues: 1) If missing <img>: add <img src="{fallback-url}" alt="{description}" width="{w}" height="{h}"> as the last child. 2) If <img> is not last: move it to after all <source> elements. 3) If <source> elements lack type attributes for format selection: add type="image/avif" or type="image/webp". 4) Verify the <img src> points to a universally supported format (JPEG or PNG) as the ultimate fallback. Show the corrected <picture> markup.

Explain

Explain how the <picture> element works. Browsers evaluate <source> elements in document order and use the first one whose conditions match (type, media, srcset). The <img> element is the fallback for browsers that don't support <picture> (IE11) or don't support the source format. Without an <img> child, <picture> is invalid HTML and produces no output. The alt attribute must be on the <img>, not the <source> elements—it applies regardless of which source is selected.

Code Review

Review image assets, markup, and delivery configuration related to Use <picture> with an <img> fallback. 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/picture-element

常见问题

为什么需要 img 备用元素?

不支持 picture 或所列格式的浏览器,需要 img 元素才能显示任何内容。

source 元素如何工作?

这个技能说,它们会从上到下依次检查,并使用第一个匹配项。

alt 和尺寸属性应该放在哪里?

这个技能说,它们应放在 img 元素上,无论选中哪个 source,它们都适用。

详情

作者的原始描述

Use when reviewing image assets, markup, and CDN or build transforms related to Use <picture> with an <img> fallback. Check encoded size, rendered size, loading strategy, and above-the-fold impact together.

repo: thedaviddias/Front-End-Checklist

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能