Sub topic 5
Paint containment
This chapter covers contain: paint. It assumes you know the CSS box model, positioning, and overflow. It also assumes you have read the earlier chapters on the rendering pipeline and the idea of containment. Layout containment is covered in the previous chapter and is not repeated here.
What paint containment promises
Paint is the last big step of rendering. The browser turns boxes into pixels. When you write contain: paint, you make this promise to the browser:
- No descendant of the element will be drawn outside the element's padding box.
- If the element is off-screen, nothing inside it can be visible either.
The browser can then act on that promise. It does not need to look at the descendants of an off-screen element to decide whether they might show up on screen. It can skip painting them. It also knows the element's contents are clipped, so it can limit the area it repaints when something inside changes.
.card {
contain: paint;
}The promise is enforced, not just trusted. The browser clips anything that sticks out. You are not hinting at a property of the page. You are changing how the element renders.
What changes when you apply it
Paint containment has visible side effects. They matter more than the performance benefit, because they can break a layout that worked before.
Overflow is clipped
Content that would normally spill out of the element is cut off at the padding box edge. It behaves like overflow: clip, with no scrolling. Consider a tooltip positioned outside its parent:
<div class="card">
Hover target
<span class="tooltip">I am a tooltip</span>
</div>
<style>
.card {
position: relative;
width: 200px;
height: 80px;
border: 1px solid #888;
contain: paint;
}
.tooltip {
position: absolute;
top: -30px;
left: 0;
}
</style>The tooltip sits 30px above the card. With contain: paint, it is clipped and you see nothing. Remove the contain line and it appears.
It becomes a containing block
An element with paint containment becomes the containing block for its absolutely positioned and fixed-position descendants. This holds even if the element itself is position: static. A position: fixed child no longer sticks to the viewport. It is placed relative to the contained element.
It creates a stacking context
The element gets its own stacking context. Descendant z-index values only compete with each other inside it. A child with z-index: 9999 cannot rise above a sibling of the contained element that has a higher stacking order.
It establishes an independent formatting context
The element also becomes an independent formatting context. Margins of children do not collapse through its edges, and floats inside it are contained.
| Effect | Result | Typical surprise |
|---|---|---|
| Overflow clipping | Content outside the padding box is not drawn | Tooltips, dropdowns, and focus rings cut off |
| Containing block | Absolute and fixed descendants position against the element | A "fixed" header scrolls with the card |
| Stacking context | `z-index` inside is isolated | A menu appears under a neighbor |
| Formatting context | No margin collapse through the edge | Extra space appears above or below |
Where it helps
Paint containment helps most when you have many similar blocks, and some are often off-screen: feed items, table rows built from blocks, comment threads, and result cards. The browser can cull the ones outside the viewport at almost no cost.
It also helps when a block changes often. An animated widget inside a paint-contained box cannot cause repaints outside its bounds, so the browser repaints a smaller region.
The gain depends on the page. A small page with a few elements shows no measurable change. Measure before you apply it widely, as covered in the DevTools chapter.
Paint containment and the shorthands
You rarely write contain: paint alone for large wins. It is one part of contain: content, which is layout paint style, and of contain: strict, which adds size. The content-visibility: auto property applies layout, style, and paint containment for you, plus the skipping behavior. When you use content-visibility: auto, you already have paint containment and do not need to add it.
You can combine values directly:
.result {
contain: layout paint;
}Debugging clipped content
When something disappears after you add paint containment, work through these steps:
- Toggle the
containdeclaration in DevTools. If the content returns, containment is the cause. - Find the element that should extend outside its parent. Check absolutely positioned children, box shadows, outlines, and focus rings.
- Move
contain: paintto a deeper element, or remove it from the ancestor of the overflowing part. - If you need the box to stay contained but need a shadow, add padding to the contained element and compensate with margin.
Box shadows and outlines are painted outside the border box, so they are clipped too. An element with a box-shadow of 0 0 20px on a paint-contained element shows a cut-off shadow at its own edge only if the shadow belongs to a descendant. The element's own shadow is part of its own painting and is not clipped by its own containment. A child's shadow that extends past the parent's padding box is clipped.
Progressive enhancement
Paint containment is widely supported in current browsers. Because the effect is a performance optimization, an unsupported browser simply renders everything without clipping. Check that your layout is correct in both cases. If clipping is needed for the design, use overflow: hidden or overflow: clip as well, so the visual result does not depend on contain.
.card {
overflow: clip;
contain: paint;
}Exercise
A page has a .panel with contain: paint. Inside it is a position: fixed banner with top: 0. The banner no longer stays at the top of the browser window, and part of it is cut off. Explain both symptoms and give one fix.
Show answer
Paint containment makes .panel the containing block for fixed-position descendants, so the banner is placed relative to the panel and scrolls with it instead of the viewport. The panel also clips anything outside its padding box, so any part of the banner outside the panel is cut off. Fix it by moving the banner outside .panel in the DOM, or by removing contain: paint from the panel.
Exercise
You add contain: paint to a list of 500 cards. A tooltip inside each card is positioned 20px above the card and is now invisible. Why, and how can you keep the optimization without losing the tooltip?
Show answer
The tooltip is outside the card's padding box, and paint containment clips it. Either render the tooltip in a separate layer outside the cards, for example a single element at the end of body, or remove paint containment from the cards that need the tooltip. You could also place the tooltip inside the card's bounds.