CSS Containment — Performance

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…

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:

/* 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 */
}

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 */
}

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

⚠️ Promises that end up breaking the page

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.

What you will learn