跳到正文
Skillpedia
网站与界面(前端)75k · #2783

container-queries

它能做什么

这项技能展示如何使用容器查询(container queries),这是一种根据父元素宽度改变组件布局的 CSS 功能。适合构建可复用组件的前端开发者。

适用场景

  • ✓当卡片或小组件在侧边栏中显示不正常,但在主栏中显示正常时。
  • ✓当你构建的组件会出现在多种不同布局中时。
  • ✓当你的媒体查询只根据屏幕宽度来设置组件样式时。

技能链接

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

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

使用方法

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

可直接粘贴的消息

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

可以这样问 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.

repo: thedaviddias/Front-End-Checklist

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能