The feed on your phone holds 40 articles, but the screen only fits 3. The browser still renders all of them as soon as the page opens, and in the Lab that takes about 80 ms for articles you may never scroll to.
CSS containment lets the browser do less. It's a promise you make to the browser: "what's inside this element stands on its own, and what changes in here doesn't affect the rest of the page." With that promise, the browser can keep Layout and Paint inside that element, and even skip rendering elements that can't be seen yet. Picture an office building. Each room has walls, so rearranging the desks in one room doesn't make the whole building get rearranged. The lights run on motion sensors: rooms with nobody in them stay dark, but the floor plan still records their size. The walls are contain, the sensor lights are content-visibility: auto, and the size on the floor plan is contain-intrinsic-size.
contain: a promise to the browser
In the CSS & Rendering Pipeline lesson you saw that Layout can spread: one element changes size, and its neighbours and parent are worked out again. The contain property cuts that spread off. Its values:
| Value | The promise | The visible effect |
|---|---|---|
layout |
layout inside the element doesn't affect the outside, and the other way round | the element becomes the reference for children with position: absolute or fixed |
paint |
its contents are never drawn outside its box | content that sticks out is clipped, and when the box is off screen its contents aren't painted |
size |
the element's size doesn't depend on its contents | without width and height, its height becomes 0 |
style |
counters and quotes inside don't leak out | rarely matters for performance |
Two shorthands come up a lot:
contain: contentis the same aslayout paint style. Safe for most components, because their size still follows their contents.contain: strictis the same assize layout paint style. The most aggressive one, but the element must have its own size.
/* A chat widget in the sidebar that changes with every new message */
.widget-chat {
contain: content; /* layout + paint + style */
}
/* A game canvas with a fixed size */
.game-board {
width: 320px;
height: 240px;
contain: strict; /* size + layout + paint + style */
}
contain: contenton the chat widget: with every new message, Layout and Paint are only worked out inside the widget, and don't spread to the article next to it.contain: strictneedswidthandheight, because withsizethe browser no longer looks at the contents to decide the size.contain: layout paintmeans the element is isolated: changes inside it don't affect the layout and paint of elements outside.
content-visibility: auto
content-visibility: auto goes further: while an element is far from the screen, the browser doesn't render its contents at all. Style, Layout and Paint for its contents are skipped until the element gets close to the screen.
The Lab's example: a feed of 40 articles, each 80 px tall, on a screen 240 px tall (it fits 3 articles). The browser also prepares a little area around the screen, in the Lab half the screen's height, 120 px.
| Articles rendered at first | Render time (2 ms per article) | |
|---|---|---|
without content-visibility |
40 | 80 ms |
with content-visibility: auto |
5 (3 on screen, 2 close by) | 10 ms |
The first render drops to about 10 ms. The other 35 articles are skipped, and only rendered when you scroll close to them. The 2 ms is the Lab's simulation, but the pattern is real: in the web.dev example, a blog page's first render went from 232 ms to 30 ms.
.article {
content-visibility: auto;
contain-intrinsic-size: auto 80px; /* estimated height while skipped */
}
content-visibility: autoalways applieslayout,paintandstylecontainment, and addssizewhile the element is skipped.- Because of
size, a skipped article's height becomes 0.contain-intrinsic-size: auto 80pxgives it a stand-in height of 80 px, so the page stays 40 × 80 = 3,200 px tall from the start. - The word
autoin it makes the browser remember an article's real height once it has been rendered, and reuse it when the article is skipped again. - The contents stay in the DOM: Ctrl+F and screen readers can still find them. That's different from
display: none. - Every major browser supports it now: Chrome 85, Firefox 125 and Safari 26 (2025). Safari 18 already knew it, but there Ctrl+F could not find the skipped contents.
Without contain-intrinsic-size, skipped articles are 0 px tall, so the page never settles on one height: Chromium keeps re-measuring it every time an article starts or stops being rendered. As you scroll, new articles get rendered, their height appears, and the scrollbar jumps around.
Which one when
- Long lists or pages (feeds, comments, articles with many sections):
content-visibility: autopluscontain-intrinsic-size. - Self-contained widgets that change often (chat, stock prices, a clock):
contain: content. - Fixed-size components that are truly self-contained (a game canvas, a fixed-size video player):
contain: strict. - Elements on the first screen (header, hero): not needed. They have to render right away anyway, so there's nothing to skip.
⚠️ Promises that end up breaking the page
contain: strictorsizewithout a size. The box collapses to 0 height and its contents overflow or get clipped.content-visibility: autowithoutcontain-intrinsic-size. Skipped articles count as 0 px, so the scrollbar jumps and content can shift as you scroll.contain: painton an element with a dropdown or tooltip. A menu that sticks out of the box gets clipped too.- Putting containment everywhere. It only pays off on parts that are big or change often. On a small box that sits still, it makes no difference.
TL;DR: contain is a promise that an element's contents stand on their own: layout and paint keep Layout and Paint inside its box, size makes its size independent of its contents, content is layout paint style, and strict (the most aggressive) is size layout paint style, so it must be given a size. content-visibility: auto skips rendering the contents of elements far from the screen: in the Lab, a feed of 40 articles only renders 5 at first, 10 ms instead of 80 ms. Always pair it with contain-intrinsic-size: auto 80px (an estimated height) so the scrollbar doesn't jump.
⚡ contain: a promise to the browser
Guess first: you write contain: layout paint on an element. What does it do?
The element is isolated: changes inside it don't affect the layout and paint of elements outside.
Write:
.widget-chat {
contain: content; /* layout + paint + style */
}
Meaning: With every new message, Layout and Paint are only worked out inside the widget. contain: strict adds size, so the element must have its own width and height, or its height becomes 0.
⚡ Skip what's far from the screen
Write:
.article {
content-visibility: auto;
contain-intrinsic-size: auto 80px;
}
Meaning: The contents of an element far from the screen aren't rendered at all. In the Lab, a feed of 40 articles only renders 5 at first: 10 ms, not 80 ms. contain-intrinsic-size gives an estimated height so the scrollbar doesn't jump.
⚡ Which one when
Meaning: Long lists or pages get content-visibility: auto. Widgets that change often get contain: content. Fixed-size components can take contain: strict. Elements on the first screen don't need it, because they have to render right away.