css-containment
它能做什么
这项技能展示如何使用 contain 属性和 content-visibility,告诉浏览器某些组件是自包含的。适合页面中有大量组件、而感觉运行缓慢的开发者。
适用场景
- ✓当包含许多独立组件的页面在更新时感觉迟缓时。
- ✓当长列表渲染了屏幕之外的内容时。
- ✓当一个小组件没有溢出内容,也不需要影响页面其他部分时。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/css-containment/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的长商品列表滚动时有些卡顿。请添加 containment,让屏幕之外的项目少渲染一些。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会为自包含的组件添加 contain 值,并为重复出现的屏幕外列表项添加 content-visibility: auto,同时确认它们不会溢出或影响相邻元素。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Use CSS containment to limit repaint scope
By default, a CSS change anywhere on the page can potentially affect any other element's layout. CSS containment declares that certain elements are isolated — changes inside them don't affect the outside world. This lets browsers skip large portions of the rendering pipeline for unchanged content, which is especially valuable for pages with many independent components.
Quick Reference
- contain: layout style paint tells the browser a component is visually self-contained
- content-visibility: auto skips rendering off-screen content entirely
- Containment limits how far layout recalculations need to propagate
- Use contain: strict for widgets with no overflow that shouldn't affect the page
Check
Identify components in this CSS that are visually self-contained (no overflow, no interaction with elements outside) and could benefit from CSS containment.
Fix
Add appropriate contain values to self-contained components. Add content-visibility: auto to repeated off-screen list items.
Explain
Explain CSS containment values, how they limit rendering scope, and when content-visibility: auto provides the biggest benefit.
Code Review
Review stylesheets, component styles, and responsive states related to Use CSS containment to limit repaint scope. 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/css-containment
常见问题
contain 有什么作用?
这项技能说明,它告诉浏览器某个组件在视觉上是自包含的,因此布局计算可以局限在其内部。
content-visibility: auto 有什么作用?
这项技能说明,它会跳过渲染屏幕之外的内容。
什么时候使用 strict 形式的 contain?
用于没有溢出内容、并且不应影响页面其他部分的小组件。
详情
作者的原始描述
Use when reviewing stylesheets, component styles, and responsive behavior related to Use CSS containment to limit repaint scope. Check the rendered layout across breakpoints and interaction states before proposing a fix.
热度
- GitHub 星标
- 75k
- 排名
- #2795
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。