Containment & container queries

The properties that isolate part of the page from the rest, so the browser can skip work on it; give skipped content a placeholder size; make an element a container that @container rules can query; and hint at changes to come. The sizing properties that containment overrides are under Box model.

Containment

contain

Promises the browser that an element's subtree is independent of the rest of the page in one or more ways, so it can lay out, paint or style that part on its own and skip it when nothing inside has changed.

Syntaxnone | strict | content | [ [size | inline-size] || layout || style || paint ]
Initialnone
Applies toAll elements. Size, layout and paint containment have no effect on non-atomic inline boxes and internal table or ruby boxes; style containment always applies.
InheritedNo
AnimationNot animatable
SupportChrome 52 · Firefox 69 · Safari 15.4
inline-size: Chrome 105 · Firefox 101 · Safari 16
Speccss-contain-2
ValueEffect
sizeThe element is sized as if it had no content, so its size must come from elsewhere (width, height, contain-intrinsic-size).
inline-sizeLike size, but in the inline axis only.
layoutNothing inside affects layout outside, and the reverse. The element becomes a containing block for positioned and fixed descendants and starts a new stacking context.
paintDescendants are clipped to the padding box and are not painted when the element is off-screen.
styleCounters and open-quote/close-quote inside do not affect the rest of the page.
strictSame as size layout paint style.
contentSame as layout paint style: every type except size.
.widget { contain: content; }
A self-contained widget whose changes never force the rest of the page to re-lay out.

content-visibility

Lets the browser skip rendering an element's contents. With auto the contents are only laid out and painted when the element is near the viewport; with hidden they are never rendered, but unlike display: none the browser can keep their rendering state cached.

Syntaxvisible | auto | hidden
Initialvisible
Applies toElements for which size containment can apply
InheritedNo
AnimationDiscrete, with special rules for transitions to and from hidden
SupportChrome 85 · Firefox 125 · Safari 18
Speccss-contain-2

auto turns on layout, style and paint containment, plus size containment while the contents are skipped. Skipped contents stay in the document, so they can still be found with find-in-page and reached by keyboard focus. Give the element a placeholder size with contain-intrinsic-size so the scrollbar does not jump as sections render.

article > section {
  content-visibility: auto;
  contain-intrinsic-size: auto 40rem;
}
Render long-page sections only as they scroll into view, remembering each one's real height.

Intrinsic size placeholders

contain-intrinsic-size

Shorthand for contain-intrinsic-width and contain-intrinsic-height: the size an element with size containment is given, as if its contents were that size. One value sets both axes; two set width then height.

Syntax[ auto? [ none | <length [0,∞]> ] ]{1,2}
InitialSee individual properties
Applies toSee individual properties
InheritedSee individual properties
PercentagesSee individual properties
AnimationSee individual properties
SupportChrome 83 · Firefox 107 · Safari 17
Speccss-sizing-4

With auto, once the element has been rendered the browser remembers its real size and uses that instead of the length while its contents are skipped.

contain-intrinsic-width, contain-intrinsic-height, contain-intrinsic-block-size, contain-intrinsic-inline-size

The placeholder size in one axis: physical for -width and -height, flow-relative for -block-size and -inline-size. none means the element is sized as if empty in that axis.

Syntaxauto? [ none | <length [0,∞]> ]
Initialnone
Applies toElements with size containment
InheritedNo
AnimationBy computed value type
SupportChrome 95 · Firefox 107 · Safari 17
Speccss-sizing-4

Query containers

container

Shorthand for container-name and, after a slash, container-type.

Syntax<'container-name'> [ / <'container-type'> ]?
InitialSee individual properties
Applies toSee individual properties
InheritedSee individual properties
PercentagesSee individual properties
AnimationSee individual properties
SupportChrome 105 · Firefox 110 · Safari 16
Speccss-conditional-5
.cards { container: cards / inline-size; }

@container cards (width > 40rem) {
  .card { grid-template-columns: 1fr 2fr; }
}
Lay cards out side by side when their container, not the viewport, is wide enough.

container-name

One or more names for a query container, so an @container rule can target it by name instead of the nearest suitable ancestor.

Syntaxnone | <custom-ident>+
Initialnone
Applies toAll elements
InheritedNo
AnimationNot animatable
SupportChrome 105 · Firefox 110 · Safari 16
Speccss-conditional-5

container-type

Makes an element a query container for size queries, scroll-state queries or both.

Syntaxnormal | [ [ size | inline-size ] || scroll-state ]
Initialnormal
Applies toAll elements
InheritedNo
AnimationNot animatable
SupportChrome 105 · Firefox 110 · Safari 16
scroll-state: Chrome 133 · Firefox no · Safari no
Speccss-conditional-5
ValueEffect
normalNot a container for size queries. Every element can still be queried with style queries.
inline-sizeSize queries on the inline axis. Applies inline-size containment, so the element's width cannot depend on its contents.
sizeSize queries on both axes. Applies size containment, so the element needs a height from elsewhere.
scroll-stateQueries on the element's scroll state, such as whether it is stuck, snapped or scrollable.

inline-size is the usual choice. Because of the containment, a container cannot size itself from its children in the queried axis; give it a width from its parent layout.

Performance hints

will-change

Tells the browser which properties of an element are about to change, so it can prepare, for example by moving the element to its own compositing layer. An <animateable-feature> is scroll-position, contents or a property name.

Syntaxauto | <animateable-feature>#
Initialauto
Applies toAll elements
InheritedNo
AnimationNot animatable
SupportChrome 36 · Firefox 36 · Safari 9.1
Speccss-will-change-1

Naming a property that would create a stacking context or containing block, such as opacity or transform, makes the element create one straight away. Use it sparingly, on elements that really are about to animate, since each hint costs memory.

.drawer { will-change: transform; }
Prepare a drawer that slides in with a transform.