Sub topic 5
Paint containment
This chapter assumes you know the CSS box model, overflow, and positioning, and that you have opened browser DevTools before. You should also know that the browser runs style, layout, and paint steps to put a page on screen.
What paint containment promises
Paint containment is the contain: paint value. It tells the browser that nothing inside the element is drawn outside the element's bounds.
.card {
contain: paint;
}The browser can use that promise in two ways:
- If the element is off-screen, it can skip painting the element and all its descendants. Their pixels could not appear anywhere else.
- It can treat the element's box as a clipping boundary and limit repaint work to that area.
Side effects you will see
Paint containment changes how the element behaves, so check these before you apply it.
- Clipped overflow. Descendants that extend past the element's padding box are clipped, much like
overflow: clip. A dropdown menu or tooltip that sticks out of the card will be cut off. - New containing block. The element becomes the containing block for absolutely and fixed-positioned descendants.
- New stacking context. The element creates its own stacking context, so
z-indexon its children only competes inside it. - New formatting context. The element establishes an independent formatting context, so it contains its children's floats and margins.
The size of the element is not affected. Paint containment does not make the box ignore its contents, which is different from size containment.
.card {
contain: paint;
position: relative;
width: 300px;
}
.card .menu {
position: absolute;
top: 100%;
left: 0;
}In this example .menu sits below the card's bottom edge. With contain: paint it is clipped and never visible. Remove the containment, or move the menu outside the card, to show it.
When to use it
Paint containment fits well on repeated, self-contained blocks such as cards, list rows, or comment boxes. These have content that stays inside their box and are often scrolled out of view.
Avoid it on elements that need to show overflow, such as menus, popovers, and tooltips. It also gives little benefit for a single large element that is always visible.
You usually do not need to write contain: paint alone for long pages. The content-visibility property, covered later, applies paint containment for you and adds skipping of rendering work.
Common mistakes
- Adding it to a parent of a dropdown and wondering why the dropdown vanished.
- Expecting it to change the element's size. It does not.
- Forgetting the new stacking context, which can make a child with a high
z-indexappear below a sibling of its parent.
Exercise
A .card has contain: paint and contains an absolutely positioned tooltip placed 10px below the card's bottom edge. What do you see, and what are two ways to fix it?
Show answer
The tooltip is clipped and not visible, because paint containment clips descendants to the element's bounds. Fix it by removing contain: paint from the card, or by moving the tooltip outside the card so a contained ancestor does not clip it. You could also place the tooltip inside the card's bounds.