Sub topic 9
What containment changes: containing blocks, stacking contexts, clipping
This chapter assumes you know the CSS box model, position values, z-index, and overflow. It also assumes you have read the earlier chapters on the contain values. Containment gives the browser a promise about a subtree. To keep that promise, the browser also changes how the element behaves. These side effects are the most common source of "my layout broke when I added contain" bugs.
The three side effects
Different containment types switch on different side effects. The table lists what each type does.
| Value | New containing block for `absolute` and `fixed` descendants | New stacking context | Clips contents | Other effect |
|---|---|---|---|---|
| `layout` | Yes | Yes | No | Independent formatting context. Floats and margins do not escape. |
| `paint` | Yes | Yes | Yes, at the padding box | Independent formatting context. |
| `size` | No | No | No | Ignores contents when computing its own size. |
| `style` | No | No | No | Scopes counters and quotes. |
Layout and paint containment both change positioning and painting rules. Size and style containment do not. The content shorthand (layout paint style) and the strict shorthand (size layout paint style) include layout and paint, so they carry all of these effects.
A new containing block
Normally an absolute descendant is positioned against its nearest positioned ancestor. A fixed descendant is positioned against the viewport. An element with contain: layout or contain: paint becomes the containing block for both kinds. This works like transform does.
The effect appears when you put a "fixed" toolbar inside a contained wrapper:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Containing block demo</title>
<style>
body { margin: 0; height: 200vh; }
.wrapper {
margin: 100px auto;
width: 300px;
height: 150px;
background: #eef;
contain: layout;
}
.toolbar {
position: fixed;
top: 0;
right: 0;
padding: 8px;
background: #fc0;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="toolbar">Toolbar</div>
</div>
</body>
</html>Without contain: layout, the toolbar sticks to the top-right of the viewport while you scroll. With it, the toolbar sits at the top-right corner of .wrapper and scrolls away with the page. Remove the contain line to compare.
A new stacking context
A stacking context is a group of elements that are painted together and ordered as one unit. Layout and paint containment each create one. Two results follow:
- The
z-indexvalues of descendants only compete with each other inside the element. A child withz-index: 9999cannot rise above a sibling of the contained element that has a higher stacking order. - The contained element itself is ordered among its siblings as if it had a
z-indexof0, even when it hasz-index: auto.
.card {
contain: paint;
position: relative;
}
.card .popover {
position: absolute;
z-index: 9999; /* only wins inside .card */
}
.sticky-header {
position: sticky;
z-index: 10; /* paints above .popover, because .card is a separate group */
}If the header and the card overlap, the header paints above the popover despite the lower number. The popover's number is only compared with other elements inside .card.
Clipping
Paint containment tells the browser that no descendant draws outside the element's bounds. The browser enforces this by clipping descendants to the padding box. The clip applies to all painted overflow, including shadows, outlines, and positioned children.
.menu-host {
contain: paint;
width: 200px;
height: 40px;
}
.menu-host .menu {
position: absolute;
top: 100%; /* below the host: clipped away */
box-shadow: 0 4px 12px rgb(0 0 0 / 0.3);
}The menu is positioned below the host, so none of it is painted. The element's own box-shadow and outline are not clipped, because they belong to the element and not to its descendants.
The clip is similar to overflow: clip. It is different from overflow: hidden in one respect. A box with overflow: hidden is still a scroll container. A paint-contained box does not become one, so the user and scripts cannot scroll the hidden content into view, though anchor navigation and scrollIntoView can behave differently across browsers.
Layout containment alone does not clip anything. Content can still overflow visually, but it cannot affect layout outside the box.
Formatting context effects
Layout and paint containment also make the element an independent formatting context. This has two practical consequences.
- Floats inside the element do not stick out of it, so the element contains its floats without a clearfix. That is similar to
display: flow-root. - Margins of the first and last child do not collapse through the element's edges.
You might notice a small spacing change when you add contain: layout to a wrapper whose first child has a top margin. The margin that used to collapse out of the wrapper now stays inside it, which increases the wrapper's height.
Size containment and collapse
Size containment has no positioning side effects, but it has the most visible one. The element's size is computed as if it had no contents. With no explicit width and height, a block collapses to zero height, and a strict element collapses in both dimensions. Always pair contain: size and contain: strict with explicit sizes, or with contain-intrinsic-size, which is covered in the next chapters.
Why content-visibility inherits these effects
content-visibility: auto applies layout, style, and paint containment to the element. content-visibility: hidden applies size as well. So every side effect in this chapter applies to elements that use them. Adding content-visibility: auto to a section can clip its popovers and re-anchor its fixed children.
A debugging checklist
If something looks wrong after adding containment:
- Content is cut off: look for
paint,strict,content, orcontent-visibilityon an ancestor. - A fixed or absolute element moved: look for
layoutorpainton an ancestor. - A
z-indexhas no effect against outside elements: the ancestor has formed a stacking context. - An element has zero height: look for
sizeorstrictwithout a set size. - Spacing changed slightly: margins no longer collapse through the new formatting context.
Exercise
A page has a position: fixed cookie banner placed inside a <section> that has contain: content. After the change, the banner scrolls with the section instead of staying at the bottom of the viewport. Why did this happen, and what are two ways to fix it?
Show answer
contain: content includes layout and paint containment, so the section became the containing block for its fixed descendants. The banner is now positioned relative to the section. Fix it by moving the banner outside the contained section (for example, to be a direct child of body), or by removing layout and paint containment from that section, for instance by using contain: style or no contain at all. It would also be wrong to keep the banner inside and expect z-index to help, because the problem is the containing block and not stacking order.