Sub topic 8
The strict and content shorthands
This chapter assumes you know the four individual contain values (layout, paint, size, style) from the earlier chapters. It also assumes you know the box model, positioning, and overflow. Here you will learn the two shorthand keywords that bundle those values, what each bundle contains, and when to choose one over the other.
Why shorthands exist
Writing contain: layout paint style on every component is tedious. Most components that benefit from containment need the same combination, so the specification defines two keywords that expand to fixed lists of values.
| Keyword | Expands to | Needs explicit size? |
|---|---|---|
| `contain: strict` | `size layout paint style` | Yes |
| `contain: content` | `layout paint style` | No |
The only difference is size containment. strict includes it and content does not. Everything else in this chapter follows from that single difference.
contain: content
contain: content is the "safe" shorthand. It isolates the subtree for layout, paint, and style, but the element still sizes itself from its children as usual.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>contain: content</title>
<style>
.comment {
contain: content;
border: 1px solid #999;
padding: 8px;
margin-bottom: 8px;
}
</style>
</head>
<body>
<article class="comment">
<p>Short comment.</p>
</article>
<article class="comment">
<p>A much longer comment that wraps onto several lines. The box grows to fit it, because size containment is not applied.</p>
</article>
</body>
</html>Both comments grow to fit their text. The browser still gets the main benefit: when something inside one .comment changes, such as a node being added or a class being toggled, it can skip re-checking layout, paint, and style for the rest of the page. Use content for repeated, self-contained blocks whose height depends on their content: list items, comments, cards, and widgets.
contain: strict
contain: strict adds size containment. The browser then treats the element as if it had no children when it computes the element's size. With no explicit size, the element collapses to zero in the contained dimension, even though its children still exist.
.broken {
contain: strict;
/* No width or height: the box collapses to 0 x 0 plus padding and border */
}
.panel {
contain: strict;
width: 320px;
height: 200px;
}Notice that strict collapses both dimensions. A block element's width normally comes from its parent, so width: auto can still fill the available space. The height is the usual problem. Always set a height, or a contain-intrinsic-size (covered in a later chapter), when you use strict.
Use strict when the size is known ahead of time and does not depend on the contents. Typical cases are fixed-size dashboard tiles, virtualized list rows with a set height, and embedded canvases. It gives the browser the strongest guarantee, so it can do the least work.
Choosing between them
- Start with
content. It is hard to break, because the element still sizes itself. - Switch to
strictonly if the element has a size you can state without looking at its children. - If you need only part of the bundle, list the values yourself, for example
contain: layout paint.
What the shorthands change
Because the shorthands include layout and paint containment, they inherit those side effects. All of the following apply to both content and strict:
- The element becomes a containing block for absolutely positioned and fixed-position descendants. A
position: fixedchild will position itself relative to the contained element, not the viewport. - The element creates a stacking context. Descendants cannot interleave their
z-indexvalues with elements outside it. - The element establishes an independent formatting context. Floats inside do not escape, and margins of children do not collapse through it.
- Paint containment clips descendants to the element's overflow clip edge. Dropdowns, tooltips, and focus rings that stick out of the box are cut off.
- Style containment scopes counters (
counter-increment,counter-reset) to the subtree.
The fixed-position effect surprises people most often. Here is a small page that shows it:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Fixed child inside contain: content</title>
<style>
body { margin: 0; height: 2000px; }
.host {
contain: content;
margin: 100px;
width: 300px;
height: 150px;
background: #eef;
}
.pin {
position: fixed;
top: 0;
right: 0;
background: gold;
}
</style>
</head>
<body>
<div class="host">
<div class="pin">Pinned?</div>
</div>
</body>
</html>Without contain, the gold label sticks to the top right of the viewport while you scroll. With contain: content on .host, the label is positioned inside .host and scrolls away with it. Paint containment also clips it to the host's box.
Shorthand versus longhands in the cascade
contain is a single property, so a later declaration replaces an earlier one completely. The values do not merge.
.card {
contain: content;
}
.card.fixed-size {
contain: size; /* Replaces content. Layout, paint, and style are no longer contained. */
}
.card.fixed-size-full {
contain: content size; /* Layout, paint, style, and size. This is effectively strict. */
}The last rule shows that content size is equivalent to strict. If you want a modifier class to add size containment, restate the full list.
Where the shorthands are not enough
Neither shorthand includes inline-size containment. Container queries use container-type: inline-size, which applies layout, style, and inline-size containment. This contains the width only, so the element can still grow in height. That is often the better choice for responsive components, and it appears in a later chapter.
content-visibility: auto also applies layout, style, and paint containment automatically. It adds size containment only while the element's contents are being skipped. You therefore do not need to write contain: content on the same element.
Common mistakes
- Using
stricton an element with unknown height, then wondering why it vanished. The element is there, but its height is zero. - Using
contentorstricton a wrapper that contains a dropdown menu. The menu is clipped. - Expecting
contain: stricton a parent to stop children from re-laying out their own contents. It isolates the subtree from the outside, not the children from each other. - Writing
contain: strictandcontain: layoutin different rules and expecting a merge.
Exercise
A product card has a title and a description of varying length. The card grid should re-render faster when one card changes. Which shorthand do you choose, and what happens if you pick the other one without setting a height?
Show answer
Choose contain: content. The card's height depends on its text, so size containment would be wrong. With contain: strict and no height, the card's content-based height is ignored and the card collapses to zero height in the block direction, so the text would overflow or be clipped.
Exercise
You apply contain: content to a toolbar. A menu inside it uses position: absolute and extends below the toolbar, but it is now cut off. Which part of the shorthand causes this, and what is one way to fix it?
Show answer
Paint containment clips descendants to the element's bounds. Fix it by applying containment only to the elements that do not hold overflowing menus, or by moving the menu outside the contained element (for example, rendering it at the end of the body), or by dropping paint and using contain: layout style.