# CSS 包含

**CSS 包含**（CSS Containment）是一项 CSS 特性，定义于 CSS 包含模块中，它允许作者告诉浏览器：某个元素及其内容与文档树的其余部分尽可能相互独立，从而让浏览器可以限制其布局、样式和绘制工作的作用范围，并施加更强的渲染优化。<sup>[1](https://www.w3.org/TR/css-contain-1/)</sup> 它通过 `contain` 属性暴露给作者，并在更高层次上通过 `content-visibility` 属性暴露——后者会自动对屏幕外内容施加包含。<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup>

| 关键事实 | 详情 |
|---|---|
| 属性 | 第 1 级：`contain: none | strict | content | [ size || layout || paint ]`；第 2 级增加 `inline-size` 和 `style`<sup>[1](https://www.w3.org/TR/css-contain-1/)</sup><sup> • </sup><sup>[2](https://drafts.csswg.org/css-contain-2/)</sup> |
| 简写值 | 第 1 级：`strict` 计算为 `size layout paint`，`content` 计算为 `layout paint`；第 2 级及 web-features 数据库将 `strict` 视为 `size layout style paint`，将 `content` 视为 `layout style paint`<sup>[1](https://www.w3.org/TR/css-contain-1/)</sup><sup> • </sup><sup>[2](https://drafts.csswg.org/css-contain-2/)</sup><sup> • </sup><sup>[3](https://web-platform-dx.github.io/web-features-explorer/features/contain/)</sup> |
| 尺寸包含 | 元素布局时如同没有子元素；若未设置 `contain-intrinsic-size`，无尺寸元素可能渲染为 0px × 0px<sup>[4](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Using)</sup><sup> • </sup><sup>[5](https://developer.chrome.com/blog/css-containment)</sup> |
| content-visibility | `visible`、`auto` 或 `hidden`；`auto` 可跳过屏幕外内容的渲染，同时将其保留在 DOM 中，仍可聚焦、可选中、可查找<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup><sup> • </sup><sup>[4](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Using)</sup> |
| 实测收益 | 一个旅行博客示例将初始加载渲染时间从 232 ms 降至 30 ms；一项 n = 1,154,365 的实地测试在移动端三星浏览器第 75 百分位测得 LCP 改善 148 ms<sup>[6](https://web.dev/articles/content-visibility)</sup><sup> • </sup><sup>[7](https://www.speedkit.com/blog/field-testing-css-containment-for-web-performance-optimization)</sup> |
| 支持情况 | `contain` 随 Chrome 52（2016 年）、Firefox 69（2019 年）、Safari 15.4（2022 年）发布；`content-visibility` 随 Safari 26 于 2025-09-15 成为 Baseline 新可用特性<sup>[3](https://web-platform-dx.github.io/web-features-explorer/features/contain/)</sup><sup> • </sup><sup>[8](https://web-platform-dx.github.io/web-features-explorer/features/content-visibility/)</sup><sup> • </sup><sup>[9](https://web.dev/blog/css-content-visibility-baseline)</sup> |

## 什么是包含

`contain` 属性让作者表明：某个元素及其内容——用规范的话说——与文档树的其余部分尽可能相互独立。用户代理可以利用包含来限制子树之外的某些渲染工作，不过效果取决于施加了哪些包含类型。<sup>[1](https://www.w3.org/TR/css-contain-1/)</sup> MDN 描述了四种包含类型：布局、绘制、尺寸和样式。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Using)</sup>

该属性不可动画化，其值要么是关键字（`none`、`strict`、`content`），要么是各包含类型的组合。<sup>[1](https://www.w3.org/TR/css-contain-1/)</sup>

## 包含值及其效果

**尺寸包含**（`contain: size`）确保包含盒可以在不检查其后代的情况下完成布局。元素尺寸的计算如同它没有子元素，因此作者必须通过 `contain-intrinsic-size` 或其 `contain-intrinsic-width`、`contain-intrinsic-height` 长属性，或通过直接尺寸或 flex 属性来提供尺寸；Chrome 的实现说明指出，设置了 `contain: size` 但未指定尺寸的元素会渲染为 0px × 0px。<sup>[1](https://www.w3.org/TR/css-contain-1/)</sup><sup> • </sup><sup>[4](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Using)</sup><sup> • </sup><sup>[5](https://developer.chrome.com/blog/css-containment)</sup> 尺寸包含的独立收益主要是防止实现容器查询的工具出现无限循环；当包含在 `html` 或 `body` 上生效时，属性向视口或画布背景的传播会被禁用。<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup>

**布局包含**使盒子在布局意义上不透明：文档其余部分的布局不依赖于该盒子的内容。**绘制包含**保证后代不会显示在元素边界之外，这使浏览器可以在子树位于屏幕外时跳过绘制。<sup>[1](https://www.w3.org/TR/css-contain-1/)</sup>

**样式包含**在第 2 级中与 `inline-size`（仅包含行内轴）一同加入，它并不提供 [Shadow DOM](https://www.edgepedia.org/zh/shadow-dom) 或 `@scope` 那样的作用域样式；其主要用例是防止计数器变化影响树的其余部分。<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup><sup> • </sup><sup>[4](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Using)</sup>

简写值在不同规范级别中有所不同。在第 1 级中，`contain: strict` 计算为 `size layout paint`，`contain: content` 计算为 `layout paint`。<sup>[1](https://www.w3.org/TR/css-contain-1/)</sup> 第 2 级和 web-features 数据库将两者都扩展为包含样式包含，因此 `strict` 等于 `size layout style paint`，`content` 等于 `layout style paint`。<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup><sup> • </sup><sup>[3](https://web-platform-dx.github.io/web-features-explorer/features/contain/)</sup> 第 1 级规范称 `contain: content` 在实践中效果相当轻微，因此可以合理安全地广泛使用，但指出在没有尺寸包含的情况下，布局失效可能比期望的更远地向上渗透树，并建议尽可能使用 `contain: strict`。<sup>[1](https://www.w3.org/TR/css-contain-1/)</sup> Chrome 的指导则强调相反的方向：默认使用 `contain: content`，当 `content` 不够强时把 `strict` 作为备用方案。<sup>[5](https://developer.chrome.com/blog/css-containment)</sup>

## content-visibility 与跳过渲染

`content-visibility` 接受 `visible`（初始值）、`auto` 和 `hidden`。<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup> 设置了 `content-visibility: auto` 的元素会获得布局、样式和绘制包含，如同设置了 `contain: content`，并且在位于屏幕外且与用户无其他关联时还会额外获得尺寸包含；相关元素包括其子树中有焦点或选区的元素。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Using)</sup><sup> • </sup><sup>[6](https://web.dev/articles/content-visibility)</sup> 当元素跳过其内容时，用户代理会开启布局、样式、绘制和尺寸包含；内容不会被绘制，也不响应命中测试。<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup>

规范将 `auto` 元素建模为根据与视口的接近程度处于三种状态之一。即使元素未被跳过，布局、样式和绘制包含也持续存在，因此进入和退出跳过状态不会改变元素的布局行为。<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup>

由于跳过会施加尺寸包含，块布局中无尺寸的元素在屏幕外时高度为 0。`contain-intrinsic-size` 提供一个占位尺寸来代替渲染内容；`auto` 关键字让浏览器记住上次渲染的尺寸（如果有），并用它代替开发者提供的占位值，这对无限滚动器尤其有用。<sup>[6](https://web.dev/articles/content-visibility)</sup>

`content-visibility: hidden` 也会跳过内容，但更为彻底：被跳过的内容不得被用户代理特性（如页内查找或 Tab 顺序导航）访问，也不可选中或聚焦，类似于给内容设置 `display: none`。<sup>[10](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/content-visibility)</sup>

## 在浏览器引擎中如何工作

包含在每个阶段修剪工作。尺寸包含将子树从固有尺寸计算中移除，布局包含阻止失效跨越元素边界，绘制包含裁剪后代，使屏幕外子树可以被完全跳过。<sup>[1](https://www.w3.org/TR/css-contain-1/)</sup> 同样的修剪思路出现在引擎研究中：用于布局失效的 Spineless Traversal 算法在 50 个真实网页（包括 Twitter、Discord、GitHub 和 Lichess）上跨换行、flexbox 和固有尺寸进行了评估，在 2216 帧上的平均速度达到原来的 1.78 倍，而在重算字段少于 1% 的 60.7% 的基准测试中，实现了 2.35 倍或更高的加速。<sup>[11](https://arxiv.org/html/2411.10659v4)</sup> 规范还将跳过扩展到嵌套浏览上下文：如果 iframe 跳过其内容，用户代理应尽可能完全跳过该 iframe 事件循环中的 Update The Rendering 步骤。<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup>

## 数据一览

web.dev 上的一个旅行博客示例将 `content-visibility: auto` 应用于页面的分块区域，将初始加载渲染时间从 232 ms 降至 30 ms，提升 7 倍，典型页面的渲染成本预期可降低 50% 或更多。<sup>[6](https://web.dev/articles/content-visibility)</sup>

一项总样本 n = 1,154,365 的实地测试测得较小且更多样的效果。在第 75 百分位，移动端三星浏览器记录到 LCP 改善 148 ms，移动端 Chrome 记录到 INP 改善 27 ms，三星浏览器记录到 INP 快 47 ms 且 CLS 约降低 0.1；在第 95 百分位，移动端三星浏览器显示 INP 改善 245 ms。本地 DOM 插入测试显示，使用包含时渲染工作约为 54 ms，不使用包含时约为 732 ms；使用包含的初始页面渲染显示 172 ms 的渲染工作，减少约 80%。<sup>[7](https://www.speedkit.com/blog/field-testing-css-containment-for-web-performance-optimization)</sup> 同一研究在第 75 百分位测得移动端 Chrome 的 CLS 退化约 0.09，移动端三星浏览器约 0.02，并指出其范围仅限于两个网站和三个 CSS 非常相似的测试页面。<sup>[7](https://www.speedkit.com/blog/field-testing-css-containment-for-web-performance-optimization)</sup>

## 与其他作用域和隔离工具的比较

包含是一种渲染作用域提示，而非样式作用域机制。尽管名字如此，样式包含并不提供 Shadow DOM 或 `@scope` 那样的作用域样式；选择器仍然跨边界匹配，而样式包含主要限制计数器传播。<sup>[4](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Using)</sup> 它也与 iframe 不同，后者隔离文档本身；包含将子树保留在同一文档中，只是向浏览器承诺渲染效果不会泄漏。上文提到的规范中 iframe 渲染跳过规则是两者之间的一个接触点。<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup>

## 无障碍与行为注意事项

两个跳过值对用户的行为差异很大。在 `content-visibility: auto` 下，被跳过的内容必须仍正常可供用户代理特性（如页内查找和 Tab 顺序导航）使用，并且必须正常可聚焦和可选中；屏幕外内容保留在 DOM 和无障碍树中。<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup><sup> • </sup><sup>[10](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/content-visibility)</sup> 在 `hidden` 下，被跳过的内容会像 `display: none` 元素一样从无障碍树中省略。<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup>

由此产生两个陷阱。第一，设置了 `display: none` 或 `visibility: hidden` 的地标元素在 `content-visibility: auto` 下位于屏幕外时会出现在无障碍树中，可能造成杂乱，因此 web.dev 建议给这些元素添加 `aria-hidden="true"`。<sup>[6](https://web.dev/articles/content-visibility)</sup> 第二，规范指出 `auto` 子树中的元素即使在跳过时仍具有语义相关性，因此用户代理应降低 DOM 更新的优先级而非无限期跳过，尤其是对辅助技术。<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup>

主要的布局故障模式是滚动条移动：由于 `content-visibility: auto` 在元素跳过其内容时施加尺寸包含，尺寸取决于其内容的元素可能随着元素移出屏幕并开始跳过而使页面或滚动条位置跳动。修复方法包括固定尺寸、网格布局，或使用带 `auto` 关键字的 `contain-intrinsic-size` 来快照上次渲染的尺寸。<sup>[2](https://drafts.csswg.org/css-contain-2/)</sup> Google 的现代 Web 指南因此将 `content-visibility: auto` 与 `contain-intrinsic-size` 配对设为强制要求：否则，原本无尺寸的元素在屏幕外时可能塌缩为 0px 高度，导致布局偏移和滚动条跳动。<sup>[12](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/performance/defer-rendering-heavy-content.md)</sup> 不理解该属性的浏览器会直接忽略它。<sup>[9](https://web.dev/blog/css-content-visibility-baseline)</sup>

## 浏览器支持与近期发展

`contain` 随 Chrome 52 于 2016-07-20、Firefox 69 于 2019-09-03、Safari 15.4 于 2022-03-14、Edge 79 于 2020-01-15 发布，随着最后一个引擎自 2024-09-14 起发布而达到完全跨浏览器状态。<sup>[3](https://web-platform-dx.github.io/web-features-explorer/features/contain/)</sup> 2022 年 9 月的 CSS-CONTAIN-1 实现报告记录了各实现中 `size`、`layout`、`paint` 及组合 `size layout` 值的测试通过情况。<sup>[13](https://drafts.csswg.org/css-contain-1/implementation-report-2022-09)</sup>

`content-visibility` 来得更晚：Chrome 108 和 Chrome Android 108 于 2022-11-29，Edge 108 于 2022-12-05，Firefox 130 于 2024-09-03，Safari 26 于 2025-09-15，此时它在三大引擎中均成为 Baseline 新可用特性。<sup>[8](https://web-platform-dx.github.io/web-features-explorer/features/content-visibility/)</sup><sup> • </sup><sup>[9](https://web.dev/blog/css-content-visibility-baseline)</sup> MDN 指出 content-visibility 的语法反映的是 CSS 包含第 2 级，并非所有浏览器都实现了每一部分。<sup>[10](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/content-visibility)</sup>

在标准方面，CSS 包含第 1 级于 2024 年作为更新的 W3C 推荐标准重新发布，纳入了 2022 年推荐标准以来的变更。<sup>[14](https://www.w3.org/news/2024/updated-w3c-recommendation-css-containment-module-level-1/)</sup> 第 2 级仍为工作草案（最后发布于 2022 年 9 月 17 日，编辑草案为 2025 年 5 月 10 日），但互操作性很高：根据 2025 年 CSS 工作组互操作快照，978 个稳定 WPT 测试中有 859 个（87.8%）在 Chrome、Edge、Firefox 和 Safari 中通过。<sup>[15](https://github.com/w3c/csswg-drafts/issues/12699)</sup>

## References

1. [CSS Containment Module Level 1, W3C Recommendation](https://www.w3.org/TR/css-contain-1/)
2. [CSS Containment Module Level 2, Editor's Draft](https://drafts.csswg.org/css-contain-2/)
3. [Web features explorer: contain, web-platform-dx](https://web-platform-dx.github.io/web-features-explorer/features/contain/)
4. [Using CSS containment, MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Using)
5. [CSS containment in Chrome 52, Chrome for Developers](https://developer.chrome.com/blog/css-containment)
6. [content-visibility: the new CSS property that boosts your rendering performance, web.dev](https://web.dev/articles/content-visibility)
7. [Field Testing CSS Containment for Web Performance Optimization, Speed Kit](https://www.speedkit.com/blog/field-testing-css-containment-for-web-performance-optimization)
8. [Web features explorer: content-visibility, web-platform-dx](https://web-platform-dx.github.io/web-features-explorer/features/content-visibility/)
9. [The CSS content-visibility property is now Baseline Newly available, web.dev](https://web.dev/blog/css-content-visibility-baseline)
10. [content-visibility CSS property, MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/content-visibility)
11. [Spineless Traversal for Layout Invalidation, arXiv](https://arxiv.org/html/2411.10659v4)
12. [Defer rendering heavy content, GoogleChrome modern-web-guidance](https://github.com/GoogleChrome/modern-web-guidance/blob/main/skills/modern-web-guidance/guides/performance/defer-rendering-heavy-content.md)
13. [Implementation Report for CSS-CONTAIN-1 (September 2022)](https://drafts.csswg.org/css-contain-1/implementation-report-2022-09)
14. [Updated W3C Recommendation: CSS Containment Module Level 1 (2024), W3C](https://www.w3.org/news/2024/updated-w3c-recommendation-css-containment-module-level-1/)
15. [Add CSS Containment 2 to Rough Interop, w3c/csswg-drafts issue #12699](https://github.com/w3c/csswg-drafts/issues/12699)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Web development and web-platform technologies › CSS layout and styling features

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/css-containment

English original: https://www.edgepedia.org/css-containment
