container-queries
它能做什么
这项技能展示如何使用容器查询(container queries),这是一种根据父元素宽度改变组件布局的 CSS 功能。适合构建可复用组件的前端开发者。
适用场景
- ✓当卡片或小组件在侧边栏中显示不正常,但在主栏中显示正常时。
- ✓当你构建的组件会出现在多种不同布局中时。
- ✓当你的媒体查询只根据屏幕宽度来设置组件样式时。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/container-queries/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的商品卡片放进侧边栏就会出问题。请让它根据容器而不是屏幕来调整。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会在父元素上设置 container-type,并编写当容器达到特定宽度时生效的容器查询规则。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Use container queries for component-level responsiveness
Media queries respond to viewport width, which makes components context-dependent — a card designed for a 3-column grid might break when placed in a 2-column grid or a sidebar. Container queries solve this: a card can layout itself based on how much space its container gives it, making the component genuinely reusable in any layout context.
Quick Reference
- Container queries respond to the parent container's size, not the viewport
- Set container-type: inline-size on the parent to enable container queries
- Use @container rule to write styles that activate at container breakpoints
- Container queries make components reusable in sidebars, grids, and full-width areas
Check
Look for components in this CSS that have media query breakpoints — consider whether container queries would make them more reusable in different layout contexts.
Fix
Convert this component from media query-based to container query-based responsiveness using container-type and @container.
Explain
Explain how container queries differ from media queries, why they enable better component reuse, and how to set up the container/query relationship.
Code Review
Review stylesheets, component styles, and responsive states related to Use container queries for component-level responsiveness. Flag exact selectors, declarations, or breakpoints that violate the rule in the rendered UI.
For full implementation details, code examples, and framework-specific guidance,
see references/rule.md.
Rule page: https://frontendchecklist.io/rules/css/container-queries
常见问题
容器查询与媒体查询有什么区别?
这项技能说明,它响应的是父容器的尺寸,而不是视口宽度。
父元素需要什么设置?
这项技能说明需要将 container-type 设为 inline-size。
为什么这有助于复用?
这项技能说明,组件可以为侧边栏、网格和全宽区域自行调整布局。
详情
作者的原始描述
Use when reviewing stylesheets, component styles, and responsive behavior related to Use container queries for component-level responsiveness. Check the rendered layout across breakpoints and interaction states before proposing a fix.
热度
- GitHub 星标
- 75k
- 排名
- #2783
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。