Sub topic 4
Layout containment
This chapter assumes you know the CSS box model, position values, and how floats and margin collapsing work. It also assumes you have read the earlier chapter on the contain property.
What it guarantees
Layout containment tells the browser that the element's internal layout is independent of the rest of the page. Two things follow:
- Nothing inside the element changes the layout of anything outside it.
- Nothing outside the element changes the layout of its contents.
When a script adds a node inside a contained element, the browser can re-lay out only that subtree. It does not have to check the whole document.
.widget {
contain: layout;
}What it changes
Layout containment does more than promise isolation. It also changes how the element behaves, so you can get surprises.
| Behavior | Effect with `contain: layout` |
|---|---|
| Containing block | The element becomes the containing block for absolutely and fixed positioned descendants. |
| Stacking context | The element creates one, so inner `z-index` values cannot interleave with outside content. |
| Formatting context | The element creates an independent one. Inner floats stay inside it, and outer floats do not intrude. |
| Margin collapsing | Child margins do not collapse through the element's edges. |
Layout containment does not clip overflow. Content that spills out of the box is still drawn. Clipping is the job of paint containment, which the next chapter covers.
Example: a positioned child
Without containment, .badge below is positioned against the page, because .widget has no position value.
<div class="widget">
<span class="badge">New</span>
Widget content
</div>
<style>
.widget {
contain: layout;
width: 200px;
height: 80px;
background: #eef;
}
.badge {
position: absolute;
top: 0;
right: 0;
}
</style>With contain: layout, the badge sits in the widget's top-right corner. The widget became its containing block, even without position: relative.
When to use it
Use layout containment on self-contained components that update often, such as a live chat message, a ticker, or a third-party embed. Each of these changes its own content without touching its surroundings. Avoid it where a descendant must escape the box. A dropdown menu or tooltip with position: absolute will be anchored to the contained element, and position: fixed will no longer be relative to the viewport.
Common errors
- Adding
contain: layoutto a wrapper and finding that aposition: fixedoverlay inside it scrolls with the wrapper. - Seeing
z-indexstop working across the boundary, because the element is now a stacking context. - Expecting overflow to be clipped. Use
contain: paintoroverflowfor that.
Exercise
A modal overlay with position: fixed; inset: 0 is placed inside a .panel element that has contain: layout. The overlay now covers only the panel and not the viewport. Why, and how do you fix it?
Show answer
Layout containment makes .panel the containing block for fixed descendants, so the overlay is positioned against the panel. Fix it by moving the overlay outside the contained element, or by removing contain: layout from the panel.