Sub topic 2
Containment: telling the browser a subtree is independent
This chapter assumes you know the CSS box model, normal block and inline layout, and positioning. It also assumes you can open the browser DevTools. The previous chapter covered why style, layout, and paint get expensive on large pages. Here we cover the idea that lets you limit that work. The contain property and its individual values come in later chapters.
The problem: the browser assumes everything is connected
By default, the browser treats a page as one connected structure. An element's size can depend on its children. A child's position can depend on a distant ancestor. A counter or a quote style can flow from one part of the tree to another.
Because of this, the browser cannot assume that a change in one place stays in that place. Suppose a script changes the text of a small <div> deep in the page. The browser must ask what else might be affected. The element could change height and push down every following sibling. It could change the size of a parent that is sized by its content. Often the answer is "nothing else", but the browser has to do work to find that out. On a page with thousands of elements, that check is the cost.
The idea: a promise about a subtree
Containment is a promise you make to the browser. You say that a given element's subtree is independent of the rest of the page in specific, named ways. In return, the browser may skip work it would otherwise do to stay safe.
The promise runs in two directions:
- Nothing inside the element affects the outside, in the ways you named.
- Nothing outside the element affects the inside, in the ways you named.
The browser enforces the promise by changing how the element behaves. If you promise that the layout is independent, the browser makes it independent, whether or not your design needed it to be. This is why containment is a trade. You give up some flexibility and the browser gives you a smaller area to recompute.
What "independent" can mean
There are four kinds of independence. Each corresponds to one value of the contain property.
| Kind of independence | What the browser may assume | Later chapter |
|---|---|---|
| Layout | Layout inside does not move anything outside, and the reverse | Layout containment |
| Paint | Descendants are never drawn outside the element's bounds | Paint containment |
| Size | The element's size does not depend on its contents | Size containment |
| Style | Certain style effects, such as counters, do not leak out | Style containment |
You can promise one, several, or all of them. Each promise is separate, and each has its own side effects. That is why the chapters on them are separate.
A boundary that stops the spread
The diagram below shows the effect in practice. Without a boundary, the browser must follow a change outward. With a boundary, the browser can stop at the edge.
The boundary does not make the work inside the subtree free. It only guarantees that the work does not grow with the size of the rest of the page.
A first example
Here is a small page with a widget that updates every second. The widget is the only part that changes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Containment idea</title>
<style>
.widget {
contain: layout;
border: 1px solid #888;
padding: 8px;
}
</style>
</head>
<body>
<div class="widget" id="clock"></div>
<p>The rest of the page lives here.</p>
<script>
const clock = document.getElementById('clock');
setInterval(() => {
clock.textContent = new Date().toLocaleTimeString();
}, 1000);
</script>
</body>
</html>With contain: layout, the browser knows that nothing inside .widget affects layout outside it, and nothing outside affects the layout of its contents. When the clock text changes, the browser can limit layout work to the widget. On a page this small you will not notice a difference. The benefit shows up when the paragraph is replaced by thousands of nodes.
The widget still grows or shrinks if its text changes length, because layout containment does not fix its size. It only stops the effects of that change from spreading to unrelated content. Fixing the size is a different promise, covered under size containment.
Containment is not free
Making the promise changes how the element behaves. Three things to expect:
- The browser may treat the element differently in layout. Some containment types make the element a containing block for absolutely and fixed-positioned descendants, and some create a stacking context. Your existing positioning may change.
- Content may be clipped. Paint containment prevents drawing outside the element's bounds. A dropdown or tooltip that used to overflow the element will be cut off.
- Size may collapse. If you promise that size is independent of contents, an element with no explicit size has nothing to size itself from.
A later chapter covers these side effects in detail. For now, remember that you should only promise independence that is really true for your design. If a tooltip must escape its parent, do not contain the parent's paint.
Where containment pays off
Containment helps most when three conditions hold:
- The subtree is self-contained in your design, such as a card, a widget, a list item, or a comment.
- The page has many such subtrees, or one very large one.
- The subtree changes often, or the page is large enough that the cost of checking outside the subtree is noticeable.
It helps less on small pages, on elements that must affect their surroundings, and on subtrees that are already cheap to lay out. Containment is an optimization, so measure it. A later chapter shows how to do that in DevTools.
Two features built on the same idea
You will meet containment in two places beyond the contain property:
content-visibilityapplies containment and then goes further, allowing the browser to skip rendering of content that is off-screen or hidden.- Container queries need the container's size to be independent of its children, or the query could never be resolved. Declaring a query container applies containment for exactly that reason.
Both are covered in later chapters. They depend on the principle introduced here: a subtree can be made independent, and the browser can exploit that.
Exercise
A page has a long list of comment cards. Each card updates its like count when a user clicks a button. Explain, in terms of the containment promise, why adding layout containment to each card can reduce work. Name one situation where promising layout independence would be a mistake.
Show answer
Without containment, the browser must consider that a changed like count could change the card's size and so push down every card after it, which means checking the rest of the list. With layout containment, the card promises that nothing inside it affects layout outside it, so the browser can limit layout work to the card. It would be a mistake if the design needs the card's contents to affect surrounding layout, for example if a child element must position itself relative to something outside the card, or if the card's contents must be allowed to influence outside elements. The promise would then be false, and the browser would enforce it anyway and break the design.