--- title: contain slug: Web/CSS/contain tags: - CSS - CSS Containment - CSS 属性 - Style - Web - 参考 - 布局 - 样式 - 渲染 translation_of: Web/CSS/contain ---
CSS contain 属性允许开发者声明当前元素和它的内容尽可能的独立于 DOM 树的其他部分。这使得浏览器在重新计算布局、样式、绘图、大小或这四项的组合时,只影响到有限的 DOM 区域,而不是整个页面,可以有效改善性能。
这个属性在包含大量独立组件的页面非常实用,它可以防止某个小部件的 CSS 规则改变对页面上的其他东西造成影响。
Note: If applied (with value: paint, strict or content), this property creates:
absolute or fixed)./* 关键词值 */ contain: none; /* 等价于 contain: layout paint size */ contain: strict; /* 等价于 contain: layout paint W3C链接: https://www.w3.org/TR/css-contain-2/#valdef-contain-content*/ contain: content; contain: size; contain: layout; contain: style; contain: paint; /* 支持指定多个关键词 */ contain: size paint; contain: size layout paint; /* 全局值 */ contain: inherit; contain: initial; contain: unset;
The contain property is specified as either one of the following:
none, strict, or content keyword.size, layout, style, and paint keywords in any order.nonestrictstyle 外的所有的包含规则应用于这个元素。等价于 contain: size layout paint。contentsize 和 style 外的所有包含规则。等价于 contain: layout paint。sizelayoutstylepaint{{cssinfo}}
The markup below consists of a number of articles, each with content:
<h1>My blog</h1> <article> <h2>Heading of a nice article</h2> <p>Content here.</p> </article> <article> <h2>Another heading of another article</h2> <img src="graphic.jpg" alt="photo"> <p>More content here.</p> </article>
Each <article> and <img> is given a border, and the images are floated:
img {
float: left;
border: 3px solid black;
}
article {
border: 1px solid black;
}
{{EmbedLiveSample('Simple_layout', '100%', '300')}}
If we were to insert another image at the bottom of the first article, a large portion of the DOM tree may be re-laid out or repainted, and this would also interfere with the layout of the second article:
<h1>My blog</h1> <article> <h2>Heading of a nice article</h2> <p>Content here.</p> <img src="i-just-showed-up.jpg" alt="social"> </article> <article> <h2>Another heading of another article</h2> <img src="graphic.jpg" alt="photo"> <p>More content here.</p> </article>
img {
float: left;
border: 3px solid black;
}
article {
border: 1px solid black;
}
As you can see, because of the way floats work, the first image ends up inside the area of the second article:
{{EmbedLiveSample('Float_interference', '100%', '300')}}
If we give each article the contain property with a value of content, when new elements are inserted the browser understands it only needs to recalculate the containing element's subtree, and not anything outside it:
<h1>My blog</h1> <article> <h2>Heading of a nice article</h2> <p>Content here.</p> <img src="i-just-showed-up.jpg" alt="social"> </article> <article> <h2>Another heading of another article</h2> <img src="graphic.jpg" alt="photo"> <p>More content here.</p> </article>
img {
float: left;
border: 3px solid black;
}
article {
border: 1px solid black;
contain: content;
}
This also means that the first image no longer floats down to the second article, and instead stays inside it's containing element's bounds:
{{EmbedLiveSample('Fixing_with_contain', '100%', '330')}}
| 规范 | 状态 | 备注 |
|---|---|---|
| {{SpecName('CSS Containment 2', '#contain-property', 'contain')}} | {{Spec2('CSS Containment')}} | Added style keyword |
{{Compat("css.properties.contain")}}